register.go 6.7 KB
Newer Older
H
update  
hongming 已提交
1
/*
H
hongming 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
Copyright 2020 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
H
update  
hongming 已提交
16 17 18 19 20 21 22 23

package oauth

import (
	"github.com/emicklei/go-restful"
	restfulspec "github.com/emicklei/go-restful-openapi"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/api/auth"
H
update  
hongming 已提交
24
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
25
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
H
update  
hongming 已提交
26
	"kubesphere.io/kubesphere/pkg/constants"
H
hongming 已提交
27
	"kubesphere.io/kubesphere/pkg/models/iam/im"
H
update  
hongming 已提交
28 29 30
	"net/http"
)

H
update  
hongming 已提交
31 32 33 34 35 36
// ks-apiserver includes a built-in OAuth server. Users obtain OAuth access tokens to authenticate themselves to the API.
// The OAuth server supports standard authorization code grant and the implicit grant OAuth authorization flows.
// All requests for OAuth tokens involve a request to <ks-apiserver>/oauth/authorize.
// Most authentication integrations place an authenticating proxy in front of this endpoint, or configure ks-apiserver
// to validate credentials against a backing identity provider.
// Requests to <ks-apiserver>/oauth/authorize can come from user-agents that cannot display interactive login pages, such as the CLI.
Z
zryfish 已提交
37
func AddToContainer(c *restful.Container, im im.IdentityManagementInterface, tokenOperator im.TokenManagementInterface, authenticator im.PasswordAuthenticator, loginRecorder im.LoginRecorder, options *authoptions.AuthenticationOptions) error {
H
update  
hongming 已提交
38
	ws := &restful.WebService{}
H
update  
hongming 已提交
39 40 41 42
	ws.Path("/oauth").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)

Z
zryfish 已提交
43
	handler := newHandler(im, tokenOperator, authenticator, loginRecorder, options)
H
update  
hongming 已提交
44 45 46 47

	// Implement webhook authentication interface
	// https://kubernetes.io/docs/reference/access-authn-authz/authentication/#webhook-token-authentication
	ws.Route(ws.POST("/authenticate").
H
hongming 已提交
48 49
		Doc("TokenReview attempts to authenticate a token to a known user. Note: TokenReview requests may be "+
			"cached by the webhook token authenticator plugin in the kube-apiserver.").
H
update  
hongming 已提交
50
		Reads(auth.TokenReview{}).
Z
zryfish 已提交
51
		To(handler.TokenReview).
H
update  
hongming 已提交
52
		Returns(http.StatusOK, api.StatusOK, auth.TokenReview{}).
H
hongming 已提交
53
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))
H
update  
hongming 已提交
54

55 56
	// Only support implicit grant flow
	// https://tools.ietf.org/html/rfc6749#section-4.2
H
update  
hongming 已提交
57
	ws.Route(ws.GET("/authorize").
H
update  
hongming 已提交
58
		Doc("All requests for OAuth tokens involve a request to <ks-apiserver>/oauth/authorize.").
H
hongming 已提交
59 60 61 62 63 64 65 66
		Param(ws.QueryParameter("response_type", "The value MUST be one of \"code\" for requesting an "+
			"authorization code as described by [RFC6749] Section 4.1.1, \"token\" for requesting an access token (implicit grant)"+
			" as described by [RFC6749] Section 4.2.2.").Required(true)).
		Param(ws.QueryParameter("client_id", "The client identifier issued to the client during the "+
			"registration process described by [RFC6749] Section 2.2.").Required(true)).
		Param(ws.QueryParameter("redirect_uri", "After completing its interaction with the resource owner, "+
			"the authorization server directs the resource owner's user-agent back to the client.The redirection endpoint "+
			"URI MUST be an absolute URI as defined by [RFC3986] Section 4.3.").Required(false)).
H
hongming 已提交
67 68 69
		To(handler.Authorize).
		Returns(http.StatusFound, http.StatusText(http.StatusFound), "").
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))
Z
zryfish 已提交
70 71 72 73
	// Resource Owner Password Credentials Grant
	// https://tools.ietf.org/html/rfc6749#section-4.3
	ws.Route(ws.POST("/token").
		Consumes("application/x-www-form-urlencoded").
H
hongming 已提交
74 75
		Doc("The resource owner password credentials grant type is suitable in\n"+
			"cases where the resource owner has a trust relationship with the\n"+
Z
zryfish 已提交
76
			"client, such as the device operating system or a highly privileged application.").
H
hongming 已提交
77 78 79 80 81 82
		Param(ws.FormParameter("grant_type", "Value MUST be set to \"password\".").Required(true)).
		Param(ws.FormParameter("username", "The resource owner username.").Required(true)).
		Param(ws.FormParameter("password", "The resource owner password.").Required(true)).
		To(handler.Token).
		Returns(http.StatusOK, http.StatusText(http.StatusOK), &oauth.Token{}).
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))
H
update  
hongming 已提交
83 84 85

	// Authorization callback URL, where the end of the URL contains the identity provider name.
	// The provider name is also used to build the callback URL.
86
	ws.Route(ws.GET("/callback/{callback}").
H
update  
hongming 已提交
87
		Doc("OAuth callback API, the path param callback is config by identity provider").
H
hongming 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100
		Param(ws.QueryParameter("access_token", "The access token issued by the authorization server.").
			Required(true)).
		Param(ws.QueryParameter("token_type", "The type of the token issued as described in [RFC6479] Section 7.1. "+
			"Value is case insensitive.").Required(true)).
		Param(ws.QueryParameter("expires_in", "The lifetime in seconds of the access token.  For "+
			"example, the value \"3600\" denotes that the access token will "+
			"expire in one hour from the time the response was generated."+
			"If omitted, the authorization server SHOULD provide the "+
			"expiration time via other means or document the default value.")).
		Param(ws.QueryParameter("scope", "if identical to the scope requested by the client;"+
			"otherwise, REQUIRED.  The scope of the access token as described by [RFC6479] Section 3.3.").Required(false)).
		Param(ws.QueryParameter("state", "if the \"state\" parameter was present in the client authorization request."+
			"The exact value received from the client.").Required(true)).
101
		To(handler.oAuthCallBack).
H
hongming 已提交
102 103
		Returns(http.StatusOK, api.StatusOK, oauth.Token{}).
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))
H
update  
hongming 已提交
104 105

	c.Add(ws)
H
update  
hongming 已提交
106

H
hongming 已提交
107 108 109 110 111 112 113 114 115 116 117
	// legacy auth API
	legacy := &restful.WebService{}
	legacy.Path("/kapis/iam.kubesphere.io/v1alpha2/login").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)
	legacy.Route(legacy.POST("").
		To(handler.Login).
		Deprecate().
		Doc("KubeSphere APIs support token-based authentication via the Authtoken request header. The POST Login API is used to retrieve the authentication token. After the authentication token is obtained, it must be inserted into the Authtoken header for all requests.").
		Reads(auth.LoginRequest{}).
		Returns(http.StatusOK, api.StatusOK, oauth.Token{}).
H
hongming 已提交
118
		Metadata(restfulspec.KeyOpenAPITags, []string{constants.AuthenticationTag}))
H
hongming 已提交
119 120 121

	c.Add(legacy)

H
update  
hongming 已提交
122 123
	return nil
}