-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinfo.go
52 lines (44 loc) · 1.39 KB
/
userinfo.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
package auth
import (
"context"
v1 "github.com/llm-operator/rbac-manager/api/v1"
)
type userInfoKey struct{}
// AssignedKubernetesEnv represents the assigned Kubernetes environment.
type AssignedKubernetesEnv struct {
ClusterID string
Namespace string
}
// UserInfo manages the user info.
type UserInfo struct {
UserID string
OrganizationID string
ProjectID string
AssignedKubernetesEnvs []AssignedKubernetesEnv
TenantID string
}
// AppendUserInfoToContext appends the user info to the context.
func AppendUserInfoToContext(ctx context.Context, info UserInfo) context.Context {
return context.WithValue(ctx, userInfoKey{}, &info)
}
// ExtractUserInfoFromContext extracts the user info from the context.
func ExtractUserInfoFromContext(ctx context.Context) (*UserInfo, bool) {
info, ok := ctx.Value(userInfoKey{}).(*UserInfo)
return info, ok
}
func newUserInfoFromAuthorizeResponse(resp *v1.AuthorizeResponse) UserInfo {
var envs []AssignedKubernetesEnv
for _, env := range resp.Project.AssignedKubernetesEnvs {
envs = append(envs, AssignedKubernetesEnv{
ClusterID: env.ClusterId,
Namespace: env.Namespace,
})
}
return UserInfo{
UserID: resp.User.Id,
OrganizationID: resp.Organization.Id,
ProjectID: resp.Project.Id,
AssignedKubernetesEnvs: envs,
TenantID: resp.TenantId,
}
}