Skip to content

Commit

Permalink
fix: await session state updates
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Mar 11, 2024
1 parent 7a221bb commit 963fb88
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('issuerCallback', () => {
})

const nonces = new MemoryStates<CNonceState>()
nonces.set('test_value', { cNonce: 'test_value', createdAt: +new Date(), issuerState: 'existing-state' })
await nonces.set('test_value', { cNonce: 'test_value', createdAt: +new Date(), issuerState: 'existing-state' })
vcIssuer = new VcIssuerBuilder<DIDDocument>()
.withAuthorizationServer('https://authorization-server')
.withCredentialEndpoint('https://credential-endpoint')
Expand Down
19 changes: 7 additions & 12 deletions packages/issuer/lib/VcIssuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ import {
toUniformCredentialOfferRequest,
TYP_ERROR,
UniformCredentialRequest,
URIState
URIState,
} from '@sphereon/oid4vci-common'
import { CompactSdJwtVc, CredentialMapper, W3CVerifiableCredential } from '@sphereon/ssi-types'
import { v4 } from 'uuid'

import { assertValidPinNumber, createCredentialOfferObject, createCredentialOfferURIFromObject } from './functions'
import { LookupStateManager } from './state-manager'
import {
CredentialDataSupplier,
CredentialDataSupplierArgs,
CredentialIssuanceInput,
CredentialSignerCallback
} from './types'
import { CredentialDataSupplier, CredentialDataSupplierArgs, CredentialIssuanceInput, CredentialSignerCallback } from './types'

const SECOND = 1000

Expand Down Expand Up @@ -350,17 +345,17 @@ export class VcIssuer<DIDDoc extends object> {
throw new Error(CREDENTIAL_MISSING_ERROR)
}
// remove the previous nonce
this.cNonces.delete(cNonceState.cNonce)
await this.cNonces.delete(cNonceState.cNonce)

if (preAuthorizedCode && preAuthSession) {
preAuthSession.lastUpdatedAt = +new Date()
preAuthSession.status = IssueStatus.CREDENTIAL_ISSUED
this._credentialOfferSessions.set(preAuthorizedCode, preAuthSession)
await this._credentialOfferSessions.set(preAuthorizedCode, preAuthSession)
} else if (issuerState && authSession) {
// If both were set we used the pre auth flow above as well, hence the else if
authSession.lastUpdatedAt = +new Date()
authSession.status = IssueStatus.CREDENTIAL_ISSUED
this._credentialOfferSessions.set(issuerState, authSession)
await this._credentialOfferSessions.set(issuerState, authSession)
}

return {
Expand Down Expand Up @@ -390,7 +385,7 @@ export class VcIssuer<DIDDoc extends object> {
preAuthSession.lastUpdatedAt = +new Date()
preAuthSession.status = IssueStatus.ERROR
preAuthSession.error = error instanceof Error ? error.message : error?.toString()
this._credentialOfferSessions.set(preAuthorizedCode, preAuthSession)
await this._credentialOfferSessions.set(preAuthorizedCode, preAuthSession)
}
}
if (issuerState) {
Expand All @@ -399,7 +394,7 @@ export class VcIssuer<DIDDoc extends object> {
authSession.lastUpdatedAt = +new Date()
authSession.status = IssueStatus.ERROR
authSession.error = error instanceof Error ? error.message : error?.toString()
this._credentialOfferSessions.set(issuerState, authSession)
await this._credentialOfferSessions.set(issuerState, authSession)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/issuer/lib/state-manager/LookupStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export class LookupStateManager<K extends StateType, V extends StateType> implem
}

async delete(id: string): Promise<boolean> {
return await this.assertedValueId(id).then((value) => {
this.keyValueMapper.delete(id)
return await this.assertedValueId(id).then(async (value) => {
await this.keyValueMapper.delete(id)
return this.valueStateManager.delete(value)
})
}
Expand All @@ -68,8 +68,8 @@ export class LookupStateManager<K extends StateType, V extends StateType> implem
}

async setMapped(id: string, keyValue: K, stateValue: V): Promise<void> {
this.keyValueMapper.set(id, keyValue)
this.valueStateManager.set(id, stateValue)
await this.keyValueMapper.set(id, keyValue)
await this.valueStateManager.set(id, stateValue)
}

async getAsserted(id: string): Promise<V> {
Expand Down
4 changes: 2 additions & 2 deletions packages/issuer/lib/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const assertValidAccessTokenRequest = async (
const credentialOfferSession = await credentialOfferSessions.getAsserted(request[PRE_AUTH_CODE_LITERAL])
credentialOfferSession.status = IssueStatus.ACCESS_TOKEN_REQUESTED
credentialOfferSession.lastUpdatedAt = +new Date()
credentialOfferSessions.set(request[PRE_AUTH_CODE_LITERAL], credentialOfferSession)
await credentialOfferSessions.set(request[PRE_AUTH_CODE_LITERAL], credentialOfferSession)
if (!isValidGrant(credentialOfferSession, request.grant_type)) {
throw new TokenError(400, TokenErrorResponse.invalid_grant, UNSUPPORTED_GRANT_TYPE_ERROR)
}
Expand Down Expand Up @@ -165,6 +165,6 @@ export const createAccessTokenResponse = async (
const credentialOfferSession = await credentialOfferSessions.getAsserted(preAuthorizedCode)
credentialOfferSession.status = IssueStatus.ACCESS_TOKEN_CREATED
credentialOfferSession.lastUpdatedAt = +new Date()
credentialOfferSessions.set(preAuthorizedCode, credentialOfferSession)
await credentialOfferSessions.set(preAuthorizedCode, credentialOfferSession)
return response
}

0 comments on commit 963fb88

Please sign in to comment.