package authenticator

import (
	"context"
	"k8s.io/apiserver/pkg/authentication/authenticator"
	"net/http"
)

//since we implemented AuthenticatePassword, we can use BasicAuthenticator directly
type Authenticator struct {
}

func (auth *Authenticator) AuthenticateRequest(req *http.Request) (*authenticator.Response, bool, error) {
	username, password, _ := req.BasicAuth()
	//call from k8s
	//if !found {
	//	return nil, false, nil
	//}
	return auth.AuthenticatePassword(req.Context(), username, password)
}

func (auth *Authenticator) AuthenticatePassword(ctx context.Context, user, password string) (*authenticator.Response, bool, error) {
	//tep server already checked the username and password, the api server only need a username to do rbac check
	return &authenticator.Response{
		User: &userInfo{
			Name:   user,
			UID:    user,
			Groups: []string{"tep"},
		},
	}, true, nil
}

type userInfo struct {
	Name   string
	UID    string
	Groups []string
	Extra  map[string][]string
}

func (i *userInfo) GetName() string {
	return i.Name
}

func (i *userInfo) GetUID() string {
	return i.UID
}

func (i *userInfo) GetGroups() []string {
	return i.Groups
}

func (i *userInfo) GetExtra() map[string][]string {
	return i.Extra
}
