-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat: modify auth token url based on instance param #260
Draft
cmars
wants to merge
3
commits into
main
Choose a base branch
from
feat/redirect-with-instance-param
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.
+116
−2
Draft
Changes from all commits
Commits
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ import ( | |
"math/big" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"regexp" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -287,6 +289,7 @@ func (o *oAuth2Authenticator) authenticateWithAuthorizationCode() error { | |
var responseCode string | ||
var responseState string | ||
var responseError string | ||
var responseInstance string | ||
verifier, err := createVerifier(128) | ||
if err != nil { | ||
return err | ||
|
@@ -336,6 +339,7 @@ func (o *oAuth2Authenticator) authenticateWithAuthorizationCode() error { | |
appUrl := o.config.GetString(configuration.WEB_APP_URL) | ||
responseCode = html.EscapeString(r.URL.Query().Get("code")) | ||
responseState = html.EscapeString(r.URL.Query().Get("state")) | ||
responseInstance = html.EscapeString(r.URL.Query().Get("instance")) | ||
w.Header().Add("Location", appUrl+"/authenticated?type=oauth") | ||
w.WriteHeader(http.StatusMovedPermanently) | ||
} | ||
|
@@ -388,6 +392,23 @@ func (o *oAuth2Authenticator) authenticateWithAuthorizationCode() error { | |
return fmt.Errorf("incorrect response state: %s != %s", responseState, state) | ||
} | ||
|
||
if responseInstance != "" { | ||
authHost := redirectAuthHost(responseInstance) | ||
if err != nil { | ||
return fmt.Errorf("invalid instance: %q", responseInstance) | ||
} | ||
if !isValidAuthHost(authHost) { | ||
return fmt.Errorf("invalid instance: %q", responseInstance) | ||
} | ||
|
||
authURL, err := url.Parse(o.oauthConfig.Endpoint.AuthURL) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse auth url: %w", err) | ||
} | ||
authURL.Host = authHost | ||
o.oauthConfig.Endpoint.AuthURL = authURL.String() | ||
} | ||
|
||
// Use the custom HTTP client when requesting a token. | ||
if o.httpClient != nil { | ||
ctx = context.WithValue(ctx, oauth2.HTTPClient, o.httpClient) | ||
|
@@ -402,6 +423,16 @@ func (o *oAuth2Authenticator) authenticateWithAuthorizationCode() error { | |
return err | ||
} | ||
|
||
func redirectAuthHost(instance string) string { | ||
return fmt.Sprintf("api.%s", instance) | ||
} | ||
|
||
var redirectAuthHostRE = regexp.MustCompile(`^api\.(.+)\.snyk\.io$`) | ||
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. snykgov too! |
||
|
||
func isValidAuthHost(authHost string) bool { | ||
return redirectAuthHostRE.MatchString(authHost) | ||
} | ||
|
||
func (o *oAuth2Authenticator) AddAuthenticationHeader(request *http.Request) error { | ||
if request == nil { | ||
return fmt.Errorf("request must not be nil") | ||
|
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.
could we by any change have multiple entries in the
aud
claim ?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.
From the spec:
It is almost expected for there to be multiple values available. We should likely search the list and extract the first one which meets our criteria.
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.
Guidance from the IdP team is to use the first value. I can provide more context offline.