Skip to content

Commit

Permalink
Merge pull request #43 from kinde-oss/leo/same_state
Browse files Browse the repository at this point in the history
Use state if existing in session
  • Loading branch information
DaveOrDead authored Jan 15, 2024
2 parents 943f759 + 5b56d07 commit e39d549
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/__tests__/sdk/oauth2-flows/AuthorizationCode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ describe('AuthorizationCode', () => {
const state = searchParams.get('state');
expect(state).toBe(expectedState);
});

it('uses same state to generate authorization URL if existing in session', async () => {
const client = new AuthorizationCode(clientConfig, clientSecret);
const authURL = await client.createAuthorizationURL(sessionManager);
const searchParams = new URLSearchParams(authURL.search);
const firstState = searchParams.get('state');

const authURL2 = await client.createAuthorizationURL(sessionManager);
const searchParams2 = new URLSearchParams(authURL2.search);
const secondState = searchParams2.get('state');

expect(firstState).toBe(secondState);
});
});

describe('handleRedirectFromAuthDomain()', () => {
Expand Down
7 changes: 6 additions & 1 deletion lib/sdk/oauth2-flows/AuthorizationCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export class AuthorizationCode extends AuthCodeAbstract {
sessionManager: SessionManager,
options: AuthURLOptions = {}
): Promise<URL> {
this.state = options.state ?? utilities.generateRandomString();
this.state =
options.state ??
((await sessionManager.getSessionItem(
AuthorizationCode.STATE_KEY
)) as string) ??
utilities.generateRandomString();
await sessionManager.setSessionItem(AuthorizationCode.STATE_KEY, this.state);
const authURL = new URL(this.authorizationEndpoint);
const authParams = this.generateAuthURLParams(options);
Expand Down

0 comments on commit e39d549

Please sign in to comment.