Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
feat: add support for pcke state
Browse files Browse the repository at this point in the history
  • Loading branch information
einaralex committed Dec 15, 2023
1 parent 9d4f0dd commit 741c581
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 13 additions & 4 deletions libs/sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class MoneriumClient {

#authorizationHeader?: string;
/**
* @deprecated, use sessionStorage
* @deprecated, use sessionStorage, will be removed in v3
* The PKCE code verifier
* */
codeVerifier?: string;
Expand All @@ -63,6 +63,8 @@ export class MoneriumClient {

#client?: BearerTokenCredentials;

state: string | undefined;

/** Constructor for no arguments, defaults to sandbox */
constructor();
/** Constructor with only env as an argument*/
Expand Down Expand Up @@ -128,6 +130,7 @@ export class MoneriumClient {
address: client?.address,
signature: client?.signature,
chainId: client?.chainId,
state: client?.state,
});

// Redirect to the authFlow
Expand Down Expand Up @@ -172,13 +175,16 @@ export class MoneriumClient {
const authCode =
new URLSearchParams(window.location.search).get('code') || undefined;

const state =
new URLSearchParams(window.location.search).get('state') || undefined;

const refreshToken =
sessionStorage.getItem(STORAGE_REFRESH_TOKEN) || undefined;

if (refreshToken) {
await this.#refreshTokenAuthorization(clientId, refreshToken);
} else if (authCode) {
await this.#authCodeAuthorization(clientId, redirectUrl, authCode);
await this.#authCodeAuthorization(clientId, redirectUrl, authCode, state);
}

return !!this.bearerProfile;
Expand Down Expand Up @@ -378,17 +384,20 @@ export class MoneriumClient {
#authCodeAuthorization = async (
clientId: string,
redirectUrl: string,
authCode: string
authCode: string,
state?: string
) => {
const codeVerifier = sessionStorage.getItem(STORAGE_CODE_VERIFIER) || '';

if (!codeVerifier) {
throw new Error('Code verifier not found');
}

/** @deprecated, use sessionStorage */
/** @deprecated, use sessionStorage, will be removed in v3 */
this.codeVerifier = codeVerifier;

this.state = state;

sessionStorage.removeItem(STORAGE_CODE_VERIFIER);
// Remove auth code from URL.
return await this.#grantAccess({
Expand Down
1 change: 1 addition & 0 deletions libs/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ export type AuthFlowOptions = {
address?: string;
signature?: string;
chainId?: ChainId;
state?: string;
};

export type ClientCredentials = {
Expand Down

0 comments on commit 741c581

Please sign in to comment.