Skip to content

Commit

Permalink
WIP waas-playfab
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav committed Jun 18, 2024
1 parent 24116d8 commit 855ebd0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
65 changes: 49 additions & 16 deletions packages/waas/src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { Observer, SequenceWaaSBase } from './base'
import { IntentDataOpenSession, IntentDataSendTransaction } from './clients/intent.gen'
import { IdentityType, IntentDataOpenSession, IntentDataSendTransaction } from './clients/intent.gen'
import { newSessionFromSessionId } from './session'
import { LocalStore, Store, StoreObj } from './store'
import {
GetTransactionReceiptArgs,
SendDelayedEncodeArgs,
SendERC1155Args,
SendERC20Args,
SendERC721Args,
SignMessageArgs,
SendTransactionsArgs,
SignedIntent,
GetTransactionReceiptArgs
SignMessageArgs
} from './intents'
import {
MaySentTransactionResponse,
SignedMessageResponse,
FeeOptionsResponse,
isCloseSessionResponse,
isFeeOptionsResponse,
isFinishValidateSessionResponse,
isGetSessionResponse,
isInitiateAuthResponse,
isIntentTimeError,
isMaySentTransactionResponse,
isSessionAuthProofResponse,
isSignedMessageResponse,
isValidationRequiredResponse,
isFinishValidateSessionResponse,
isCloseSessionResponse,
isTimedOutTransactionResponse,
isFeeOptionsResponse,
isSessionAuthProofResponse,
isIntentTimeError,
isInitiateAuthResponse
isValidationRequiredResponse,
MaySentTransactionResponse,
SignedMessageResponse
} from './intents/responses'
import { WaasAuthenticator, Session, Chain } from './clients/authenticator.gen'
import { jwtDecode } from 'jwt-decode'
import { Chain, Session, WaasAuthenticator } from './clients/authenticator.gen'
import { SimpleNetwork, WithSimpleNetwork } from './networks'
import { EmailAuth } from './email'
import { ethers } from 'ethers'
import { SubtleCryptoBackend, getDefaultSubtleCryptoBackend } from './subtle-crypto'
import { SecureStoreBackend, getDefaultSecureStoreBackend } from './secure-store'
import { getDefaultSubtleCryptoBackend, SubtleCryptoBackend } from './subtle-crypto'
import { getDefaultSecureStoreBackend, SecureStoreBackend } from './secure-store'
import { keccak256, toUtf8Bytes } from 'ethers/lib/utils'

export type Sessions = (Session & { isThis: boolean })[]

Expand Down Expand Up @@ -249,6 +249,39 @@ export class SequenceWaaS {
}
}

async signInWithPlayFab(titleID: string, sessionTicket: string, sessionName: string): Promise<SignInResponse> {
const ticketHash = keccak256(toUtf8Bytes(sessionTicket))
const verifier = `${titleID}|${ticketHash}`
const initiateAuth = await this.waas.initiateAuth(IdentityType.PlayFab, verifier)
const res = await this.sendIntent(initiateAuth)

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

const openSession = await this.waas.completeAuth(IdentityType.PlayFab, verifier, sessionTicket)
try {
const res = await this.registerSession(openSession, sessionName)

await this.waas.completeSignIn({
code: 'sessionOpened',
data: {
sessionId: res.session.id,
wallet: res.response.data.wallet
}
})

return {
sessionId: res.session.id,
wallet: res.response.data.wallet,
email: res.session.identity.email
}
} catch (e) {
await this.waas.completeSignOut()
throw e
}
}

async initiateEmailAuth(email: string) {
const intent = await this.waas.initiateEmailAuth(email)
const res = await this.sendIntent(intent)
Expand Down
26 changes: 26 additions & 0 deletions packages/waas/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ export class SequenceWaaSBase {
return this.signIntent(intent)
}

async initiateAuth(identityType: IdentityType, verifier: string): Promise<SignedIntent<IntentDataInitiateAuth>> {
const sessionId = await this.getSessionId()
const intent = await initiateAuth({
sessionId,
identityType,
verifier,
lifespan: DEFAULT_LIFESPAN
})

await this.status.set('pending')

return this.signIntent(intent)
}

async completeEmailAuth(email: string, challenge: string, answer: string): Promise<SignedIntent<IntentDataOpenSession>> {
const sessionId = await this.getSessionId()
const hashedAnswer = keccak256(toUtf8Bytes(challenge + answer))
Expand All @@ -279,6 +293,18 @@ export class SequenceWaaSBase {
return this.signIntent(intent)
}

async completeAuth(identityType: IdentityType, verifier: string, answer: string): Promise<SignedIntent<IntentDataOpenSession>> {
const sessionId = await this.getSessionId()
const intent = await openSession({
sessionId,
identityType,
verifier,
answer,
lifespan: DEFAULT_LIFESPAN
})
return this.signIntent(intent)
}

onSessionStateChanged(callback: Observer<string>): () => void {
this.sessionObservers.push(callback)
return () => {
Expand Down
10 changes: 6 additions & 4 deletions packages/waas/src/clients/intent.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
// sequence-waas-intents v0.1.0 aa4a9c749f45acce28fa4ed3fa6aae862ad54fbe
// sequence-waas-intents v0.1.0 5d94a08aafe21cad6518aa1b6ef38b6ca0a9e9c5
// --
// Code generated by [email protected].7 with typescript generator. DO NOT EDIT.
// Code generated by [email protected].8 with typescript generator. DO NOT EDIT.
//
// webrpc-gen -schema=intent.ridl -target=typescript -client -out=./intent.gen.ts

Expand All @@ -12,7 +12,7 @@ export const WebRPCVersion = 'v1'
export const WebRPCSchemaVersion = 'v0.1.0'

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = 'aa4a9c749f45acce28fa4ed3fa6aae862ad54fbe'
export const WebRPCSchemaHash = '5d94a08aafe21cad6518aa1b6ef38b6ca0a9e9c5'

//
// Types
Expand Down Expand Up @@ -75,7 +75,8 @@ export enum IdentityType {
None = 'None',
Guest = 'Guest',
OIDC = 'OIDC',
Email = 'Email'
Email = 'Email',
PlayFab = 'PlayFab'
}

export interface Intent {
Expand Down Expand Up @@ -171,6 +172,7 @@ export interface IntentDataGetTransactionReceipt {
export interface IntentDataFederateAccount {
sessionId: string
wallet: string
identityType: IdentityType
verifier?: string
answer?: string
}
Expand Down

0 comments on commit 855ebd0

Please sign in to comment.