blob: 4f60d522f7602a7c475215075c99046548473cdd [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package group
18
19import (
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
25type 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
33func NewTokenGroupAdder(auth authenticator.Token, groups []string) authenticator.Token {
34 return &TokenGroupAdder{auth, groups}
35}
36
37func (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}