Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package group |
| 18 | |
| 19 | import ( |
| 20 | "k8s.io/apiserver/pkg/authentication/authenticator" |
| 21 | "k8s.io/apiserver/pkg/authentication/user" |
| 22 | ) |
| 23 | |
| 24 | // TokenGroupAdder adds groups to an authenticated user.Info |
| 25 | type TokenGroupAdder struct { |
| 26 | // Authenticator is delegated to make the authentication decision |
| 27 | Authenticator authenticator.Token |
| 28 | // Groups are additional groups to add to the user.Info from a successful authentication |
| 29 | Groups []string |
| 30 | } |
| 31 | |
| 32 | // NewTokenGroupAdder wraps a token authenticator, and adds the specified groups to the returned user when authentication succeeds |
| 33 | func NewTokenGroupAdder(auth authenticator.Token, groups []string) authenticator.Token { |
| 34 | return &TokenGroupAdder{auth, groups} |
| 35 | } |
| 36 | |
| 37 | func (g *TokenGroupAdder) AuthenticateToken(token string) (user.Info, bool, error) { |
| 38 | u, ok, err := g.Authenticator.AuthenticateToken(token) |
| 39 | if err != nil || !ok { |
| 40 | return nil, ok, err |
| 41 | } |
| 42 | return &user.DefaultInfo{ |
| 43 | Name: u.GetName(), |
| 44 | UID: u.GetUID(), |
| 45 | Groups: append(u.GetGroups(), g.Groups...), |
| 46 | Extra: u.GetExtra(), |
| 47 | }, true, nil |
| 48 | } |