Skip to content

Commit 5fbb850

Browse files
committed
chore: guard pairing calls from concurrent execution
1 parent 357d38a commit 5fbb850

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

packages/profile-sync-controller/src/controllers/authentication/AuthenticationController.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export type AuthenticationControllerState = {
3434
srpSessionData?: Record<string, LoginResponse>;
3535
socialPairingToken?: string;
3636
socialPairingDone?: boolean;
37+
pairingInProgress?: boolean;
3738
};
3839
export const defaultState: AuthenticationControllerState = {
3940
isSignedIn: false,
@@ -55,6 +56,10 @@ const metadata: StateMetadata<AuthenticationControllerState> = {
5556
persist: true,
5657
anonymous: true,
5758
},
59+
pairingInProgress: {
60+
persist: false,
61+
anonymous: true,
62+
}
5863
};
5964

6065
// Messenger Actions
@@ -353,13 +358,16 @@ export default class AuthenticationController extends BaseController<
353358

354359
async #tryPairingWithSocialToken(): Promise<void> {
355360
console.log(`GIGEL: trying to pair with seedless token`);
356-
const { socialPairingToken, socialPairingDone } = this.state;
357-
if (socialPairingDone || !socialPairingToken) {
361+
const { socialPairingToken, socialPairingDone, pairingInProgress } = this.state;
362+
if (socialPairingDone || !socialPairingToken || pairingInProgress) {
358363
console.log(`GIGEL: pairing conditions not met`);
359364
return;
360365
}
361366

362367
try {
368+
this.update((state) => {
369+
state.pairingInProgress = true;
370+
});
363371
console.log(`GIGEL: pairing with seedless token ${socialPairingToken}`);
364372
if (await this.#auth.pairSocialIdentifier(socialPairingToken)) {
365373
console.log(`GIGEL: successfully paired with seedless onboarding token`);
@@ -374,6 +382,10 @@ export default class AuthenticationController extends BaseController<
374382
} catch (error) {
375383
console.error('GIGEL: Failed to pair identifiers:', error);
376384
// ignore the error
385+
} finally {
386+
this.update((state) => {
387+
state.pairingInProgress = false;
388+
});
377389
}
378390
}
379391

packages/profile-sync-controller/src/sdk/authentication-jwt-bearer/flow-srp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
isSnapConnected,
2727
} from '../utils/messaging-signing-snap-requests';
2828
import { validateLoginResponse } from '../utils/validate-login-response';
29+
import { Env } from '../../shared/env';
2930

3031
type JwtBearerAuth_SRP_Options = {
3132
storage: AuthStorageOptions;
@@ -213,7 +214,10 @@ export class SRPJwtBearerAuth implements IBaseAuth {
213214
`GIGEL: pairing primary SRP with social token ${jwt}`,
214215
);
215216

216-
const { env, platform } = this.#config;
217+
// TODO: need to hardcode the env as web3auth prod is not available.
218+
// const { env, platform } = this.#config;
219+
const { platform } = this.#config;
220+
const env = Env.DEV;
217221

218222
// Exchange the social token with an access token
219223
console.log(`GIGEL: exchanging social token for access token`);
@@ -239,6 +243,7 @@ export class SRPJwtBearerAuth implements IBaseAuth {
239243
const pairUrl = new URL(PAIR_SOCIAL_IDENTIFIER(env));
240244

241245
try {
246+
// TODO: this will FAIL as long as the ENV don't match.
242247
const response = await fetch(pairUrl, {
243248
method: 'POST',
244249
headers: {

packages/profile-sync-controller/src/sdk/authentication-jwt-bearer/services.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,30 +156,35 @@ export async function authorizeOIDC(
156156
urlEncodedBody.append('client_id', getOidcClientId(env, platform));
157157
urlEncodedBody.append('assertion', jwtToken);
158158

159+
console.log(`GIGEL [identity auth] requesting OIDC token with grant_type: ${grantType}, client_id: ${getOidcClientId(env, platform)} from ${OIDC_TOKEN_URL(env)} using jwtToken: ${jwtToken}`);
159160
try {
160161
const response = await fetch(OIDC_TOKEN_URL(env), {
161162
method: 'POST',
162163
headers,
163164
body: urlEncodedBody.toString(),
164165
});
166+
console.log(`GIGEL [identity auth] OIDC token response status: ${response.status}`);
165167

166168
if (!response.ok) {
167169
const responseBody = (await response.json()) as {
168170
error_description: string;
169171
error: string;
170172
};
173+
console.error(`GIGEL [identity auth] OIDC token error response: ${JSON.stringify(responseBody)}`);
171174
throw new Error(
172175
`HTTP error: ${responseBody.error_description}, error code: ${responseBody.error}`,
173176
);
174177
}
175178

176179
const accessTokenResponse = await response.json();
180+
console.log(`GIGEL [identity auth] OIDC token response: ${JSON.stringify(accessTokenResponse)}`);
177181
return {
178182
accessToken: accessTokenResponse.access_token,
179183
expiresIn: accessTokenResponse.expires_in,
180184
obtainedAt: Date.now(),
181185
};
182186
} catch (e) {
187+
console.error(`GIGEL [identity auth] OIDC token request failed: ${e as Error}`);
183188
/* istanbul ignore next */
184189
const errorMessage =
185190
e instanceof Error ? e.message : JSON.stringify(e ?? '');

0 commit comments

Comments
 (0)