Skip to content

Commit 8078da4

Browse files
committed
fix(webauthn): to include scopes in the API
1 parent 925d8f4 commit 8078da4

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

packages/auth-services/src/auth-server/src/routes/pkp/mint.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@ import { addJob } from '../../../../queue-manager/src/bullmqSetup';
33
import { resp } from '../../response-helpers/response-helpers';
44
import { mintPkpDoc } from '../../../../queue-manager/src/handlers/pkpMint/pkpMint.doc';
55
import { AuthServiceMintRequestRaw } from '../../schemas/AuthServiceMintRequestSchema';
6+
import { randomUUID } from 'node:crypto';
67

78
export const mint = (app: ElysiaInstance) => {
89
app.post(
910
'/mint',
1011
async ({ body }: { body: AuthServiceMintRequestRaw }) => {
12+
const reqId = randomUUID();
13+
// console.log('[PKP Mint][INBOUND]', {
14+
// reqId,
15+
// authMethodType: body.authMethodType,
16+
// authMethodId: body.authMethodId,
17+
// pubkey_len: body.pubkey?.length ?? 0,
18+
// pubkey_is_0x: body.pubkey === '0x',
19+
// pubkey_preview: (body.pubkey ?? '').slice(0, 12),
20+
// scopes: body.scopes,
21+
// });
22+
1123
try {
1224
const job = await addJob('pkpMint', { requestBody: body });
13-
return resp.QUEUED(job.id, 'PKP minting request queued successfully.');
25+
return resp.QUEUED(job.id, `PKP mint queued. reqId=${reqId}`);
1426
} catch (error: any) {
1527
console.error(`[API] Failed to add job 'pkpMint' to queue:`, error);
1628
return resp.ERROR(

packages/auth-services/src/auth-server/src/schemas/AuthServiceMintRequestSchema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type AuthServiceMintRequestTransformed = z.infer<
2828
export const tAuthServiceMintRequestSchema = t.Object({
2929
authMethodType: t.String(),
3030
authMethodId: t.String(),
31-
pubkey: t.Optional(t.String({ default: '0x' })),
31+
pubkey: t.Optional(t.String()),
3232
scopes: t.Optional(
3333
t.Array(
3434
t.Union([

packages/auth-services/src/queue-manager/src/handlers/pkpMint/pkpMint.doc.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ export const mintPkpDoc = {
3131
),
3232
pubkey: t.Optional(
3333
t.String({
34-
default: '0x',
3534
description:
36-
"Public key associated with the authentication method. This is primarily used for WebAuthn, where it should be the public key obtained from the WebAuthn registration process. For other authentication types, if this field is omitted or an empty string is provided, it will default to '0x'. If explicitly providing for non-WebAuthn, use '0x'.",
35+
'For WebAuthn (type 3), pubkey is required and must be the COSE key. For other types, omit it.',
3736
})
3837
),
3938
scopes: t.Optional(

packages/auth-services/src/queue-manager/src/handlers/pkpMint/pkpMint.handler.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,42 @@ export async function handlePkpMintTask(jobData: {
1414
pubkey: Hex;
1515
scopes?: ('sign-anything' | 'personal-sign' | 'no-permissions')[];
1616
};
17+
reqId?: string;
1718
}): Promise<any> {
19+
// console.log('[PKP Mint][HANDLER][REQ]', {
20+
// reqId: jobData.reqId,
21+
// authMethodType: jobData.requestBody.authMethodType,
22+
// authMethodId: jobData.requestBody.authMethodId,
23+
// pubkey_len: jobData.requestBody.pubkey?.length ?? 0,
24+
// pubkey_is_0x: jobData.requestBody.pubkey === '0x',
25+
// pubkey_preview: (jobData.requestBody.pubkey ?? '').slice(0, 12),
26+
// scopes: jobData.requestBody.scopes,
27+
// });
28+
29+
if (
30+
// AUTH_METHOD_TYPE.WebAuthn = 3
31+
Number(jobData.requestBody.authMethodType) === 3 &&
32+
(!jobData.requestBody.pubkey || jobData.requestBody.pubkey === '0x')
33+
) {
34+
throw new Error(
35+
`[PKP Mint][HANDLER] WebAuthn requires a non-empty COSE pubkey; got '${jobData.requestBody.pubkey}'. reqId=${jobData.reqId}`
36+
);
37+
}
38+
1839
const userAuthData: Optional<AuthData, 'accessToken'> = {
1940
authMethodId: jobData.requestBody.authMethodId,
2041
authMethodType: Number(jobData.requestBody.authMethodType),
2142
publicKey: jobData.requestBody.pubkey,
2243
};
2344

45+
// console.log('[PKP Mint][HANDLER][MAPPING]', {
46+
// reqId: jobData.reqId,
47+
// authMethodType: userAuthData.authMethodType,
48+
// authMethodId: userAuthData.authMethodId,
49+
// publicKey_len: userAuthData.publicKey?.length ?? 0,
50+
// publicKey_preview: (userAuthData.publicKey ?? '').slice(0, 12),
51+
// });
52+
2453
const result = await globalThis.systemContext.litClient.mintWithAuth({
2554
account: globalThis.systemContext.account,
2655
authData: userAuthData,

packages/auth/src/lib/authenticators/native/WebAuthnAuthenticator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export class WebAuthnAuthenticator {
149149
public static async registerAndMintPKP(params: {
150150
username?: string;
151151
authServiceBaseUrl: string;
152+
scopes?: ('sign-anything' | 'personal-sign' | 'no-permissions')[];
152153
}): Promise<{
153154
pkpInfo: PKPData;
154155

@@ -183,6 +184,7 @@ export class WebAuthnAuthenticator {
183184
authMethodType: AUTH_METHOD_TYPE.WebAuthn,
184185
authMethodId: authMethodId,
185186
pubkey: authMethodPubkey,
187+
scopes: params.scopes,
186188
};
187189

188190
// Immediate mint a new PKP to associate with the auth method

packages/lit-client/src/lib/LitClient/createLitClient.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,15 @@ export const _createNagaLitClient = async (
346346
currentHandshakeResult.threshold
347347
);
348348

349+
// console.log('custom session key result:', result);
350+
// console.log('custom request id:', requestId);
351+
349352
// 4. 🟪 Handle response
350353
return await networkModule.api.signCustomSessionKey.handleResponse(
351354
result as any,
352355
params.requestBody.pkpPublicKey,
353-
jitContext
356+
jitContext,
357+
requestId
354358
);
355359
}
356360

packages/networks/src/networks/vNaga/shared/managers/LitChainClient/apis/highLevelApis/mintPKP/MintPKPSchema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export const MintPKPSchema = z
1717

1818
// Determine pubkey based on the (potentially derived) authMethodType
1919
if (data.authMethodType === AUTH_METHOD_TYPE.WebAuthn) {
20-
if (!data.pubkey) {
21-
throw new Error('pubkey is required for WebAuthn');
20+
if (!data.pubkey || data.pubkey === '0x') {
21+
throw new Error(
22+
`pubkey is required for WebAuthn and cannot be 0x. Received pubkey: "${data.pubkey}" and authMethodType: ${data.authMethodType}`
23+
);
2224
}
2325
derivedPubkey = data.pubkey as Hex;
2426
} else {

0 commit comments

Comments
 (0)