-
Notifications
You must be signed in to change notification settings - Fork 24
feat(core): Expose context authn methods #1812
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dfa463e
expose context methods
elizabethhealy 1052141
linting
elizabethhealy b774ebc
Merge branch 'main' into expose-context-auth-methods
elizabethhealy 5248a6d
Merge branch 'main' into expose-context-auth-methods
elizabethhealy 02034fb
use struct instead
elizabethhealy 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
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,64 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/lestrrat-go/jwx/v2/jwk" | ||
| "github.com/lestrrat-go/jwx/v2/jwt" | ||
| "github.com/opentdf/platform/service/logger" | ||
| ) | ||
|
|
||
| const ( | ||
| authnContextKey = authContextKey("dpop-jwk") | ||
| ) | ||
|
|
||
| type authContextKey string | ||
|
|
||
| type authContext struct { | ||
| key jwk.Key | ||
| accessToken jwt.Token | ||
| rawToken string | ||
| } | ||
|
|
||
| func ContextWithAuthNInfo(ctx context.Context, key jwk.Key, accessToken jwt.Token, raw string) context.Context { | ||
| return context.WithValue(ctx, authnContextKey, &authContext{ | ||
| key, | ||
| accessToken, | ||
| raw, | ||
| }) | ||
| } | ||
|
|
||
| func getContextDetails(ctx context.Context, l *logger.Logger) *authContext { | ||
| key := ctx.Value(authnContextKey) | ||
| if key == nil { | ||
| return nil | ||
| } | ||
| if c, ok := key.(*authContext); ok { | ||
| return c | ||
| } | ||
|
|
||
| // We should probably return an error here? | ||
| l.ErrorContext(ctx, "invalid authContext") | ||
| return nil | ||
| } | ||
|
|
||
| func GetJWKFromContext(ctx context.Context, l *logger.Logger) jwk.Key { | ||
| if c := getContextDetails(ctx, l); c != nil { | ||
| return c.key | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func GetAccessTokenFromContext(ctx context.Context, l *logger.Logger) jwt.Token { | ||
| if c := getContextDetails(ctx, l); c != nil { | ||
| return c.accessToken | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func GetRawAccessTokenFromContext(ctx context.Context, l *logger.Logger) string { | ||
| if c := getContextDetails(ctx, l); c != nil { | ||
| return c.rawToken | ||
| } | ||
| return "" | ||
| } | ||
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,72 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/lestrrat-go/jwx/v2/jwk" | ||
| "github.com/lestrrat-go/jwx/v2/jwt" | ||
| "github.com/opentdf/platform/service/logger" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestContextWithAuthNInfo(t *testing.T) { | ||
| // Create mock JWK, JWT, and raw token | ||
| mockJWK, _ := jwk.FromRaw([]byte("mockKey")) | ||
| mockJWT, _ := jwt.NewBuilder().Build() | ||
| rawToken := "mockRawToken" | ||
|
|
||
| // Initialize context | ||
| ctx := context.Background() | ||
| newCtx := ContextWithAuthNInfo(ctx, mockJWK, mockJWT, rawToken) | ||
|
|
||
| // Assert that the context contains the correct values | ||
| value := newCtx.Value(authnContextKey) | ||
| testAuthContext, ok := value.(*authContext) | ||
| assert.True(t, ok) | ||
| assert.NotNil(t, testAuthContext) | ||
| assert.Equal(t, mockJWK, testAuthContext.key, "JWK should match") | ||
| assert.Equal(t, mockJWT, testAuthContext.accessToken, "JWT should match") | ||
| assert.Equal(t, rawToken, testAuthContext.rawToken, "Raw token should match") | ||
| } | ||
|
|
||
| func TestGetJWKFromContext(t *testing.T) { | ||
| // Create mock context with JWK | ||
| mockJWK, _ := jwk.FromRaw([]byte("mockKey")) | ||
| ctx := ContextWithAuthNInfo(context.Background(), mockJWK, nil, "") | ||
|
|
||
| // Retrieve the JWK and assert | ||
| retrievedJWK := GetJWKFromContext(ctx, logger.CreateTestLogger()) | ||
| assert.NotNil(t, retrievedJWK, "JWK should not be nil") | ||
| assert.Equal(t, mockJWK, retrievedJWK, "Retrieved JWK should match the mock JWK") | ||
| } | ||
|
|
||
| func TestGetAccessTokenFromContext(t *testing.T) { | ||
| // Create mock context with JWT | ||
| mockJWT, _ := jwt.NewBuilder().Build() | ||
| ctx := ContextWithAuthNInfo(context.Background(), nil, mockJWT, "") | ||
|
|
||
| // Retrieve the JWT and assert | ||
| retrievedJWT := GetAccessTokenFromContext(ctx, logger.CreateTestLogger()) | ||
| assert.NotNil(t, retrievedJWT, "Access token should not be nil") | ||
| assert.Equal(t, mockJWT, retrievedJWT, "Retrieved JWT should match the mock JWT") | ||
| } | ||
|
|
||
| func TestGetRawAccessTokenFromContext(t *testing.T) { | ||
| // Create mock context with raw token | ||
| rawToken := "mockRawToken" | ||
| ctx := ContextWithAuthNInfo(context.Background(), nil, nil, rawToken) | ||
|
|
||
| // Retrieve the raw token and assert | ||
| retrievedRawToken := GetRawAccessTokenFromContext(ctx, logger.CreateTestLogger()) | ||
| assert.Equal(t, rawToken, retrievedRawToken, "Retrieved raw token should match the mock raw token") | ||
| } | ||
|
|
||
| func TestGetContextDetailsInvalidType(t *testing.T) { | ||
| // Create a context with an invalid type | ||
| ctx := context.WithValue(context.Background(), authnContextKey, "invalidType") | ||
|
|
||
| // Assert that GetJWKFromContext handles the invalid type correctly | ||
| retrievedJWK := GetJWKFromContext(ctx, logger.CreateTestLogger()) | ||
| assert.Nil(t, retrievedJWK, "JWK should be nil when context value is invalid") | ||
| } |
Oops, something went wrong.
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.