Skip to content

Commit 90883d6

Browse files
committed
feat: return google URL instead of nonce
1 parent 0f5d253 commit 90883d6

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

packages/wallets/wallet-base/src/types/strategy.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export interface TurnkeyMetadata {
8585
otpInitPath?: string
8686
otpVerifyPath?: string
8787
oauthLoginPath?: string
88+
googleClientId?: string
89+
googleRedirectUri?: string
8890
}
8991

9092
export interface WalletMetadata {

packages/wallets/wallet-turnkey/src/strategy/strategy/base.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ import { TurnkeyOauthWallet } from '../turnkey/oauth.js'
3333
import { SessionType } from '@turnkey/sdk-browser'
3434
import { TurnkeyIframeClient } from '@turnkey/sdk-browser'
3535
import { getAddress } from 'viem'
36-
37-
const DEFAULT_TURNKEY_API_ENDPOINT = 'https://api.ui.injective.network/api/v1'
36+
import { generateGoogleUrl } from '../../utils.js'
3837

3938
export class BaseTurnkeyWalletStrategy
4039
extends BaseConcreteStrategy
@@ -55,9 +54,15 @@ export class BaseTurnkeyWalletStrategy
5554
super(args)
5655

5756
this.turnkeyProvider = args.provider
58-
this.client = new HttpRestClient(
59-
args.apiServerEndpoint || DEFAULT_TURNKEY_API_ENDPOINT,
60-
)
57+
58+
const endpoint =
59+
args.apiServerEndpoint || this.metadata?.turnkey?.apiServerEndpoint
60+
61+
if (!endpoint) {
62+
throw new WalletException(new Error('apiServerEndpoint is required'))
63+
}
64+
65+
this.client = new HttpRestClient(endpoint)
6166
}
6267

6368
async getWalletDeviceType(): Promise<WalletDeviceType> {
@@ -253,7 +258,26 @@ export class BaseTurnkeyWalletStrategy
253258

254259
throw new WalletException(new Error('Oauth result not found'))
255260
} else {
256-
return await TurnkeyOauthWallet.generateOAuthNonce(iframeClient)
261+
const nonce = await TurnkeyOauthWallet.generateOAuthNonce(iframeClient)
262+
263+
if (
264+
!metadata?.turnkey?.googleClientId ||
265+
!metadata?.turnkey?.googleRedirectUri
266+
) {
267+
throw new WalletException(
268+
new Error('googleClientId and googleRedirectUri are required'),
269+
)
270+
}
271+
272+
if (turnkeyProvider === TurnkeyProvider.Google) {
273+
return generateGoogleUrl({
274+
nonce,
275+
clientId: metadata.turnkey.googleClientId,
276+
redirectUri: metadata.turnkey.googleRedirectUri,
277+
})
278+
}
279+
//? When we add Apple support, we might also want to return the URL as well
280+
return nonce
257281
}
258282
}
259283

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function generateGoogleUrl({
2+
nonce,
3+
clientId,
4+
redirectUri,
5+
scope = 'openid profile email',
6+
prompt = 'consent',
7+
}: {
8+
nonce: string
9+
clientId: string
10+
redirectUri: string
11+
scope?: string
12+
prompt?: string
13+
}) {
14+
if (!clientId) {
15+
throw new Error('Google client ID not found')
16+
}
17+
18+
const responseType = 'id_token'
19+
20+
return `https://accounts.google.com/o/oauth2/v2/auth?prompt=${prompt}&client_id=${clientId}&redirect_uri=${redirectUri}&response_type=${responseType}&scope=${scope}&nonce=${nonce}`
21+
}

0 commit comments

Comments
 (0)