Skip to content
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

update client assertion #2155

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions sdk/v2_requestExecutor.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (a *PrivateKeyAuth) Authorize(method, URL string) error {
return err
}

accessToken, nonce, privateKey, err := getAccessTokenForPrivateKey(a.httpClient, a.orgURL, clientAssertion, a.scopes, a.maxRetries, a.maxBackoff)
accessToken, nonce, privateKey, err := getAccessTokenForPrivateKey(a.httpClient, a.orgURL, clientAssertion, a.scopes, a.maxRetries, a.maxBackoff, a.clientId, a.privateKeySigner)
if err != nil {
return err
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (a *JWTAuth) Authorize(method, URL string) error {
}
}
} else {
accessToken, nonce, privateKey, err := getAccessTokenForPrivateKey(a.httpClient, a.orgURL, a.clientAssertion, a.scopes, a.maxRetries, a.maxBackoff)
accessToken, nonce, privateKey, err := getAccessTokenForPrivateKey(a.httpClient, a.orgURL, a.clientAssertion, a.scopes, a.maxRetries, a.maxBackoff, "", nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -349,30 +349,29 @@ func CreateClientAssertion(orgURL, clientID string, privateKeySinger jose.Signer
Expiry: jwt.NewNumericDate(time.Now().Add(time.Hour * time.Duration(1))),
Issuer: clientID,
Audience: orgURL + "/oauth2/v1/token",
ID: uuid.New().String(),
}
jwtBuilder := jwt.Signed(privateKeySinger).Claims(claims)
return jwtBuilder.CompactSerialize()
}

func getAccessTokenForPrivateKey(httpClient *http.Client, orgURL, clientAssertion string, scopes []string, maxRetries int32, maxBackoff int64) (*RequestAccessToken, string, *rsa.PrivateKey, error) {
var tokenRequestBuff io.ReadWriter
func getAccessTokenForPrivateKey(httpClient *http.Client, orgURL, clientAssertion string, scopes []string, maxRetries int32, maxBackoff int64, clientID string, signer jose.Signer) (*RequestAccessToken, string, *rsa.PrivateKey, error) {
query := urlpkg.Values{}
tokenRequestURL := orgURL + "/oauth2/v1/token"

query.Add("grant_type", "client_credentials")
query.Add("scope", strings.Join(scopes, " "))
query.Add("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
query.Add("client_assertion", clientAssertion)
tokenRequestURL += "?" + query.Encode()
tokenRequest, err := http.NewRequest("POST", tokenRequestURL, tokenRequestBuff)

tokenRequest, err := http.NewRequest("POST", tokenRequestURL, strings.NewReader(query.Encode()))
if err != nil {
return nil, "", nil, err
}

tokenRequest.Header.Add("Accept", "application/json")
tokenRequest.Header.Add("Content-Type", "application/x-www-form-urlencoded")
tokenRequest.Header.Add("User-Agent", NewUserAgent(&config{}).String())

bOff := &oktaBackoff{
ctx: context.TODO(),
maxRetries: maxRetries,
Expand All @@ -393,9 +392,14 @@ func getAccessTokenForPrivateKey(httpClient *http.Client, orgURL, clientAssertio
tokenResponse.Body = origResp
var accessToken *RequestAccessToken

newClientAssertion, err := CreateClientAssertion(orgURL, clientID, signer)
if err != nil {
return nil, "", nil, err
}

if tokenResponse.StatusCode >= 300 {
if strings.Contains(string(respBody), "invalid_dpop_proof") {
return getAccessTokenForDpopPrivateKey(tokenRequest, httpClient, orgURL, "", maxRetries, maxBackoff)
return getAccessTokenForDpopPrivateKey(tokenRequest, httpClient, orgURL, "", maxRetries, maxBackoff, newClientAssertion, strings.Join(scopes, " "), clientID, signer)
} else {
return nil, "", nil, err
}
Expand All @@ -408,7 +412,7 @@ func getAccessTokenForPrivateKey(httpClient *http.Client, orgURL, clientAssertio
return accessToken, "", nil, nil
}

func getAccessTokenForDpopPrivateKey(tokenRequest *http.Request, httpClient *http.Client, orgURL, nonce string, maxRetries int32, maxBackoff int64) (*RequestAccessToken, string, *rsa.PrivateKey, error) {
func getAccessTokenForDpopPrivateKey(tokenRequest *http.Request, httpClient *http.Client, orgURL, nonce string, maxRetries int32, maxBackoff int64, clientAssertion string, scopes string, clientID string, signer jose.Signer) (*RequestAccessToken, string, *rsa.PrivateKey, error) {
privateKey, err := generatePrivateKey(2048)
if err != nil {
return nil, "", nil, err
Expand All @@ -417,7 +421,19 @@ func getAccessTokenForDpopPrivateKey(tokenRequest *http.Request, httpClient *htt
if err != nil {
return nil, "", nil, err
}
newClientAssertion, err := CreateClientAssertion(orgURL, clientID, signer)
if err != nil {
return nil, "", nil, err
}

query := urlpkg.Values{}
query.Add("grant_type", "client_credentials")
query.Add("scope", scopes)
query.Add("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
query.Add("client_assertion", newClientAssertion)
tokenRequest.Body = io.NopCloser(strings.NewReader(query.Encode()))
tokenRequest.Header.Set("DPoP", dpopJWT)

bOff := &oktaBackoff{
ctx: context.TODO(),
maxRetries: maxRetries,
Expand All @@ -441,7 +457,7 @@ func getAccessTokenForDpopPrivateKey(tokenRequest *http.Request, httpClient *htt
if tokenResponse.StatusCode >= 300 {
if strings.Contains(string(respBody), "use_dpop_nonce") {
newNonce := tokenResponse.Header.Get("Dpop-Nonce")
return getAccessTokenForDpopPrivateKey(tokenRequest, httpClient, orgURL, newNonce, maxRetries, maxBackoff)
return getAccessTokenForDpopPrivateKey(tokenRequest, httpClient, orgURL, newNonce, maxRetries, maxBackoff, clientAssertion, scopes, clientID, signer)
} else {
return nil, "", nil, err
}
Expand Down
Loading