-
Notifications
You must be signed in to change notification settings - Fork 11
fix(api): Google OAuth provider #26
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
Open
kelkawi-a
wants to merge
7
commits into
tuannvm:main
Choose a base branch
from
kelkawi-a:fix/google-oauth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
470322b
fix(api): Google OAuth provider
kelkawi-a 3c1fb73
address review comments
kelkawi-a 99dac51
address review comments
kelkawi-a 1d17606
address review comments
kelkawi-a 1421767
fix regression
kelkawi-a 18c789b
address review comment
kelkawi-a 087f3b7
Merge branch 'main' into fix/google-oauth
kelkawi-a 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package oauth | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| func tokenHash(v interface{}) string { | ||
| s, _ := v.(string) | ||
| if s == "" { | ||
| return "<empty>" | ||
| } | ||
| return fmt.Sprintf("%x", sha256.Sum256([]byte(s)))[:16] | ||
| } | ||
|
|
||
| func TestBuildTokenResponse(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| provider string | ||
| accessToken string | ||
| idToken string | ||
| refreshToken string | ||
| scope string | ||
| expectedAccessToken string | ||
| expectedIDToken string | ||
| expectedRefreshToken string | ||
| }{ | ||
| { | ||
| name: "google opaque access token uses id token", | ||
| provider: "google", | ||
| accessToken: "ya29.a0ARW5m7Opaque", | ||
| idToken: "header.payload.signature", | ||
| refreshToken: "refresh-token", | ||
| scope: "openid profile email", | ||
| expectedAccessToken: "header.payload.signature", | ||
| expectedIDToken: "header.payload.signature", | ||
| expectedRefreshToken: "refresh-token", | ||
| }, | ||
| { | ||
| name: "google jwt access token preserved", | ||
| provider: "google", | ||
| accessToken: "jwt.access.token", | ||
| idToken: "id.token.value", | ||
| expectedAccessToken: "jwt.access.token", | ||
| expectedIDToken: "id.token.value", | ||
| }, | ||
| { | ||
| name: "non google access token preserved", | ||
| provider: "okta", | ||
| accessToken: "opaque-token-value", | ||
| idToken: "id.token.value", | ||
| expectedAccessToken: "opaque-token-value", | ||
| expectedIDToken: "id.token.value", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| handler := &OAuth2Handler{ | ||
| config: &OAuth2Config{Provider: tt.provider}, | ||
| logger: &defaultLogger{}, | ||
| } | ||
|
|
||
| extra := map[string]interface{}{ | ||
| "id_token": tt.idToken, | ||
| } | ||
| if tt.scope != "" { | ||
| extra["scope"] = tt.scope | ||
| } | ||
|
|
||
| token := (&oauth2.Token{ | ||
| AccessToken: tt.accessToken, | ||
| TokenType: "Bearer", | ||
| RefreshToken: tt.refreshToken, | ||
| Expiry: time.Now().Add(time.Hour), | ||
| }).WithExtra(extra) | ||
|
|
||
| response := handler.buildTokenResponse(token) | ||
|
|
||
| if got := response["access_token"]; got != tt.expectedAccessToken { | ||
| t.Fatalf("access_token hash = %s, want hash %s", tokenHash(got), tokenHash(tt.expectedAccessToken)) | ||
| } | ||
|
|
||
| if got := response["id_token"]; got != tt.expectedIDToken { | ||
| t.Fatalf("id_token hash = %s, want hash %s", tokenHash(got), tokenHash(tt.expectedIDToken)) | ||
| } | ||
|
|
||
| if got := response["refresh_token"]; got != tt.expectedRefreshToken && !(got == nil && tt.expectedRefreshToken == "") { | ||
| t.Fatalf("refresh_token hash = %s, want hash %s", tokenHash(got), tokenHash(tt.expectedRefreshToken)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.