handler.go 10.6 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

package oauth

import (
H
update  
hongming 已提交
20
	"fmt"
H
update  
hongming 已提交
21
	"github.com/emicklei/go-restful"
H
update  
hongming 已提交
22
	apierrors "k8s.io/apimachinery/pkg/api/errors"
H
hongming 已提交
23
	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Z
zryfish 已提交
24
	"k8s.io/apiserver/pkg/authentication/user"
H
update  
hongming 已提交
25 26 27
	"k8s.io/klog"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/api/auth"
H
hongming 已提交
28
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
H
update  
hongming 已提交
29
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
H
update  
hongming 已提交
30
	"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
31
	authoptions "kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
H
update  
hongming 已提交
32
	"kubesphere.io/kubesphere/pkg/apiserver/request"
H
hongming 已提交
33
	"kubesphere.io/kubesphere/pkg/models/iam/im"
H
update  
hongming 已提交
34
	"net/http"
H
update  
hongming 已提交
35 36
)

Z
zryfish 已提交
37 38 39 40 41 42
type handler struct {
	im            im.IdentityManagementInterface
	options       *authoptions.AuthenticationOptions
	tokenOperator im.TokenManagementInterface
	authenticator im.PasswordAuthenticator
	loginRecorder im.LoginRecorder
H
update  
hongming 已提交
43 44
}

Z
zryfish 已提交
45 46
func newHandler(im im.IdentityManagementInterface, tokenOperator im.TokenManagementInterface, authenticator im.PasswordAuthenticator, loginRecorder im.LoginRecorder, options *authoptions.AuthenticationOptions) *handler {
	return &handler{im: im, tokenOperator: tokenOperator, authenticator: authenticator, loginRecorder: loginRecorder, options: options}
H
update  
hongming 已提交
47 48 49 50
}

// Implement webhook authentication interface
// https://kubernetes.io/docs/reference/access-authn-authz/authentication/#webhook-token-authentication
Z
zryfish 已提交
51
func (h *handler) TokenReview(req *restful.Request, resp *restful.Response) {
H
update  
hongming 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	var tokenReview auth.TokenReview

	err := req.ReadEntity(&tokenReview)

	if err != nil {
		klog.Error(err)
		api.HandleBadRequest(resp, req, err)
		return
	}

	if err = tokenReview.Validate(); err != nil {
		klog.Error(err)
		api.HandleBadRequest(resp, req, err)
		return
	}

Z
zryfish 已提交
68
	authenticated, err := h.tokenOperator.Verify(tokenReview.Spec.Token)
H
update  
hongming 已提交
69 70 71 72 73 74 75 76 77 78 79

	if err != nil {
		klog.Errorln(err)
		api.HandleInternalError(resp, req, err)
		return
	}

	success := auth.TokenReview{APIVersion: tokenReview.APIVersion,
		Kind: auth.KindTokenReview,
		Status: &auth.Status{
			Authenticated: true,
Z
zryfish 已提交
80
			User:          map[string]interface{}{"username": authenticated.GetName(), "uid": authenticated.GetUID()},
H
update  
hongming 已提交
81 82 83 84 85
		},
	}

	resp.WriteEntity(success)
}
H
update  
hongming 已提交
86

Z
zryfish 已提交
87 88
func (h *handler) Authorize(req *restful.Request, resp *restful.Response) {
	authenticated, ok := request.UserFrom(req.Request.Context())
H
update  
hongming 已提交
89 90
	clientId := req.QueryParameter("client_id")
	responseType := req.QueryParameter("response_type")
91
	redirectURI := req.QueryParameter("redirect_uri")
H
update  
hongming 已提交
92

H
update  
hongming 已提交
93
	conf, err := h.options.OAuthOptions.OAuthClient(clientId)
H
update  
hongming 已提交
94 95 96 97 98 99 100
	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}

	if responseType != "token" {
Z
zryfish 已提交
101
		err := apierrors.NewBadRequest(fmt.Sprintf("Response type %s is not supported", responseType))
H
update  
hongming 已提交
102 103 104 105 106 107 108 109 110 111
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}

	if !ok {
		err := apierrors.NewUnauthorized("Unauthorized")
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}

Z
zryfish 已提交
112
	token, err := h.tokenOperator.IssueTo(authenticated)
113 114 115 116 117 118 119
	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}

	redirectURL, err := conf.ResolveRedirectURL(redirectURI)
H
update  
hongming 已提交
120 121 122 123 124 125 126

	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}

H
hongming 已提交
127
	redirectURL = fmt.Sprintf("%s#access_token=%s&token_type=Bearer", redirectURL, token.AccessToken)
128

H
hongming 已提交
129 130
	if token.ExpiresIn > 0 {
		redirectURL = fmt.Sprintf("%s&expires_in=%v", redirectURL, token.ExpiresIn)
H
update  
hongming 已提交
131
	}
132
	resp.Header().Set("Content-Type", "text/plain")
H
update  
hongming 已提交
133 134
	http.Redirect(resp, req.Request, redirectURL, http.StatusFound)
}
135

136
func (h *handler) oAuthCallBack(req *restful.Request, resp *restful.Response) {
137 138 139 140 141 142 143 144 145

	code := req.QueryParameter("code")
	name := req.PathParameter("callback")

	if code == "" {
		err := apierrors.NewUnauthorized("Unauthorized: missing code")
		resp.WriteError(http.StatusUnauthorized, err)
	}

H
hongming 已提交
146
	providerOptions, err := h.options.OAuthOptions.IdentityProviderOptions(name)
147 148 149 150 151 152

	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		resp.WriteError(http.StatusUnauthorized, err)
	}

H
hongming 已提交
153
	oauthIdentityProvider, err := identityprovider.GetOAuthProvider(providerOptions.Type, providerOptions.Provider)
154 155 156 157

	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		resp.WriteError(http.StatusUnauthorized, err)
H
hongming 已提交
158
		return
159 160
	}

Z
zryfish 已提交
161
	identity, err := oauthIdentityProvider.IdentityExchange(code)
162 163

	if err != nil {
164
		err = apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
165
		resp.WriteError(http.StatusUnauthorized, err)
H
hongming 已提交
166 167 168
		return
	}

Z
zryfish 已提交
169
	authenticated, err := h.im.DescribeUser(identity.GetName())
H
hongming 已提交
170 171
	if err != nil {
		// create user if not exist
Z
zryfish 已提交
172 173 174
		if (oauth.MappingMethodAuto == providerOptions.MappingMethod ||
			oauth.MappingMethodMixed == providerOptions.MappingMethod) &&
			apierrors.IsNotFound(err) {
H
hongming 已提交
175
			create := &iamv1alpha2.User{
Z
zryfish 已提交
176
				ObjectMeta: v1.ObjectMeta{Name: identity.GetName(),
H
hongming 已提交
177
					Annotations: map[string]string{iamv1alpha2.IdentifyProviderLabel: providerOptions.Name}},
Z
zryfish 已提交
178
				Spec: iamv1alpha2.UserSpec{Email: identity.GetEmail()},
H
hongming 已提交
179
			}
Z
zryfish 已提交
180
			if authenticated, err = h.im.CreateUser(create); err != nil {
H
hongming 已提交
181 182 183 184 185 186 187 188 189 190 191
				klog.Error(err)
				api.HandleInternalError(resp, req, err)
				return
			}
		} else {
			klog.Error(err)
			api.HandleInternalError(resp, req, err)
			return
		}
	}

Z
zryfish 已提交
192 193 194
	if oauth.MappingMethodLookup == providerOptions.MappingMethod &&
		authenticated == nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("user %s cannot bound to this identify provider", identity.GetName()))
H
hongming 已提交
195 196 197 198 199 200 201
		klog.Error(err)
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}

	// oauth.MappingMethodAuto
	// Fails if a user with that user name is already mapped to another identity.
Z
zryfish 已提交
202 203
	if providerOptions.MappingMethod == oauth.MappingMethodAuto && authenticated.Annotations[iamv1alpha2.IdentifyProviderLabel] != providerOptions.Name {
		err := apierrors.NewUnauthorized(fmt.Sprintf("user %s is already bound to other identify provider", identity.GetName()))
H
hongming 已提交
204 205 206
		klog.Error(err)
		resp.WriteError(http.StatusUnauthorized, err)
		return
207 208
	}

Z
zryfish 已提交
209 210 211 212 213
	result, err := h.tokenOperator.IssueTo(&user.DefaultInfo{
		Name: authenticated.Name,
		UID:  string(authenticated.UID),
	})

H
hongming 已提交
214 215 216 217 218
	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		resp.WriteError(http.StatusUnauthorized, err)
		return
	}
Z
zryfish 已提交
219

220
	if err = h.loginRecorder.RecordLogin(authenticated.Name, iamv1alpha2.OAuth, providerOptions.Name, nil, req.Request); err != nil {
Z
zryfish 已提交
221 222 223 224 225 226
		klog.Error(err)
		err := apierrors.NewInternalError(err)
		resp.WriteError(http.StatusInternalServerError, err)
		return
	}

H
hongming 已提交
227 228 229
	resp.WriteEntity(result)
}

Z
zryfish 已提交
230
func (h *handler) Login(request *restful.Request, response *restful.Response) {
H
hongming 已提交
231 232 233 234 235 236
	var loginRequest auth.LoginRequest
	err := request.ReadEntity(&loginRequest)
	if err != nil || loginRequest.Username == "" || loginRequest.Password == "" {
		response.WriteHeaderAndEntity(http.StatusUnauthorized, fmt.Errorf("empty username or password"))
		return
	}
Z
zryfish 已提交
237 238
	h.passwordGrant(loginRequest.Username, loginRequest.Password, request, response)
}
H
hongming 已提交
239

Z
zryfish 已提交
240 241
func (h *handler) Token(req *restful.Request, response *restful.Response) {
	grantType, err := req.BodyParameter("grant_type")
H
hongming 已提交
242
	if err != nil {
Z
zryfish 已提交
243 244
		klog.Error(err)
		api.HandleBadRequest(response, req, err)
H
hongming 已提交
245 246
		return
	}
Z
zryfish 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	switch grantType {
	case "password":
		username, err := req.BodyParameter("username")
		if err != nil {
			klog.Error(err)
			api.HandleBadRequest(response, req, err)
			return
		}
		password, err := req.BodyParameter("password")
		if err != nil {
			klog.Error(err)
			api.HandleBadRequest(response, req, err)
			return
		}
		h.passwordGrant(username, password, req, response)
		break
	case "refresh_token":
		h.refreshTokenGrant(req, response)
		break
	default:
		err := apierrors.NewBadRequest(fmt.Sprintf("Grant type %s is not supported", grantType))
		response.WriteError(http.StatusBadRequest, err)
	}
}
H
hongming 已提交
271

Z
zryfish 已提交
272 273
func (h *handler) passwordGrant(username string, password string, req *restful.Request, response *restful.Response) {
	authenticated, err := h.authenticator.Authenticate(username, password)
H
hongming 已提交
274
	if err != nil {
275 276 277
		klog.Error(err)
		switch err {
		case im.AuthFailedIncorrectPassword:
278
			if err := h.loginRecorder.RecordLogin(username, iamv1alpha2.Token, "", err, req.Request); err != nil {
Z
zryfish 已提交
279
				klog.Error(err)
280
				response.WriteError(http.StatusInternalServerError, apierrors.NewInternalError(err))
H
hongming 已提交
281
				return
Z
zryfish 已提交
282
			}
H
hongming 已提交
283
			response.WriteError(http.StatusUnauthorized, apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err)))
Z
zryfish 已提交
284
			return
285 286 287 288 289 290 291 292
		case im.AuthFailedIdentityMappingNotMatch:
			response.WriteError(http.StatusUnauthorized, apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err)))
			return
		case im.AuthRateLimitExceeded:
			response.WriteError(http.StatusTooManyRequests, apierrors.NewTooManyRequests(fmt.Sprintf("Unauthorized: %s", err), 60))
			return
		default:
			response.WriteError(http.StatusInternalServerError, apierrors.NewInternalError(err))
Z
zryfish 已提交
293
		}
H
hongming 已提交
294 295
	}

Z
zryfish 已提交
296 297 298
	result, err := h.tokenOperator.IssueTo(authenticated)
	if err != nil {
		klog.Error(err)
299
		response.WriteError(http.StatusInternalServerError, apierrors.NewInternalError(err))
Z
zryfish 已提交
300 301 302
		return
	}

303
	if err = h.loginRecorder.RecordLogin(authenticated.GetName(), iamv1alpha2.Token, "", nil, req.Request); err != nil {
Z
zryfish 已提交
304
		klog.Error(err)
305
		response.WriteError(http.StatusInternalServerError, apierrors.NewInternalError(err))
Z
zryfish 已提交
306 307
		return
	}
308

Z
zryfish 已提交
309 310
	response.WriteEntity(result)
}
311

Z
zryfish 已提交
312 313
func (h *handler) refreshTokenGrant(req *restful.Request, response *restful.Response) {
	refreshToken, err := req.BodyParameter("refresh_token")
314
	if err != nil {
H
hongming 已提交
315
		klog.Error(err)
Z
zryfish 已提交
316 317
		api.HandleBadRequest(response, req, err)
		return
318 319
	}

Z
zryfish 已提交
320 321 322 323 324
	authenticated, err := h.tokenOperator.Verify(refreshToken)
	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		response.WriteError(http.StatusUnauthorized, err)
		return
325 326
	}

Z
zryfish 已提交
327 328 329 330 331 332 333 334
	result, err := h.tokenOperator.IssueTo(authenticated)
	if err != nil {
		err := apierrors.NewUnauthorized(fmt.Sprintf("Unauthorized: %s", err))
		response.WriteError(http.StatusUnauthorized, err)
		return
	}

	response.WriteEntity(result)
335
}