Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api for identity provider login #5534

Merged
merged 7 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions pkg/kapis/oauth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
)

const (
ldapProvider = "LDAPIdentityProvider"
KindTokenReview = "TokenReview"
grantTypePassword = "password"
grantTypeRefreshToken = "refresh_token"
Expand Down Expand Up @@ -688,3 +689,33 @@ func (h *handler) userinfo(req *restful.Request, response *restful.Response) {
}
response.WriteEntity(result)
}

func (h *handler) ldapLogin(req *restful.Request, response *restful.Response) {
username, _ := req.BodyParameter("username")
password, _ := req.BodyParameter("password")

authenticated, providerName, err := h.passwordAuthenticator.Authenticate(req.Request.Context(), username, password)
if err != nil {
api.HandleBadRequest(response, req, err)
return
}

if providerName != ldapProvider {
err = errors.New("username or password is not correct")
api.HandleBadRequest(response, req, err)
return
}

t, err := h.issueTokenTo(authenticated)
if err != nil {
api.HandleInternalError(response, req, err)
return
}

requestInfo, _ := request.RequestInfoFrom(req.Request.Context())
if err = h.loginRecorder.RecordLogin(authenticated.GetName(), iamv1alpha2.Ldap, providerName, requestInfo.SourceIP, requestInfo.UserAgent, nil); err != nil {
klog.Errorf("Failed to record successful login for user %s, error: %v", authenticated.GetName(), err)
}

_ = response.WriteEntity(t)
}
8 changes: 8 additions & 0 deletions pkg/kapis/oauth/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ func AddToContainer(c *restful.Container, im im.IdentityManagementInterface,
Returns(http.StatusOK, http.StatusText(http.StatusOK), "").
Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))

ws.Route(ws.POST("/login-ldap").
zhou1203 marked this conversation as resolved.
Show resolved Hide resolved
Doc("Login with ldap user").
Param(ws.FormParameter("username", "The username of the relevant user in ldap")).
Param(ws.FormParameter("password", "The password of the relevant user in ldap")).
To(handler.ldapLogin).
Returns(http.StatusOK, http.StatusText(http.StatusOK), oauth.Token{}).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))

c.Add(ws)

// legacy auth API
Expand Down
1 change: 1 addition & 0 deletions staging/src/kubesphere.io/api/iam/v1alpha2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ const (
BasicAuth LoginType = "Basic"
OAuth LoginType = "OAuth"
Token LoginType = "Token"
Ldap LoginType = "Ldap"
zhou1203 marked this conversation as resolved.
Show resolved Hide resolved
)

// +kubebuilder:object:root=true
Expand Down