Skip to content

Commit

Permalink
add api for ldap login
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou1203 committed Feb 17, 2023
1 parent 4ee6c8d commit 7fef40b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
25 changes: 25 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,27 @@ 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 || providerName != ldapProvider {
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").
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"
)

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

0 comments on commit 7fef40b

Please sign in to comment.