|
| 1 | +// Copyright 2017 The Kubernetes Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package login |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "encoding/json" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/golang-jwt/jwt/v4" |
| 23 | + |
| 24 | + "k8s.io/dashboard/client" |
| 25 | + "k8s.io/dashboard/errors" |
| 26 | + "k8s.io/dashboard/types" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + tokenServiceAccountKey = "serviceaccount" |
| 31 | +) |
| 32 | + |
| 33 | +type ServiceAccount struct { |
| 34 | + Name string `json:"name"` |
| 35 | + UID string `json:"uid"` |
| 36 | +} |
| 37 | + |
| 38 | +func me(request *http.Request) (*types.User, int, error) { |
| 39 | + k8sClient, err := client.Client(request) |
| 40 | + if err != nil { |
| 41 | + return nil, http.StatusInternalServerError, err |
| 42 | + } |
| 43 | + |
| 44 | + // Make sure that authorization token is valid |
| 45 | + if _, err = k8sClient.Discovery().ServerVersion(); err != nil { |
| 46 | + code, err := errors.HandleError(err) |
| 47 | + return nil, code, err |
| 48 | + } |
| 49 | + |
| 50 | + return getUserFromToken(client.GetBearerToken(request)), http.StatusOK, nil |
| 51 | +} |
| 52 | + |
| 53 | +func getUserFromToken(token string) *types.User { |
| 54 | + parsed, _ := jwt.Parse(token, nil) |
| 55 | + if parsed == nil { |
| 56 | + return &types.User{Authenticated: true} |
| 57 | + } |
| 58 | + |
| 59 | + claims := parsed.Claims.(jwt.MapClaims) |
| 60 | + |
| 61 | + found, value := traverse(tokenServiceAccountKey, claims) |
| 62 | + if !found { |
| 63 | + return &types.User{Authenticated: true} |
| 64 | + } |
| 65 | + |
| 66 | + var sa ServiceAccount |
| 67 | + ok := transcode(value, &sa) |
| 68 | + if !ok { |
| 69 | + return &types.User{Authenticated: true} |
| 70 | + } |
| 71 | + |
| 72 | + return &types.User{Name: sa.Name, Authenticated: true} |
| 73 | +} |
| 74 | + |
| 75 | +func traverse(key string, m map[string]interface{}) (found bool, value interface{}) { |
| 76 | + for k, v := range m { |
| 77 | + if k == key { |
| 78 | + return true, v |
| 79 | + } |
| 80 | + |
| 81 | + if innerMap, ok := v.(map[string]interface{}); ok { |
| 82 | + return traverse(key, innerMap) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return false, "" |
| 87 | +} |
| 88 | + |
| 89 | +func transcode(in, out interface{}) bool { |
| 90 | + buf := new(bytes.Buffer) |
| 91 | + err := json.NewEncoder(buf).Encode(in) |
| 92 | + if err != nil { |
| 93 | + return false |
| 94 | + } |
| 95 | + |
| 96 | + err = json.NewDecoder(buf).Decode(out) |
| 97 | + return err == nil |
| 98 | +} |
0 commit comments