auth.go 3.4 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 24
	"kubesphere.io/kubesphere/pkg/utils/iputil"
	"kubesphere.io/kubesphere/pkg/utils/jwtutil"
H
hongming 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	"net/http"

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

type Spec struct {
	Token string `json:"token"`
}

type Status struct {
	Authenticated bool                   `json:"authenticated"`
	User          map[string]interface{} `json:"user,omitempty"`
}

type TokenReview struct {
	APIVersion string  `json:"apiVersion"`
	Kind       string  `json:"kind"`
	Spec       *Spec   `json:"spec,omitempty"`
	Status     *Status `json:"status,omitempty"`
}

type LoginRequest struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

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

func LoginHandler(req *restful.Request, resp *restful.Response) {
	var loginRequest LoginRequest

	err := req.ReadEntity(&loginRequest)

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

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

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

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

H
hongming 已提交
76
	resp.WriteAsJson(token)
H
hongming 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
}

// 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 已提交
91
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.New("token must not be null"))
H
hongming 已提交
92 93 94 95 96
		return
	}

	uToken := tokenReview.Spec.Token

H
hongming 已提交
97
	token, err := jwtutil.ValidateToken(uToken)
H
hongming 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111

	if err != nil {
		failed := TokenReview{APIVersion: APIVersion,
			Kind: KindTokenReview,
			Status: &Status{
				Authenticated: false,
			},
		}
		resp.WriteAsJson(failed)
		return
	}

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

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

H
hongming 已提交
114 115 116 117 118 119
	if !ok {
		resp.WriteHeaderAndEntity(http.StatusBadRequest, errors.New("username not found"))
		return
	}

	user, err := iam.GetUserInfo(username)
H
hongming 已提交
120 121 122 123 124 125

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

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

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

H
hongming 已提交
133 134
	user.Groups = groups

H
hongming 已提交
135 136 137 138 139 140 141 142 143 144 145
	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
}