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

Use state if existing in session #43

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
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
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