Skip to content

Commit ea0e20d

Browse files
committed
Merge pull-request #1074
2 parents 1648aba + beee465 commit ea0e20d

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

.changeset/dark-symbols-warn.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@turnkey/core": patch
3+
---
4+
5+
- added optional `organizationId` to `loginWithOAuth()`
6+
- added optional `invalidateExisting` to `signUpWithOAuth()`
7+
- fixed `invalidateExisting` being ignored in `completeOAuth()` during signup

packages/core/src/__clients__/core.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,10 @@ export class TurnkeyClient {
484484
* - Stores the resulting session token and manages cleanup of unused key pairs.
485485
*
486486
* @param params.passkeyDisplayName - display name for the passkey (defaults to a generated name based on the current timestamp).
487+
* @param params.challenge - challenge string to use for passkey registration. If not provided, a new challenge will be generated.
488+
* @param params.expirationSeconds - session expiration time in seconds (defaults to the configured default).
487489
* @param params.createSubOrgParams - parameters for creating a sub-organization (e.g., authenticators, user metadata).
488490
* @param params.sessionKey - session key to use for storing the session (defaults to the default session key).
489-
* @param params.expirationSeconds - session expiration time in seconds (defaults to the configured default).
490-
* @param params.challenge - challenge string to use for passkey registration. If not provided, a new challenge will be generated.
491491
* @param params.organizationId - organization ID to target (defaults to the session's organization ID or the parent organization ID).
492492
* @returns A promise that resolves to a {@link PasskeyAuthResult}, which includes:
493493
* - `sessionToken`: the signed JWT session token.
@@ -498,10 +498,11 @@ export class TurnkeyClient {
498498
params?: SignUpWithPasskeyParams,
499499
): Promise<PasskeyAuthResult> => {
500500
const {
501-
createSubOrgParams,
502501
passkeyDisplayName,
503-
sessionKey = SessionKey.DefaultSessionkey,
502+
challenge,
504503
expirationSeconds = DEFAULT_SESSION_EXPIRATION_IN_SECONDS,
504+
createSubOrgParams,
505+
sessionKey = SessionKey.DefaultSessionkey,
505506
organizationId,
506507
} = params || {};
507508

@@ -514,7 +515,7 @@ export class TurnkeyClient {
514515
// A passkey will be created automatically when you call this function. The name is passed in
515516
const passkey = await this.createPasskey({
516517
name: passkeyName,
517-
...(params?.challenge && { challenge: params.challenge }),
518+
...(challenge && { challenge }),
518519
});
519520

520521
if (!passkey) {
@@ -1653,9 +1654,10 @@ export class TurnkeyClient {
16531654
* @param params.oidcToken - OIDC token received after successful authentication with the OAuth provider.
16541655
* @param params.publicKey - public key to use for authentication. Must be generated prior to calling this function, this is because the OIDC nonce has to be set to `sha256(publicKey)`.
16551656
* @param params.providerName - name of the OAuth provider (defaults to a generated name with a timestamp).
1656-
* @param params.sessionKey - session key to use for session creation (defaults to the default session key).
1657-
* @param params.invalidateExisting - flag to invalidate existing sessions for the user.
16581657
* @param params.createSubOrgParams - parameters for sub-organization creation (e.g., authenticators, user metadata).
1658+
* @param params.invalidateExisting - flag to invalidate existing sessions for the user.
1659+
* @param params.sessionKey - session key to use for session creation (defaults to the default session key).
1660+
*
16591661
* @returns A promise that resolves to an object containing:
16601662
* - `sessionToken`: the signed JWT session token.
16611663
* - `action`: whether the flow resulted in a login or signup ({@link AuthAction}).
@@ -1667,10 +1669,10 @@ export class TurnkeyClient {
16671669
const {
16681670
oidcToken,
16691671
publicKey,
1672+
providerName,
16701673
createSubOrgParams,
1671-
providerName = "OpenID Connect Provider" + " " + Date.now(),
1672-
sessionKey = SessionKey.DefaultSessionkey,
1673-
invalidateExisting = false,
1674+
invalidateExisting,
1675+
sessionKey,
16741676
} = params;
16751677

16761678
return withTurnkeyErrorHandling(
@@ -1692,8 +1694,8 @@ export class TurnkeyClient {
16921694
const loginRes = await this.loginWithOauth({
16931695
oidcToken,
16941696
publicKey,
1695-
invalidateExisting,
1696-
sessionKey,
1697+
...(invalidateExisting && { invalidateExisting }),
1698+
...(sessionKey && { sessionKey }),
16971699
});
16981700

16991701
return {
@@ -1704,11 +1706,14 @@ export class TurnkeyClient {
17041706
const signUpRes = await this.signUpWithOauth({
17051707
oidcToken,
17061708
publicKey,
1707-
providerName,
1708-
sessionKey,
1709+
...(providerName && {
1710+
providerName,
1711+
}),
17091712
...(createSubOrgParams && {
17101713
createSubOrgParams,
17111714
}),
1715+
...(invalidateExisting && { invalidateExisting }),
1716+
...(sessionKey && { sessionKey }),
17121717
});
17131718

17141719
return {
@@ -1733,7 +1738,10 @@ export class TurnkeyClient {
17331738
* - Handles cleanup of unused key pairs if login fails.
17341739
*
17351740
* @param params.oidcToken - OIDC token received after successful authentication with the OAuth provider.
1736-
* @param params.publicKey - public key to use for authentication. Must be generated prior to calling this function.
1741+
* @param params.publicKey - The public key bound to the login session. This key is required because it is directly
1742+
* tied to the nonce used during OIDC token generation and must match the value
1743+
* encoded in the token.
1744+
* @param params.organizationId - ID of the organization to target when creating the session.
17371745
* @param params.invalidateExisting - flag to invalidate existing sessions for the user.
17381746
* @param params.sessionKey - session key to use for session creation (defaults to the default session key).
17391747
* @returns A promise that resolves to a {@link BaseAuthResult}, which includes:
@@ -1745,8 +1753,9 @@ export class TurnkeyClient {
17451753
): Promise<BaseAuthResult> => {
17461754
const {
17471755
oidcToken,
1748-
invalidateExisting = false,
17491756
publicKey,
1757+
organizationId,
1758+
invalidateExisting = false,
17501759
sessionKey = SessionKey.DefaultSessionkey,
17511760
} = params;
17521761

@@ -1763,6 +1772,7 @@ export class TurnkeyClient {
17631772
oidcToken,
17641773
publicKey,
17651774
invalidateExisting,
1775+
...(organizationId && { organizationId }),
17661776
});
17671777

17681778
if (!loginRes) {
@@ -1837,7 +1847,7 @@ export class TurnkeyClient {
18371847
const {
18381848
oidcToken,
18391849
publicKey,
1840-
providerName,
1850+
providerName = "OpenID Connect Provider" + " " + Date.now(),
18411851
createSubOrgParams,
18421852
sessionKey,
18431853
} = params;

packages/core/src/__types__/method-types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ export type SignUpWithPasskeyParams = {
5858
passkeyDisplayName?: string;
5959
expirationSeconds?: string;
6060
challenge?: string;
61+
62+
// TODO: (breaking change): remove organizationId from here, there is literally
63+
// no reason to have it
6164
organizationId?: string;
6265
};
6366

@@ -109,6 +112,10 @@ export type InitOtpParams = {
109112
export type VerifyOtpParams = {
110113
otpId: string;
111114
otpCode: string;
115+
116+
// TODO (breaking change): we should be able to remove these and make verifyOtp()
117+
// purely about verifying and not also finding an `organizationId`. That should
118+
// be the responsibility of completeOtp()
112119
contact: string;
113120
otpType: OtpType;
114121
};
@@ -158,14 +165,16 @@ export type CompleteOauthParams = {
158165
export type LoginWithOauthParams = {
159166
oidcToken: string;
160167
publicKey: string;
168+
organizationId?: string;
161169
invalidateExisting?: boolean;
162170
sessionKey?: string;
163171
};
164172

165173
export type SignUpWithOauthParams = {
166174
oidcToken: string;
167175
publicKey: string;
168-
providerName: string;
176+
providerName?: string;
177+
invalidateExisting?: boolean;
169178
createSubOrgParams?: CreateSubOrgParams;
170179
sessionKey?: string;
171180
};

packages/react-wallet-kit/src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ export function parseOAuthRedirect(hash: string): {
210210
}
211211
}
212212

213-
// Function to generate PKCE challenge pair for Facebook OAuth
213+
// Function to generate PKCE challenge pair
214214
export async function generateChallengePair(): Promise<{
215215
verifier: string;
216216
codeChallenge: string;

0 commit comments

Comments
 (0)