auth.go 3.8 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*

 Copyright 2019 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.

*/
package iam

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/emicklei/go-restful"
H
hongming 已提交
23
	"github.com/golang/glog"
H
hongming 已提交
24 25
	"kubesphere.io/kubesphere/pkg/utils/iputil"
	"kubesphere.io/kubesphere/pkg/utils/jwtutil"
H
hongming 已提交
26 27 28 29 30 31 32
	"net/http"

	"kubesphere.io/kubesphere/pkg/errors"
	"kubesphere.io/kubesphere/pkg/models/iam"
)

type Spec struct {
H
hongming 已提交
33
	Token string `json:"token" description:"access token"`
H
hongming 已提交
34 35 36
}

type Status struct {
H
hongming 已提交
37 38
	Authenticated bool                   `json:"authenticated" description:"is authenticated"`
	User          map[string]interface{} `json:"user,omitempty" description:"user info"`
H
hongming 已提交
39 40 41
}

type TokenReview struct {
H
hongming 已提交
42 43
	APIVersion string  `json:"apiVersion" description:"Kubernetes API version"`
	Kind       string  `json:"kind" description:"kind of the API object"`
H
hongming 已提交
44
	Spec       *Spec   `json:"spec,omitempty"`
H
hongming 已提交
45
	Status     *Status `json:"status,omitempty" description:"token review status"`
H
hongming 已提交
46 47 48
}

type LoginRequest struct {
H
hongming 已提交
49 50
	Username string `json:"username" description:"username"`
	Password string `json:"password" description:"password"`
H
hongming 已提交
51 52 53 54 55 56 57
}

const (
	APIVersion      = "authentication.k8s.io/v1beta1"
	KindTokenReview = "TokenReview"
)

58
func Login(req *restful.Request, resp *restful.Response) {
H
hongming 已提交
59 60 61 62 63
	var loginRequest LoginRequest

	err := req.ReadEntity(&loginRequest)

	if err != nil || loginRequest.Username == "" || loginRequest.Password == "" {
H
hongming 已提交
64
		resp.WriteHeaderAndEntity(http.StatusUnauthorized, errors.New("incorrect username or password"))
H
hongming 已提交
65 66 67
		return
	}

H
hongming 已提交
68
	ip := iputil.RemoteIp(req.Request)
H
hongming 已提交
69 70 71 72

	token, err := iam.Login(loginRequest.Username, loginRequest.Password, ip)

	if err != nil {
73 74 75 76
		if serviceError, ok := err.(restful.ServiceError); ok {
			resp.WriteHeaderAndEntity(serviceError.Code, errors.New(serviceError.Message))
			return
		}
H
hongming 已提交
77 78 79 80
		resp.WriteHeaderAndEntity(http.StatusUnauthorized, errors.Wrap(err))
		return
	}

H
hongming 已提交
81
	resp.WriteAsJson(token)
H
hongming 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

// k8s token review
func TokenReviewHandler(req *restful.Request, resp *restful.Response) {
	var tokenReview TokenReview

	err := req.ReadEntity(&tokenReview)

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.Wrap(err))
		return
	}

	if tokenReview.Spec == nil {
H
hongming 已提交
96
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.New("token must not be null"))
H
hongming 已提交
97 98 99 100 101
		return
	}

	uToken := tokenReview.Spec.Token

H
hongming 已提交
102
	token, err := jwtutil.ValidateToken(uToken)
H
hongming 已提交
103 104

	if err != nil {
H
hongming 已提交
105
		glog.Errorln("token review failed", uToken, err)
H
hongming 已提交
106 107 108 109 110 111 112 113 114 115 116 117
		failed := TokenReview{APIVersion: APIVersion,
			Kind: KindTokenReview,
			Status: &Status{
				Authenticated: false,
			},
		}
		resp.WriteAsJson(failed)
		return
	}

	claims := token.Claims.(jwt.MapClaims)

H
hongming 已提交
118
	username, ok := claims["username"].(string)
H
hongming 已提交
119

H
hongming 已提交
120 121 122 123 124 125
	if !ok {
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.New("username not found"))
		return
	}

	user, err := iam.GetUserInfo(username)
H
hongming 已提交
126 127 128 129 130 131

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
132
	groups, err := iam.GetUserGroups(username)
H
hongming 已提交
133 134 135 136 137 138

	if err != nil {
		resp.WriteHeaderAndEntity(http.StatusInternalServerError, errors.Wrap(err))
		return
	}

H
hongming 已提交
139 140
	user.Groups = groups

H
hongming 已提交
141 142 143 144 145 146 147 148 149 150 151
	success := TokenReview{APIVersion: APIVersion,
		Kind: KindTokenReview,
		Status: &Status{
			Authenticated: true,
			User:          map[string]interface{}{"username": user.Username, "uid": user.Username, "groups": user.Groups},
		},
	}

	resp.WriteAsJson(success)
	return
}