-
Notifications
You must be signed in to change notification settings - Fork 55
/
user.go
115 lines (95 loc) · 2.91 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright © 2021 - 2023 SUSE LLC
// 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 auth collects structures and functions around the
// generation and processing of credentials.
package auth
import (
"strings"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/epinio/epinio/helpers/kubernetes"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
)
// User is a struct containing all the information of an Epinio User
type User struct {
Username string
Password string
CreatedAt time.Time
Role string
Namespaces []string
secretName string
}
// NewUserFromSecret create an Epinio User from a Secret
func NewUserFromSecret(secret corev1.Secret) User {
user := User{
Username: string(secret.Data["username"]),
Password: string(secret.Data["password"]),
CreatedAt: secret.ObjectMeta.CreationTimestamp.Time,
Role: secret.Labels[kubernetes.EpinioAPISecretRoleLabelKey],
Namespaces: []string{},
secretName: secret.GetName(),
}
if ns, found := secret.Data["namespaces"]; found {
namespaces := strings.TrimSpace(string(ns))
for _, namespace := range strings.Split(namespaces, "\n") {
namespace = strings.TrimSpace(namespace)
if namespace != "" {
user.Namespaces = append(user.Namespaces, namespace)
}
}
}
return user
}
// NewUserFromIDToken create an Epinio User from an IDToken
func NewUserFromIDToken(idToken *oidc.IDToken) (User, error) {
user := User{}
var claims struct {
Email string `json:"email"`
Groups []string `json:"groups"`
}
if err := idToken.Claims(&claims); err != nil {
return user, errors.Wrap(err, "parsing claims")
}
user = User{
Username: claims.Email,
Role: "user",
Namespaces: []string{},
}
return user, nil
}
// AddNamespace adds the namespace to the User's namespaces, if not already exists
func (u *User) AddNamespace(namespace string) {
if namespace == "" {
return
}
for _, ns := range u.Namespaces {
if ns == namespace {
return
}
}
u.Namespaces = append(u.Namespaces, namespace)
}
// RemoveNamespace removes a namespace from the User's namespaces.
// It returns false if the namespace was not there
func (u *User) RemoveNamespace(namespace string) bool {
updatedNamespaces := []string{}
removed := false
for _, ns := range u.Namespaces {
if ns != namespace {
updatedNamespaces = append(updatedNamespaces, ns)
} else {
removed = true
}
}
u.Namespaces = updatedNamespaces
return removed
}