Skip to content

Commit

Permalink
waas: support getIdToken intent
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Jul 23, 2024
1 parent 4a57939 commit 748c59a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/waas/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { Observer, SequenceWaaSBase } from './base'
import { Account, IdentityType, IntentDataOpenSession, IntentDataSendTransaction } from './clients/intent.gen'
import {
Account,
IdentityType,
IntentDataOpenSession,
IntentDataSendTransaction,
IntentResponseIdToken
} from './clients/intent.gen'
import { newSessionFromSessionId } from './session'
import { LocalStore, Store, StoreObj } from './store'
import {
Expand All @@ -17,6 +23,7 @@ import {
isCloseSessionResponse,
isFeeOptionsResponse,
isFinishValidateSessionResponse,
isGetIdTokenResponse,
isGetSessionResponse,
isInitiateAuthResponse,
isIntentTimeError,
Expand Down Expand Up @@ -564,6 +571,17 @@ export class SequenceWaaS {
await this.sendIntent(intent)
}

async getIdToken(args?: { nonce?: string }): Promise<IntentResponseIdToken> {
const intent = await this.waas.getIdToken({ nonce: args?.nonce })
const res = await this.sendIntent(intent)

if (!isGetIdTokenResponse(res)) {
throw new Error(`Invalid response: ${JSON.stringify(res)}`)
}

return res.data
}

async useIdentifier<T extends CommonAuthArgs>(args: T): Promise<T & { identifier: string }> {
if (args.identifier) {
return args as T & { identifier: string }
Expand Down
16 changes: 16 additions & 0 deletions packages/waas/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
combineTransactionIntents,
feeOptions,
finishValidateSession,
getIdToken,
getSession,
getTransactionReceipt,
GetTransactionReceiptArgs,
Expand Down Expand Up @@ -573,6 +574,21 @@ export class SequenceWaaSBase {
return this.signIntent(intent)
}

async getIdToken({ nonce }: { nonce?: string }) {
const sessionId = await this.sessionId.get()
if (!sessionId) {
throw new Error('session not open')
}

const intent = getIdToken({
wallet: await this.getWalletAddress(),
lifespan: DEFAULT_LIFESPAN,
sessionId,
nonce
})
return this.signIntent(intent)
}

async batch(intents: Intent<IntentDataSendTransaction>[]): Promise<SignedIntent<IntentDataSendTransaction>> {
const combined = combineTransactionIntents(intents)
return this.signIntent(combined)
Expand Down
11 changes: 11 additions & 0 deletions packages/waas/src/intents/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IntentResponseAuthInitiated,
IntentResponseCode,
IntentResponseGetSession,
IntentResponseIdToken,
IntentResponseValidationFinished,
IntentResponseValidationStarted
} from '../clients/intent.gen'
Expand Down Expand Up @@ -128,6 +129,7 @@ export type FinishValidateSessionResponse = Response<IntentResponseCode.validati
export type GetSessionResponse = Response<IntentResponseCode.getSessionResponse, IntentResponseGetSession>
export type LinkAccountResponse = Response<IntentResponseCode.accountFederated, IntentResponseAccountFederated>
export type ListAccountsResponse = Response<IntentResponseCode.accountList, IntentResponseAccountList>
export type IdTokenResponse = Response<IntentResponseCode.idToken, IntentResponseIdToken>

export function isInitiateAuthResponse(receipt: any): receipt is InitiateAuthResponse {
return (
Expand Down Expand Up @@ -277,3 +279,12 @@ export function isIntentTimeError(error: any): error is WebrpcEndpointError {
error.cause?.endsWith('intent is invalid: intent issued in the future'))
)
}

export function isGetIdTokenResponse(receipt: any): receipt is IdTokenResponse {
return (
typeof receipt === 'object' &&
receipt.code === IntentResponseCode.idToken &&
typeof receipt.data === 'object' &&
typeof receipt.data.idToken === 'string'
)
}
7 changes: 7 additions & 0 deletions packages/waas/src/intents/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IntentDataValidateSession,
IntentDataSessionAuthProof,
IntentDataInitiateAuth,
IntentDataGetIdToken,
IntentName
} from '../clients/intent.gen'

Expand Down Expand Up @@ -62,3 +63,9 @@ export type SessionAuthProof = BaseArgs & IntentDataSessionAuthProof
export function sessionAuthProof({ lifespan, ...data }: SessionAuthProof): Intent<IntentDataSessionAuthProof> {
return makeIntent(IntentName.sessionAuthProof, lifespan, data)
}

export type GetIdTokenArgs = BaseArgs & IntentDataGetIdToken

export function getIdToken({ lifespan, ...data }: GetIdTokenArgs): Intent<IntentDataGetIdToken> {
return makeIntent(IntentName.getIdToken, lifespan, data)
}

0 comments on commit 748c59a

Please sign in to comment.