Skip to content

Commit

Permalink
feat: Add scopes option to client
Browse files Browse the repository at this point in the history
  • Loading branch information
alldoami committed Dec 12, 2024
1 parent 03a70fa commit bc35216
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions oidc_cli/oidc_impl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Config struct {
}

// NewClient returns a new client
func NewClient(ctx context.Context, config *Config, clientOptions ...Option) (*Client, error) {
func NewClient(ctx context.Context, config *Config, scopes []string, clientOptions ...Option) (*Client, error) {
provider, err := oidc.NewProvider(ctx, config.IssuerURL)
if err != nil {
return nil, errors.Wrap(err, "could not create oidc provider")
Expand All @@ -47,16 +47,20 @@ func NewClient(ctx context.Context, config *Config, clientOptions ...Option) (*C
return nil, err
}

oauthConfig := &oauth2.Config{
ClientID: config.ClientID,
RedirectURL: fmt.Sprintf("http://localhost:%d", server.GetBoundPort()),
Endpoint: provider.Endpoint(),
Scopes: []string{
if len(scopes) == 0 {
scopes = []string{
oidc.ScopeOpenID,
oidc.ScopeOfflineAccess,
"email",
"groups",
},
}
}

oauthConfig := &oauth2.Config{
ClientID: config.ClientID,
RedirectURL: fmt.Sprintf("http://localhost:%d", server.GetBoundPort()),
Endpoint: provider.Endpoint(),
Scopes: scopes,
}

oidcConfig := &oidc.Config{
Expand Down
4 changes: 2 additions & 2 deletions oidc_cli/oidc_impl/token_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

// GetToken gets an oidc token.
// It handles caching with a default cache and keyring storage.
func GetToken(ctx context.Context, clientID string, issuerURL string, clientOptions ...client.Option) (*client.Token, error) {
func GetToken(ctx context.Context, clientID string, issuerURL string, scopes []string, clientOptions ...client.Option) (*client.Token, error) {
fileLock, err := pidlock.NewLock(lockFilePath)
if err != nil {
return nil, errors.Wrap(err, "unable to create lock")
Expand All @@ -34,7 +34,7 @@ func GetToken(ctx context.Context, clientID string, issuerURL string, clientOpti
},
}

c, err := client.NewClient(ctx, conf, clientOptions...)
c, err := client.NewClient(ctx, conf, scopes, clientOptions...)
if err != nil {
return nil, errors.Wrap(err, "Unable to create client")
}
Expand Down

0 comments on commit bc35216

Please sign in to comment.