Skip to content

Commit 0c23375

Browse files
committed
Add support for custom auth providers (authcode & authcode-pkce only)
1 parent f59aeb9 commit 0c23375

File tree

7 files changed

+89
-26
lines changed

7 files changed

+89
-26
lines changed

packages/wallet/wdk/src/dbs/auth-commitments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const TABLE_NAME = 'auth-commitments'
55

66
export type AuthCommitment = {
77
id: string
8-
kind: 'google-pkce' | 'apple'
8+
kind: 'google-pkce' | 'apple' | `custom-${string}`
99
metadata: { [key: string]: string }
1010
verifier?: string
1111
challenge?: string

packages/wallet/wdk/src/sequence/handlers/authcode-pkce.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import { AuthCodeHandler } from './authcode.js'
88

99
export class AuthCodePkceHandler extends AuthCodeHandler implements Handler {
1010
constructor(
11-
signupKind: 'google-pkce',
11+
signupKind: 'google-pkce' | `custom-${string}`,
1212
issuer: string,
13+
oauthUrl: string,
1314
audience: string,
1415
nitro: Identity.IdentityInstrument,
1516
signatures: Signatures,
1617
commitments: Db.AuthCommitments,
1718
authKeys: Db.AuthKeys,
1819
) {
19-
super(signupKind, issuer, audience, nitro, signatures, commitments, authKeys)
20+
super(signupKind, issuer, oauthUrl, audience, nitro, signatures, commitments, authKeys)
2021
}
2122

2223
public async commitAuth(target: string, isSignUp: boolean, state?: string, signer?: string) {
@@ -50,8 +51,7 @@ export class AuthCodePkceHandler extends AuthCodeHandler implements Handler {
5051
state,
5152
})
5253

53-
const oauthUrl = this.oauthUrl()
54-
return `${oauthUrl}?${searchParams.toString()}`
54+
return `${this.oauthUrl}?${searchParams.toString()}`
5555
}
5656

5757
public async completeAuth(

packages/wallet/wdk/src/sequence/handlers/authcode.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ export class AuthCodeHandler extends IdentityHandler implements Handler {
1111
protected redirectUri: string = ''
1212

1313
constructor(
14-
public readonly signupKind: 'apple' | 'google-pkce',
14+
public readonly signupKind: 'apple' | 'google-pkce' | `custom-${string}`,
1515
public readonly issuer: string,
16+
protected readonly oauthUrl: string,
1617
public readonly audience: string,
1718
nitro: Identity.IdentityInstrument,
1819
signatures: Signatures,
@@ -48,12 +49,11 @@ export class AuthCodeHandler extends IdentityHandler implements Handler {
4849
client_id: this.audience,
4950
redirect_uri: this.redirectUri,
5051
response_type: 'code',
51-
scope: 'openid',
52+
scope: 'openid profile email',
5253
state,
5354
})
5455

55-
const oauthUrl = this.oauthUrl()
56-
return `${oauthUrl}?${searchParams.toString()}`
56+
return `${this.oauthUrl}?${searchParams.toString()}`
5757
}
5858

5959
public async completeAuth(
@@ -100,15 +100,4 @@ export class AuthCodeHandler extends IdentityHandler implements Handler {
100100
},
101101
}
102102
}
103-
104-
protected oauthUrl() {
105-
switch (this.issuer) {
106-
case 'https://accounts.google.com':
107-
return 'https://accounts.google.com/o/oauth2/v2/auth'
108-
case 'https://appleid.apple.com':
109-
return 'https://appleid.apple.com/auth/authorize'
110-
default:
111-
throw new Error('unsupported-issuer')
112-
}
113-
}
114103
}

packages/wallet/wdk/src/sequence/manager.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ export type ManagerOptions = {
7979
enabled: boolean
8080
clientId: string
8181
}
82+
customProviders?: {
83+
kind: `custom-${string}`
84+
authMethod: 'id-token' | 'authcode' | 'authcode-pkce'
85+
issuer: string
86+
oauthUrl: string
87+
clientId: string
88+
}[]
8289
}
8390
}
8491

@@ -466,6 +473,7 @@ export class Manager {
466473
new AuthCodePkceHandler(
467474
'google-pkce',
468475
'https://accounts.google.com',
476+
'https://accounts.google.com/o/oauth2/v2/auth',
469477
ops.identity.google.clientId,
470478
identityInstrument,
471479
modules.signatures,
@@ -480,6 +488,7 @@ export class Manager {
480488
new AuthCodeHandler(
481489
'apple',
482490
'https://appleid.apple.com',
491+
'https://appleid.apple.com/auth/authorize',
483492
ops.identity.apple.clientId,
484493
identityInstrument,
485494
modules.signatures,
@@ -488,6 +497,46 @@ export class Manager {
488497
),
489498
)
490499
}
500+
if (ops.identity.customProviders?.length) {
501+
for (const provider of ops.identity.customProviders) {
502+
switch (provider.authMethod) {
503+
case 'id-token':
504+
throw new Error('id-token is not supported yet')
505+
case 'authcode':
506+
shared.handlers.set(
507+
provider.kind,
508+
new AuthCodeHandler(
509+
provider.kind,
510+
provider.issuer,
511+
provider.oauthUrl,
512+
provider.clientId,
513+
identityInstrument,
514+
modules.signatures,
515+
shared.databases.authCommitments,
516+
shared.databases.authKeys,
517+
),
518+
)
519+
break
520+
case 'authcode-pkce':
521+
shared.handlers.set(
522+
provider.kind,
523+
new AuthCodePkceHandler(
524+
provider.kind,
525+
provider.issuer,
526+
provider.oauthUrl,
527+
provider.clientId,
528+
identityInstrument,
529+
modules.signatures,
530+
shared.databases.authCommitments,
531+
shared.databases.authKeys,
532+
),
533+
)
534+
break
535+
default:
536+
throw new Error('unsupported auth method')
537+
}
538+
}
539+
}
491540

492541
shared.modules = modules
493542
this.shared = shared

packages/wallet/wdk/src/sequence/signers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export function isWitnessExtraSignerKind(extra: any): extra is WitnessExtraSigne
88
}
99

1010
function toKnownKind(kind: string): Kind {
11-
if (Object.values(Kinds).includes(kind as Kind)) {
11+
if (kind.startsWith('custom-')) {
12+
return kind as Kind
13+
}
14+
15+
if (Object.values(Kinds).includes(kind as (typeof Kinds)[keyof typeof Kinds])) {
1216
return kind as Kind
1317
}
1418

packages/wallet/wdk/src/sequence/types/signer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const Kinds = {
1212
Unknown: 'unknown',
1313
} as const
1414

15-
export type Kind = (typeof Kinds)[keyof typeof Kinds]
15+
export type Kind = (typeof Kinds)[keyof typeof Kinds] | `custom-${string}`
1616

1717
export type WitnessExtraSignerKind = {
1818
signerKind: string

packages/wallet/wdk/src/sequence/wallets.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { PasskeysHandler } from './handlers/passkeys.js'
1414
import { GuardRole } from './guards.js'
1515

1616
export type StartSignUpWithRedirectArgs = {
17-
kind: 'google-pkce' | 'apple'
17+
kind: 'google-pkce' | 'apple' | `custom-${string}`
1818
target: string
1919
metadata: { [key: string]: string }
2020
}
@@ -55,7 +55,7 @@ export type CompleteRedirectArgs = CommonSignupArgs & {
5555
}
5656

5757
export type AuthCodeSignupArgs = CommonSignupArgs & {
58-
kind: 'google-pkce' | 'apple'
58+
kind: 'google-pkce' | 'apple' | `custom-${string}`
5959
commitment: AuthCommitment
6060
code: string
6161
target: string
@@ -693,10 +693,30 @@ export class Wallets implements WalletsInterface {
693693
}
694694
}
695695
}
696+
697+
if (args.kind.startsWith('custom-')) {
698+
// TODO: support other custom auth methods (e.g. id-token)
699+
const handler = this.shared.handlers.get(args.kind) as AuthCodeHandler
700+
if (!handler) {
701+
throw new Error('handler-not-registered')
702+
}
703+
704+
const [signer, metadata] = await handler.completeAuth(args.commitment, args.code)
705+
return {
706+
signer,
707+
extra: {
708+
signerKind: args.kind,
709+
},
710+
loginEmail: metadata.email,
711+
}
712+
}
713+
714+
throw new Error('invalid-signup-kind')
696715
}
697716

698717
async startSignUpWithRedirect(args: StartSignUpWithRedirectArgs) {
699-
const handler = this.shared.handlers.get('login-' + args.kind) as AuthCodeHandler
718+
const kind = args.kind.startsWith('custom-') ? args.kind : 'login-' + args.kind
719+
const handler = this.shared.handlers.get(kind) as AuthCodeHandler
700720
if (!handler) {
701721
throw new Error('handler-not-registered')
702722
}
@@ -721,7 +741,8 @@ export class Wallets implements WalletsInterface {
721741
use4337: args.use4337,
722742
})
723743
} else {
724-
const handler = this.shared.handlers.get('login-' + commitment.kind) as AuthCodeHandler
744+
const kind = commitment.kind.startsWith('custom-') ? commitment.kind : 'login-' + commitment.kind
745+
const handler = this.shared.handlers.get(kind) as AuthCodeHandler
725746
if (!handler) {
726747
throw new Error('handler-not-registered')
727748
}

0 commit comments

Comments
 (0)