-
Notifications
You must be signed in to change notification settings - Fork 90
/
client.go
63 lines (52 loc) · 1.74 KB
/
client.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
package identity
import (
"context"
oidc "github.com/coreos/go-oidc"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/identity/client"
"golang.org/x/oauth2"
"k8s.io/client-go/kubernetes"
)
func GetKotsadmOIDCProvider(ctx context.Context, clientset kubernetes.Interface, namespace string) (*oidc.Provider, error) {
dexConfig, err := getKotsadmDexConfig(ctx, clientset, namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to get kotsadm dex config")
}
if dexConfig == nil {
return nil, errors.New("dex config not found")
}
identityConfig, err := GetConfig(ctx, namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to get identity config")
}
httpClient, err := client.HTTPClient(ctx, namespace, *identityConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to init http client")
}
oidcClientCtx := oidc.ClientContext(ctx, httpClient)
provider, err := oidc.NewProvider(oidcClientCtx, dexConfig.Issuer)
if err != nil {
return nil, errors.Wrapf(err, "failed to query provider %q", dexConfig.Issuer)
}
return provider, nil
}
func GetKotsadmOAuth2Config(ctx context.Context, clientset kubernetes.Interface, namespace string, provider oidc.Provider) (*oauth2.Config, error) {
client, err := getOIDCClient(ctx, clientset, namespace)
if err != nil {
return nil, errors.Wrap(err, "failed to get oidc client secret")
}
if client == nil {
return nil, errors.Wrap(err, "oidc client not found")
}
oauth2Config := oauth2.Config{
ClientID: client.ID,
ClientSecret: client.Secret,
Endpoint: provider.Endpoint(),
Scopes: getScopes(),
RedirectURL: client.RedirectURIs[0],
}
return &oauth2Config, nil
}
func getScopes() []string {
return []string{"openid", "email", "groups"}
}