-
Notifications
You must be signed in to change notification settings - Fork 55
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
DXCDT-293: Access token management for client credentials #537
Merged
willvedd
merged 11 commits into
main
from
DXCDT-293-store-access-token-client-credentials
Dec 2, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c5027b6
Storing and refreshing access token for client credentials
willvedd 5dd0688
Removing unnecessary comment
willvedd 972c191
Removing tenant name from being stored, removing flag declarations
willvedd 0b57340
Removing tenant name from being stored
willvedd 2247df1
Fixing erroneous delete
willvedd 6219b13
Simplifying ExpiresAt assignment
willvedd 1a0d1b2
Remove duplicate addTenant in tenants add command
sergiught 9fa07db
Remove setting scopes on tenant when using client credentials
sergiught cd306af
Refactor how we check for token expiration while preparing the tenant
sergiught 5abe26a
Refactor cli.prepareTenant func
sergiught 4e61126
Refactor cli.setup func
sergiught File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
"time" | ||
|
||
"github.com/joeshaw/envdecode" | ||
"golang.org/x/oauth2/clientcredentials" | ||
) | ||
|
||
const ( | ||
|
@@ -65,7 +66,7 @@ type Result struct { | |
Domain string | ||
RefreshToken string | ||
AccessToken string | ||
ExpiresIn int64 | ||
ExpiresAt time.Time | ||
} | ||
|
||
type State struct { | ||
|
@@ -170,9 +171,11 @@ func (a *Authenticator) Wait(ctx context.Context, state State) (Result, error) { | |
return Result{ | ||
RefreshToken: res.RefreshToken, | ||
AccessToken: res.AccessToken, | ||
ExpiresIn: res.ExpiresIn, | ||
Tenant: ten, | ||
Domain: domain, | ||
ExpiresAt: time.Now().Add( | ||
time.Duration(res.ExpiresIn) * time.Second, | ||
), | ||
Tenant: ten, | ||
Domain: domain, | ||
}, nil | ||
} | ||
} | ||
|
@@ -249,3 +252,39 @@ func parseTenant(accessToken string) (tenant, domain string, err error) { | |
} | ||
return "", "", fmt.Errorf("audience not found for %s", audiencePath) | ||
} | ||
|
||
// ClientCredentials encapsulates all data to facilitate access token creation with client credentials (client ID and client secret) | ||
type ClientCredentials struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would've used the |
||
ClientID string | ||
ClientSecret string | ||
Domain string | ||
} | ||
|
||
// GetAccessTokenFromClientCreds generates an access token from client credentials | ||
func GetAccessTokenFromClientCreds(args ClientCredentials) (Result, error) { | ||
u, err := url.Parse("https://" + args.Domain) | ||
if err != nil { | ||
return Result{}, err | ||
} | ||
|
||
credsConfig := &clientcredentials.Config{ | ||
ClientID: args.ClientID, | ||
ClientSecret: args.ClientSecret, | ||
TokenURL: u.String() + "/oauth/token", | ||
EndpointParams: url.Values{ | ||
"client_id": {args.ClientID}, | ||
"scope": {strings.Join(RequiredScopesMin(), " ")}, | ||
"audience": {u.String() + "/api/v2/"}, | ||
}, | ||
} | ||
|
||
resp, err := credsConfig.Token(context.Background()) | ||
if err != nil { | ||
return Result{}, err | ||
} | ||
|
||
return Result{ | ||
AccessToken: resp.AccessToken, | ||
ExpiresAt: resp.Expiry, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making this change enabled me to reuse the
Result
type for client credential access tokens. The associated arithmetic gets slightly shuffled as a result.