-
Notifications
You must be signed in to change notification settings - Fork 24
feat(sdk): support oauth2 tokensource with option #1394
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
da84ea3
feat(sdk): support oauth2 tokensource with option
jakedoublev ea55907
Merge branch 'main' into feat/sdk-access-token
jakedoublev 2e7a40f
put back getAccessToken function signature
jakedoublev 907803e
cleanup
jakedoublev 3ce163f
cleanup
jakedoublev ab46fec
cleanup
jakedoublev 89403aa
Merge branch 'main' into feat/sdk-access-token
jakedoublev b99e16d
lint
jakedoublev 5b723d7
go mod tidy
jakedoublev 6b6f1bf
generate dpop key only if needed
jakedoublev 32adfe6
use oidc lib .Valid() check on oauth token
jakedoublev ad0909d
unit tests
jakedoublev afe9154
lint
jakedoublev 3ae1d02
Merge branch 'main' into feat/sdk-access-token
jakedoublev 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| package sdk | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/lestrrat-go/jwx/v2/jwk" | ||
| "github.com/opentdf/platform/lib/ocrypto" | ||
| "github.com/opentdf/platform/sdk/auth" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| // OAuthAccessTokenSource allow connecting to an IDP and obtain a DPoP bound access token | ||
| type OAuthAccessTokenSource struct { | ||
| source oauth2.TokenSource | ||
| scopes []string | ||
| dpopKey jwk.Key | ||
| asymDecryption ocrypto.AsymDecryption | ||
| dpopPEM string | ||
| } | ||
|
|
||
| func NewOAuthAccessTokenSource( | ||
| source oauth2.TokenSource, scopes []string, key *ocrypto.RsaKeyPair, | ||
| ) (*OAuthAccessTokenSource, error) { | ||
| dpopPublicKeyPEM, dpopKey, asymDecryption, err := getNewDPoPKey(key) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| tokenSource := OAuthAccessTokenSource{ | ||
| source: source, | ||
| scopes: scopes, | ||
| asymDecryption: *asymDecryption, | ||
| dpopKey: dpopKey, | ||
| dpopPEM: dpopPublicKeyPEM, | ||
| } | ||
|
|
||
| return &tokenSource, nil | ||
| } | ||
|
|
||
| // AccessToken use a pointer receiver so that the token state is shared | ||
| func (t *OAuthAccessTokenSource) AccessToken(_ context.Context, _ *http.Client) (auth.AccessToken, error) { // must satisfy auth.AccessTokenSource interface | ||
| tok, err := t.source.Token() | ||
| if err != nil { | ||
| return "", fmt.Errorf("error getting access token: %w", err) | ||
| } | ||
|
|
||
| // Non-nil with AccessToken and not Expired | ||
| if !tok.Valid() { | ||
| return "", ErrAccessTokenInvalid | ||
| // TODO: refresh tokens if expired? | ||
| } | ||
|
|
||
| return auth.AccessToken(tok.AccessToken), nil | ||
| } | ||
|
|
||
| func (t *OAuthAccessTokenSource) MakeToken(tokenMaker func(jwk.Key) ([]byte, error)) ([]byte, error) { | ||
| return tokenMaker(t.dpopKey) | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package sdk | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/lestrrat-go/jwx/v2/jwk" | ||
| "github.com/opentdf/platform/lib/ocrypto" | ||
| "github.com/opentdf/platform/sdk/auth" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| func TestNewOAuthAccessTokenSource_Success(t *testing.T) { | ||
| mockToken := "mockToken" | ||
| // Expected | ||
| mockSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: mockToken}) | ||
| mockScopes := []string{"scope1", "scope2"} | ||
| mockKey, _ := ocrypto.NewRSAKeyPair(dpopKeySize) | ||
| dpopPublicKeyPEM, dpopKey, asymDecryption, _ := getNewDPoPKey(&mockKey) | ||
|
|
||
| // Testable | ||
| tokenSource, err := NewOAuthAccessTokenSource(mockSource, mockScopes, &mockKey) | ||
|
|
||
| // Sanity Checks | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, tokenSource) | ||
| assert.Equal(t, mockSource, tokenSource.source) | ||
| assert.Equal(t, mockScopes, tokenSource.scopes) | ||
| // DPoP values | ||
| assert.Equal(t, asymDecryption, &tokenSource.asymDecryption) | ||
| assert.Equal(t, dpopPublicKeyPEM, tokenSource.dpopPEM) | ||
| assert.Equal(t, dpopKey, tokenSource.dpopKey) | ||
| // Interface checks | ||
| tok, err := tokenSource.AccessToken(context.Background(), nil) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tok, auth.AccessToken(mockToken)) | ||
| made, err := tokenSource.MakeToken(func(jwk.Key) ([]byte, error) { return []byte(mockToken), nil }) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, made, []byte(mockToken)) | ||
| } | ||
|
|
||
| func TestNewOAuthAccessTokenSource_ExpiredToken(t *testing.T) { | ||
| // Expected | ||
| pastTime := time.Now().Add(-time.Hour) | ||
| mockSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "mockToken", Expiry: pastTime}) | ||
| mockScopes := []string{"scope1"} | ||
| mockKey, _ := ocrypto.NewRSAKeyPair(dpopKeySize) | ||
|
|
||
| // Testable | ||
| tokenSource, err := NewOAuthAccessTokenSource(mockSource, mockScopes, &mockKey) | ||
|
|
||
| // Sanity Checks | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, tokenSource) | ||
| assert.Equal(t, mockSource, tokenSource.source) | ||
| // Interface checks | ||
| tok, err := tokenSource.AccessToken(context.Background(), nil) | ||
| require.Error(t, err) | ||
| require.ErrorIs(t, err, ErrAccessTokenInvalid) | ||
| assert.Empty(t, tok) | ||
| } | ||
|
|
||
| func TestNewOAuthAccessTokenSource_InvalidTokenSource(t *testing.T) { | ||
| // Expected | ||
| mockSource := oauth2.StaticTokenSource(&oauth2.Token{}) | ||
| mockScopes := []string{"scope1"} | ||
| mockKey, _ := ocrypto.NewRSAKeyPair(dpopKeySize) | ||
|
|
||
| // Testable | ||
| tokenSource, err := NewOAuthAccessTokenSource(mockSource, mockScopes, &mockKey) | ||
|
|
||
| // Sanity Checks | ||
| require.NoError(t, err) | ||
| assert.NotNil(t, tokenSource) | ||
| assert.Equal(t, mockSource, tokenSource.source) | ||
| // Interface checks | ||
| tok, err := tokenSource.AccessToken(context.Background(), nil) | ||
| require.Error(t, err) | ||
| assert.Empty(t, tok) | ||
| } | ||
|
|
||
| func TestNewOAuthAccessTokenSource_InvalidKey(t *testing.T) { | ||
| // Expected | ||
| mockSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "mockToken"}) | ||
| mockScopes := []string{"scope1"} | ||
| badKey := ocrypto.RsaKeyPair{} | ||
|
|
||
| // Testable | ||
| tokenSource, err := NewOAuthAccessTokenSource(mockSource, mockScopes, &badKey) | ||
|
|
||
| // Sanity Checks | ||
| require.Error(t, err) | ||
| assert.Nil(t, tokenSource) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.