I have created a fork of the okta-angular library in order to implement support for the Authorization Code Flow with PKCE.
The redirect to the /authorize
endpoint works as expected:
async authorizationCodeRedirect() {
const url = this.auth.issuer + '/v1/authorize'
+ '?response_type=' + encodeURIComponent(this.auth.responseType)
+ '&client_id=' + encodeURIComponent(this.auth.clientId)
+ '&state=' + encodeURIComponent(this.auth.state)
+ '&scope=' + encodeURIComponent(this.auth.scope)
+ '&redirect_uri=' + encodeURIComponent(this.auth.redirectUri)
+ '&code_challenge=' + encodeURIComponent(this.auth.code_challenge)
+ '&code_challenge_method=' + encodeURIComponent(this.auth.code_challenge_method);
this.document.location.href = url;
}
As does the POST to the /token endpoint:
async handleAuthorizationCodeFlow() {
const params = new URLSearchParams(this.document.location.search.substring(1));
const code = params.get('code');
const state = params.get('state');
const endpoint = this.auth.issuer + '/v1/token';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
})
};
const body = {
grant_type: 'authorization_code',
client_id: this.auth.clientId,
redirect_uri: this.auth.redirectUri,
code: code,
code_verifier: 'M25iVXpKU3puUjFaYWg3T1NDTDQtcW1ROUY5YXlwalNoc0hhakxifmZHag'
};
const urlEncoded = Object.keys(body).map(key => key + '=' + body[key]).join('&');
const response = await this.http.post<any>(endpoint, urlEncoded, httpOptions).toPromise();
...
}
The response is as expected:
{
"token_type": "Bearer",
"expires_in": 3600,
"access_token": "eyJraWQiOiJCMHcxTjV ...",
"scope": "openid email groups profile address phone",
"id_token": "eyJraWQiOiJCMHcx ..."
}
However, if I try to add the tokens to the token manager:
...
if (response.id_token) {
this.oktaAuth.tokenManager.add('idToken', response.id_token);
}
if (response.access_token) {
this.oktaAuth.tokenManager.add('accessToken', response.access_token);
}
I receive the following error:
// ERROR Error: Uncaught (in promise): AuthSdkError: Token must be an Object with scopes, expiresAt, and an idToken or
// accessToken properties