{"version":3,"file":"OpenId4VcVerificationSessionRecord.mjs","names":[],"sources":["../../../src/openid4vc-verifier/repository/OpenId4VcVerificationSessionRecord.ts"],"sourcesContent":["import { BaseRecord, CredoError, DateTransformer, Jwt, type RecordTags, type TagsBase, utils } from '@credo-ts/core'\nimport type { OpenId4VpAuthorizationRequestPayload, OpenId4VpAuthorizationResponsePayload } from '../../shared/models'\nimport type { OpenId4VcVerificationSessionState } from '../OpenId4VcVerificationSessionState'\nimport type { OpenId4VpVersion } from '../OpenId4VpVerifierServiceOptions'\n\nexport type OpenId4VcVerificationSessionRecordTags = RecordTags<OpenId4VcVerificationSessionRecord>\n\nexport type DefaultOpenId4VcVerificationSessionRecordTags = {\n  verifierId: string\n  state: OpenId4VcVerificationSessionState\n  nonce: string\n  payloadState?: string\n  authorizationRequestUri?: string\n  authorizationRequestId?: string\n  openId4VpVersion?: OpenId4VpVersion\n}\n\nexport interface OpenId4VcVerificationSessionRecordProps {\n  id?: string\n  createdAt?: Date\n  tags?: TagsBase\n\n  verifierId: string\n  state: OpenId4VcVerificationSessionState\n  errorMessage?: string\n\n  authorizationRequestJwt?: string\n  authorizationRequestUri?: string\n  authorizationRequestId: string\n  authorizationRequestPayload?: OpenId4VpAuthorizationRequestPayload\n\n  authorizationResponseRedirectUri?: string\n\n  expiresAt: Date\n\n  authorizationResponsePayload?: OpenId4VpAuthorizationResponsePayload\n\n  /**\n   * Presentation during issuance session. This is used when issuance of a credential requires a presentation, and helps\n   * prevent session fixation attacks\n   */\n  presentationDuringIssuanceSession?: string\n\n  /**\n   * The version of openid4vp used for the request\n   */\n  openId4VpVersion: OpenId4VpVersion\n}\n\nexport class OpenId4VcVerificationSessionRecord extends BaseRecord<DefaultOpenId4VcVerificationSessionRecordTags> {\n  public static readonly type = 'OpenId4VcVerificationSessionRecord'\n  public readonly type = OpenId4VcVerificationSessionRecord.type\n\n  /**\n   * The id of the verifier that this session is for.\n   */\n  public verifierId!: string\n\n  /**\n   * The state of the verification session.\n   */\n  public state!: OpenId4VcVerificationSessionState\n\n  /**\n   * Optional error message of the error that occurred during the verification session. Will be set when state is {@link OpenId4VcVerificationSessionState.Error}\n   */\n  public errorMessage?: string\n\n  /**\n   * The signed JWT containing the authorization request\n   */\n  public authorizationRequestJwt?: string\n\n  /**\n   * Authorization request payload. This should be used only for unsigned requests\n   */\n  public authorizationRequestPayload?: OpenId4VpAuthorizationRequestPayload\n\n  /**\n   * URI of the authorization request. This is the url that can be used to\n   * retrieve the authorization request.\n   *\n   * Not used for requests with response_mode of dc_api or dc_api.jwt\n   */\n  public authorizationRequestUri?: string\n\n  /**\n   * The public id for the authorization request. This is used in the authorization\n   * request uri.\n   *\n   * @since 0.6\n   */\n  public authorizationRequestId?: string\n\n  /**\n   * The version of OpenID4VP used.\n   *\n   * If `v1` is used this is always defined. Otherwise it could be both\n   * `v1.draft21` or `v1.draft24`.\n   *\n   * You can detect this based on:\n   * - if `client_id_scheme` is defined -> `v1.draft21`\n   * - otherwise `v1.draft24`\n   *\n   * @since 0.6\n   */\n  public openId4VpVersion?: OpenId4VpVersion\n\n  /**\n   * The time at which the authorization request expires.\n   *\n   * @since 0.6\n   */\n  @DateTransformer()\n  public expiresAt?: Date\n\n  /**\n   * The payload of the received authorization response\n   */\n  public authorizationResponsePayload?: OpenId4VpAuthorizationResponsePayload\n\n  /**\n   * Presentation during issuance session. This is used when issuance of a credential requires a presentation, and helps\n   * prevent session fixation attacks\n   */\n  public presentationDuringIssuanceSession?: string\n\n  /**\n   * Redirect uri that should be used in the authorization response. This will be included in both error and success\n   * responses.\n   *\n   * @since 0.6\n   */\n  public authorizationResponseRedirectUri?: string\n\n  public constructor(props: OpenId4VcVerificationSessionRecordProps) {\n    super()\n\n    if (props) {\n      this.id = props.id ?? utils.uuid()\n      this.createdAt = props.createdAt ?? new Date()\n      this._tags = props.tags ?? {}\n\n      this.verifierId = props.verifierId\n      this.state = props.state\n      this.errorMessage = props.errorMessage\n      this.authorizationRequestPayload = props.authorizationRequestPayload\n      this.authorizationRequestJwt = props.authorizationRequestJwt\n      this.authorizationRequestUri = props.authorizationRequestUri\n      this.authorizationRequestId = props.authorizationRequestId\n      this.authorizationResponseRedirectUri = props.authorizationResponseRedirectUri\n      this.authorizationResponsePayload = props.authorizationResponsePayload\n      this.expiresAt = props.expiresAt\n      this.openId4VpVersion = props.openId4VpVersion\n\n      this.presentationDuringIssuanceSession = props.presentationDuringIssuanceSession\n    }\n  }\n\n  public get request(): string | OpenId4VpAuthorizationRequestPayload {\n    if (this.authorizationRequestJwt) return this.authorizationRequestJwt\n    if (this.authorizationRequestPayload) return this.authorizationRequestPayload\n\n    throw new CredoError('Unable to extract authorization payload from openid4vc session record')\n  }\n\n  public get requestPayload(): OpenId4VpAuthorizationRequestPayload {\n    if (this.authorizationRequestJwt)\n      return Jwt.fromSerializedJwt(\n        this.authorizationRequestJwt\n      ).payload.toJson() as OpenId4VpAuthorizationRequestPayload\n    if (this.authorizationRequestPayload) return this.authorizationRequestPayload\n\n    throw new CredoError('Unable to extract authorization payload from openid4vc session record')\n  }\n\n  public assertState(expectedStates: OpenId4VcVerificationSessionState | OpenId4VcVerificationSessionState[]) {\n    if (!Array.isArray(expectedStates)) {\n      expectedStates = [expectedStates]\n    }\n\n    if (!expectedStates.includes(this.state)) {\n      throw new CredoError(\n        `OpenId4VcVerificationSessionRecord is in invalid state ${this.state}. Valid states are: ${expectedStates.join(\n          ', '\n        )}.`\n      )\n    }\n  }\n\n  public getTags() {\n    const request = this.requestPayload\n\n    const nonce = request.nonce\n    const payloadState = 'state' in request ? (request.state as string) : undefined\n\n    return {\n      ...this._tags,\n      verifierId: this.verifierId,\n      state: this.state,\n      nonce,\n      payloadState,\n      authorizationRequestUri: this.authorizationRequestUri,\n      authorizationRequestId: this.authorizationRequestId,\n      openId4VpVersion: this.openId4VpVersion,\n    }\n  }\n}\n"],"mappings":";;;;;;AAiDA,IAAa,qCAAb,MAAa,2CAA2C,WAA0D;CAsFhH,AAAO,YAAY,OAAgD;AACjE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