-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathprovider.go
More file actions
85 lines (70 loc) · 3.26 KB
/
Copy pathprovider.go
File metadata and controls
85 lines (70 loc) · 3.26 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package auth
import (
"context"
"github.com/opentdf/platform/otdfctl/pkg/profiles"
"github.com/opentdf/platform/sdk"
"golang.org/x/oauth2"
)
// Provider builds SDK authentication for a single auth type (keyed by
// AuthCredentials.AuthType). Extending projects implement it and register it via
// RegisterProvider to add a custom authentication process.
type Provider interface {
// SDKAuthOption returns the sdk.Option used to authenticate the SDK client.
SDKAuthOption(profile *profiles.OtdfctlProfileStore) (sdk.Option, error)
// Validate reports whether the profile's stored credentials are present and usable.
Validate(ctx context.Context, profile *profiles.OtdfctlProfileStore) error
// GetToken returns an OAuth2 token for the profile.
GetToken(ctx context.Context, profile *profiles.OtdfctlProfileStore) (*oauth2.Token, error)
}
// authProviders maps an auth type to its provider. Registration is init-time and
// single-threaded, so no locking is required.
var authProviders = map[string]Provider{}
// RegisterProvider registers (or replaces) the provider for an auth type.
// Extending projects call this from init().
func RegisterProvider(authType string, provider Provider) {
authProviders[authType] = provider
}
// lookupProvider returns the provider for authType, or ErrInvalidAuthType if none.
func lookupProvider(authType string) (Provider, error) {
provider, ok := authProviders[authType]
if !ok {
return nil, ErrInvalidAuthType
}
return provider, nil
}
func init() {
RegisterProvider(profiles.AuthTypeClientCredentials, clientCredentialsProvider{})
RegisterProvider(profiles.AuthTypeAccessToken, accessTokenProvider{})
}
// clientCredentialsProvider implements the built-in OAuth2 client-credentials flow.
type clientCredentialsProvider struct{}
func (clientCredentialsProvider) SDKAuthOption(profile *profiles.OtdfctlProfileStore) (sdk.Option, error) {
c := profile.GetAuthCredentials()
return sdk.WithClientCredentials(c.ClientID, c.ClientSecret, NormalizeScopes(c.Scopes)), nil
}
func (clientCredentialsProvider) Validate(ctx context.Context, profile *profiles.OtdfctlProfileStore) error {
c := profile.GetAuthCredentials()
_, err := GetTokenWithClientCreds(ctx, profile.GetEndpoint(), c.ClientID, c.ClientSecret, profile.GetTLSNoVerify(), c.Scopes)
return err
}
func (clientCredentialsProvider) GetToken(ctx context.Context, profile *profiles.OtdfctlProfileStore) (*oauth2.Token, error) {
c := profile.GetAuthCredentials()
return GetTokenWithClientCreds(ctx, profile.GetEndpoint(), c.ClientID, c.ClientSecret, profile.GetTLSNoVerify(), c.Scopes)
}
// accessTokenProvider implements the built-in static access-token flow.
type accessTokenProvider struct{}
func (accessTokenProvider) SDKAuthOption(profile *profiles.OtdfctlProfileStore) (sdk.Option, error) {
c := profile.GetAuthCredentials()
return sdk.WithOAuthAccessTokenSource(oauth2.StaticTokenSource(buildToken(&c))), nil
}
func (accessTokenProvider) Validate(_ context.Context, profile *profiles.OtdfctlProfileStore) error {
c := profile.GetAuthCredentials()
if !buildToken(&c).Valid() {
return ErrAccessTokenExpired
}
return nil
}
func (accessTokenProvider) GetToken(_ context.Context, profile *profiles.OtdfctlProfileStore) (*oauth2.Token, error) {
c := profile.GetAuthCredentials()
return buildToken(&c), nil
}