37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
user.go 1.23 KB
一键复制 编辑 原始数据 按行查看 历史
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)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.11-rc1

搜索帮助