/
oidc.go
70 lines (59 loc) · 1.58 KB
/
oidc.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
package config
import (
"errors"
"k8s.io/client-go/tools/clientcmd/api"
)
type Oidc struct {
ClientId string
Token string
IdpUrl string
RefreshToken string
}
func LoadOidcConfig(cmdline *CmdLine, k8sConfig *api.Config) (*OidcRequest, string, error) {
if cmdline.ClientId != "" && cmdline.IdpUrl != "" {
return &OidcRequest{
ClientId: cmdline.ClientId,
IdpUrl: cmdline.IdpUrl,
}, cmdline.NewUsername, nil
}
contextName := cmdline.ContextName
if contextName == "" {
if k8sConfig.CurrentContext == "" {
return nil, "", errors.New("context is not specified")
}
contextName = k8sConfig.CurrentContext
}
ctx, ok := k8sConfig.Contexts[contextName]
if !ok {
return nil, "", errors.New("context " + contextName + " not found")
}
user, ok := k8sConfig.AuthInfos[ctx.AuthInfo]
if !ok {
return nil, "", errors.New("user " + ctx.AuthInfo + " not found")
}
if user.AuthProvider == nil || user.AuthProvider.Name != "oidc" {
return nil, "", errors.New("user " + ctx.AuthInfo + " was found but auth provider is not oidc")
}
oidcConf, err := newOidcRequest(user.AuthProvider.Config)
if err != nil {
return nil, "", err
}
newUsername := cmdline.NewUsername
if newUsername == "" {
newUsername = ctx.AuthInfo
}
return oidcConf, newUsername, nil
}
func (o *Oidc) ToAuthInfo() *api.AuthInfo {
return &api.AuthInfo{
AuthProvider: &api.AuthProviderConfig{
Name: "oidc",
Config: map[string]string{
"idp-issuer-url": o.IdpUrl,
"client-id": o.ClientId,
"id-token": o.Token,
"refresh-token": o.RefreshToken,
},
},
}
}