forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
51 lines (44 loc) · 1.23 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
package common
import (
"fmt"
"time"
clusterv3 "github.com/rancher/types/apis/cluster.cattle.io/v3"
managementv3 "github.com/rancher/types/apis/management.cattle.io/v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func NewClusterAuthToken(token *managementv3.Token) (*clusterv3.ClusterAuthToken, error) {
hash, err := CreateHash(token.Token)
if err != nil {
return nil, err
}
tokenEnabled := token.Enabled == nil || *token.Enabled
result := &clusterv3.ClusterAuthToken{
ObjectMeta: metav1.ObjectMeta{
Name: token.ObjectMeta.Name,
},
TypeMeta: metav1.TypeMeta{
Kind: "ClusterAuthToken",
},
UserName: token.UserID,
SecretKeyHash: hash,
ExpiresAt: token.ExpiresAt,
Enabled: tokenEnabled,
}
return result, nil
}
func VerifyClusterAuthToken(secretKey string, clusterAuthToken *clusterv3.ClusterAuthToken) error {
if !clusterAuthToken.Enabled {
return fmt.Errorf("token is not enabled")
}
expiresAt := clusterAuthToken.ExpiresAt
if expiresAt != "" {
expires, err := time.Parse(time.RFC3339, expiresAt)
if err != nil {
return err
}
if expires.Before(time.Now()) {
return fmt.Errorf("auth expired at %s", expiresAt)
}
}
return VerifyHash(clusterAuthToken.SecretKeyHash, secretKey)
}