Skip to content

Commit

Permalink
Allow using customstate
Browse files Browse the repository at this point in the history
  • Loading branch information
bpedroza committed Jul 12, 2021
1 parent cf73454 commit 0cfa6c8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-pkce",
"version": "1.1.2",
"version": "1.1.3",
"description": "A package that makes using the OAuth2 PKCE flow easier",
"main": "dist/PKCE.js",
"types": "dist/PKCE.d.ts",
Expand Down
3 changes: 3 additions & 0 deletions src/IObject.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface IObject {
[key: string]: any;
}
17 changes: 12 additions & 5 deletions src/PKCE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Base64 from 'crypto-js/enc-base64';
import WordArray from 'crypto-js/lib-typedarrays';
import IAuthResponse from './IAuthResponse';
import IConfig from './IConfig';
import IObject from './IObject';
import ITokenResponse from './ITokenResponse';

export default class PKCE {
Expand All @@ -23,15 +24,15 @@ export default class PKCE {
* @param {object} additionalParams include additional parameters in the query
* @return Promise<string>
*/
public authorizeUrl(additionalParams: object = {}): string {
public authorizeUrl(additionalParams: IObject = {}): string {
const codeChallenge = this.pkceChallengeFromVerifier();

const queryString = new URLSearchParams(
Object.assign(
{
response_type: 'code',
client_id: this.config.client_id,
state: this.getState(),
state: this.getState(additionalParams.state || null),
scope: this.config.requested_scopes,
redirect_uri: this.config.redirect_uri,
code_challenge: codeChallenge,
Expand All @@ -50,7 +51,7 @@ export default class PKCE {
* @param {object} additionalParams include additional parameters in the request body
* @return {Promise<ITokenResponse>}
*/
public exchangeForAccessToken(url: string, additionalParams: object = {}): Promise<ITokenResponse> {
public exchangeForAccessToken(url: string, additionalParams: IObject = {}): Promise<ITokenResponse> {
return this.parseAuthResponseUrl(url).then((q) => {
return fetch(this.config.token_endpoint, {
method: 'POST',
Expand Down Expand Up @@ -90,9 +91,15 @@ export default class PKCE {
* Get the current state or generate a new one
* @return {string}
*/
private getState(): string {
private getState(explicit: string = null): string {
const stateKey = 'pkce_state';

if (explicit !== null) {
sessionStorage.setItem(stateKey, explicit);
}

if (this.state === '') {
this.state = this.randomStringFromStorage('pkce_state');
this.state = this.randomStringFromStorage(stateKey);
}

return this.state;
Expand Down
8 changes: 8 additions & 0 deletions tests/PKCE.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ describe('Test PKCE authorization url', () => {
expect(url).toContain('&client_id=' + config.client_id);
expect(url).toContain('&test_param=test');
});

it('Should update state from additional params', async () => {
const instance = new PKCE(config);
const url = instance.authorizeUrl({state: 'Anewteststate'});

expect(url).toContain('&state=Anewteststate');
expect(sessionStorage.getItem('pkce_state')).toEqual('Anewteststate');
});
});

describe('Test PKCE exchange code for token', () => {
Expand Down

0 comments on commit 0cfa6c8

Please sign in to comment.