refactor: extract method for making authCodeURL

Co-authored-by: tronghn <trong.huu.nguyen@nav.no>
This commit is contained in:
Sindre Rødseth Hansen
2025-01-24 10:02:15 +01:00
parent 39207677b5
commit c07077a148
2 changed files with 26 additions and 18 deletions

View File

@@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"net/http"
urllib "net/url"
"slices"
stringslib "strings"
@@ -129,25 +128,25 @@ func (c *Client) authCodeURL(ctx context.Context, authCodeParams openid.Authoriz
return "", fmt.Errorf("unmarshalling token response: %w", err)
}
// TODO: this can be a separate function to replace oauth2config.AuthCodeURL
v := urllib.Values{
"client_id": {c.oauth2Config.ClientID},
"request_uri": {pushedAuthorizationResponse.RequestUri},
}
var buf bytes.Buffer
buf.WriteString(c.oauth2Config.Endpoint.AuthURL)
if stringslib.Contains(c.oauth2Config.Endpoint.AuthURL, "?") {
buf.WriteByte('&')
} else {
buf.WriteByte('?')
}
buf.WriteString(v.Encode())
return buf.String(), nil
return c.makeAuthCodeURL(openid.ParAuthorizationRequestParams(
c.oauth2Config.ClientID,
pushedAuthorizationResponse.RequestUri,
)), nil
}
opts := authCodeParams.RequestParams().AuthCodeOptions()
// TODO: replace with separate function
return c.oauth2Config.AuthCodeURL(authCodeParams.State, opts...), nil
return c.makeAuthCodeURL(authCodeParams.RequestParams()), nil
}
func (c *Client) makeAuthCodeURL(params openid.RequestParams) string {
var buf bytes.Buffer
buf.WriteString(c.oauth2Config.Endpoint.AuthURL)
if stringslib.Contains(c.oauth2Config.Endpoint.AuthURL, "?") {
buf.WriteByte('&')
} else {
buf.WriteByte('?')
}
buf.WriteString(params.URLValues().Encode())
return buf.String()
}
func (l *Login) SetCookie(w http.ResponseWriter, opts cookie.Options, crypter crypto.Crypter, canonicalRedirect string) error {

View File

@@ -164,6 +164,15 @@ func RefreshGrantParams(clientID, refreshToken string) RequestParams {
}
}
// ParAuthorizationRequestParams returns a map of parameters to be sent to the authorization server when using the
// authorization endpoint after performing a Pushed Authorization Request (PAR) as defined in RFC 9126, section 4.
func ParAuthorizationRequestParams(clientID, requestUri string) RequestParams {
return RequestParams{
"client_id": clientID,
"request_uri": requestUri,
}
}
func StateMismatchError(queryParams url.Values, expectedState string) error {
actualState := queryParams.Get("state")