Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: attempt at making the oidc_impl package more general #874

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions oidc_cli/oidc_impl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (

// Client is an oauth client
type Client struct {
provider *oidc.Provider
oauthConfig *oauth2.Config
verifier *oidc.IDTokenVerifier
server *server
provider *oidc.Provider
oauthConfig *oauth2.Config
verifier *oidc.IDTokenVerifier
server *server
DisableCache bool

// Extra configuration options
customMessages map[oidcStatus]string
Expand Down Expand Up @@ -53,9 +54,7 @@ func NewClient(ctx context.Context, config *Config, clientOptions ...Option) (*C
Endpoint: provider.Endpoint(),
Scopes: []string{
oidc.ScopeOpenID,
oidc.ScopeOfflineAccess,
"email",
"groups",
},
}

Expand Down Expand Up @@ -223,7 +222,7 @@ func (c *Client) Authenticate(ctx context.Context) (*Token, error) {
}

c.server.Start(ctx, c, oauthMaterial)
fmt.Fprintf(os.Stderr, "Opening browser in order to authenticate with Okta, hold on a brief second...\n")
fmt.Fprintf(os.Stderr, "Opening browser in order to authenticate with your Identity Provider, hold on a brief second...\n")
time.Sleep(2 * time.Second)

// intercept these outputs, send them back on error
Expand Down
6 changes: 6 additions & 0 deletions oidc_cli/oidc_impl/client/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ var SetOauth2AuthStyle = func(authStyle oauth2.AuthStyle) Option {
c.oauthConfig.Endpoint.AuthStyle = authStyle
}
}

var DisableTokenCache = func() Option {
return func(c *Client) {
c.DisableCache = true
}
}
2 changes: 1 addition & 1 deletion oidc_cli/oidc_impl/storage/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (f *File) Set(ctx context.Context, value string) error {
return errors.Wrapf(err, "could not create cache dir %s", f.dir)
}

err = ioutil.WriteFile(f.key, []byte(value), 0600)
err = os.WriteFile(f.key, []byte(value), 0600)
return errors.Wrap(err, "could not set value to file")
}

Expand Down
32 changes: 21 additions & 11 deletions oidc_cli/oidc_impl/token_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,29 @@ func GetToken(ctx context.Context, clientID string, issuerURL string, clientOpti
return nil, errors.Wrap(err, "Unable to create client")
}

storage, err := storage.GetOIDC(clientID, issuerURL)
if err != nil {
return nil, err
}
var token *client.Token

cache := cache.NewCache(storage, c.RefreshToken, fileLock)
if c.DisableCache {
token, err = c.Authenticate(ctx)
if err != nil {
return nil, errors.Wrap(err, "Unable to authenticate with OAuth client")
}
} else {
storage, err := storage.GetOIDC(clientID, issuerURL)
if err != nil {
return nil, err
}

token, err := cache.Read(ctx)
if err != nil {
return nil, errors.Wrap(err, "Unable to extract token from client")
}
if token == nil {
return nil, errors.New("nil token from OIDC-IDP")
cache := cache.NewCache(storage, c.RefreshToken, fileLock)

token, err := cache.Read(ctx)
if err != nil {
return nil, errors.Wrap(err, "Unable to extract token from client")
}
if token == nil {
return nil, errors.New("nil token from OIDC-IDP")
}
}

return token, nil
}
Loading