@@ -3,6 +3,7 @@ import Base64 from 'crypto-js/enc-base64';
3
3
import WordArray from 'crypto-js/lib-typedarrays' ;
4
4
import IAuthResponse from './IAuthResponse' ;
5
5
import IConfig from './IConfig' ;
6
+ import IObject from './IObject' ;
6
7
import ITokenResponse from './ITokenResponse' ;
7
8
8
9
export default class PKCE {
@@ -23,15 +24,15 @@ export default class PKCE {
23
24
* @param {object } additionalParams include additional parameters in the query
24
25
* @return Promise<string>
25
26
*/
26
- public authorizeUrl ( additionalParams : object = { } ) : string {
27
+ public authorizeUrl ( additionalParams : IObject = { } ) : string {
27
28
const codeChallenge = this . pkceChallengeFromVerifier ( ) ;
28
29
29
30
const queryString = new URLSearchParams (
30
31
Object . assign (
31
32
{
32
33
response_type : 'code' ,
33
34
client_id : this . config . client_id ,
34
- state : this . getState ( ) ,
35
+ state : this . getState ( additionalParams . state || null ) ,
35
36
scope : this . config . requested_scopes ,
36
37
redirect_uri : this . config . redirect_uri ,
37
38
code_challenge : codeChallenge ,
@@ -50,7 +51,7 @@ export default class PKCE {
50
51
* @param {object } additionalParams include additional parameters in the request body
51
52
* @return {Promise<ITokenResponse> }
52
53
*/
53
- public exchangeForAccessToken ( url : string , additionalParams : object = { } ) : Promise < ITokenResponse > {
54
+ public exchangeForAccessToken ( url : string , additionalParams : IObject = { } ) : Promise < ITokenResponse > {
54
55
return this . parseAuthResponseUrl ( url ) . then ( ( q ) => {
55
56
return fetch ( this . config . token_endpoint , {
56
57
method : 'POST' ,
@@ -90,9 +91,15 @@ export default class PKCE {
90
91
* Get the current state or generate a new one
91
92
* @return {string }
92
93
*/
93
- private getState ( ) : string {
94
+ private getState ( explicit : string = null ) : string {
95
+ const stateKey = 'pkce_state' ;
96
+
97
+ if ( explicit !== null ) {
98
+ sessionStorage . setItem ( stateKey , explicit ) ;
99
+ }
100
+
94
101
if ( this . state === '' ) {
95
- this . state = this . randomStringFromStorage ( 'pkce_state' ) ;
102
+ this . state = this . randomStringFromStorage ( stateKey ) ;
96
103
}
97
104
98
105
return this . state ;
0 commit comments