{"version":3,"file":"OpenId4VcHolderApi.mjs","names":[],"sources":["../../src/openid4vc-holder/OpenId4VcHolderApi.ts"],"sourcesContent":["import {\n  AgentContext,\n  type DcqlQueryResult,\n  type DcqlSelectCredentialsForRequestOptions,\n  DcqlService,\n  type DifPexCredentialsForRequest,\n  DifPresentationExchangeService,\n  injectable,\n} from '@credo-ts/core'\nimport type { OpenId4VciMetadata } from '../shared'\nimport { OpenId4VciHolderService } from './OpenId4VciHolderService'\nimport type {\n  OpenId4VciAuthCodeFlowOptions,\n  OpenId4VciDeferredCredentialRequestOptions,\n  OpenId4VciCredentialRequestOptions as OpenId4VciRequestCredentialOptions,\n  OpenId4VciTokenRequestOptions as OpenId4VciRequestTokenOptions,\n  OpenId4VciRequestTokenResponse,\n  OpenId4VciResolvedCredentialOffer,\n  OpenId4VciRetrieveAuthorizationCodeUsingPresentationOptions,\n  OpenId4VciSendNotificationOptions,\n  OpenId4VciTokenRefreshOptions,\n  OpenId4VcParseAndVerifyAuthorizationResponseOptions,\n} from './OpenId4VciHolderServiceOptions'\nimport { OpenId4VpHolderService } from './OpenId4vpHolderService'\nimport type {\n  OpenId4VpAcceptAuthorizationRequestOptions,\n  ResolveOpenId4VpAuthorizationRequestOptions,\n} from './OpenId4vpHolderServiceOptions'\n\n/**\n * @public\n */\n@injectable()\nexport class OpenId4VcHolderApi {\n  public constructor(\n    private agentContext: AgentContext,\n    private openId4VciHolderService: OpenId4VciHolderService,\n    private openId4VpHolderService: OpenId4VpHolderService,\n    private difPresentationExchangeService: DifPresentationExchangeService,\n    private dcqlService: DcqlService\n  ) {}\n\n  /**\n   * Resolves the authentication request given as URI or JWT to a unified format, and\n   * verifies the validity of the request.\n   *\n   * The resolved request can be accepted with the @see acceptOpenId4VpAuthorizationRequest.\n   *\n   * If the authorization request uses OpenID4VP and included presentation definitions,\n   * a `presentationExchange` property will be defined with credentials that satisfy the\n   * incoming request. When `presentationExchange` is present, you MUST supply `presentationExchange`\n   * when calling `acceptOpenId4VpAuthorizationRequest` as well.\n   *\n   * @param request\n   * Can be:\n   * - JWT\n   * - URI containing request or request_uri param\n   * - Request payload\n   * @returns the resolved and verified authentication request.\n   */\n  public async resolveOpenId4VpAuthorizationRequest(\n    request: string | Record<string, unknown>,\n    options?: ResolveOpenId4VpAuthorizationRequestOptions\n  ) {\n    return this.openId4VpHolderService.resolveAuthorizationRequest(this.agentContext, request, options)\n  }\n\n  /**\n   * Accepts the authentication request after it has been resolved and verified with {@link resolveOpenId4VpAuthorizationRequest}.\n   *\n   * If the resolved authorization request included a `presentationExchange` property, you MUST supply `presentationExchange`\n   * in the `options` parameter. The same is true for `dcql`.\n   *\n   * For response mode of `direct_post` or `direct_post.jwt` the response will be submitted directly\n   * to the response url. For `dc_api` and `dc_api.jwt` the response will be returned but without a\n   * `serverResponse`, and you have to submit the response yourself.\n   */\n  public async acceptOpenId4VpAuthorizationRequest(options: OpenId4VpAcceptAuthorizationRequestOptions) {\n    return await this.openId4VpHolderService.acceptAuthorizationRequest(this.agentContext, options)\n  }\n\n  /**\n   * Automatically select credentials from available credentials for a presentation exchange request. Can be called after calling\n   * @see resolveOpenId4VpAuthorizationRequest.\n   */\n  public selectCredentialsForPresentationExchangeRequest(credentialsForRequest: DifPexCredentialsForRequest) {\n    return this.difPresentationExchangeService.selectCredentialsForRequest(credentialsForRequest)\n  }\n\n  /**\n   * Automatically select credentials from available credentials for a dcql request. Can be called after calling\n   * @see resolveOpenId4VpAuthorizationRequest.\n   */\n  public selectCredentialsForDcqlRequest(\n    dcqlQueryResult: DcqlQueryResult,\n    options?: DcqlSelectCredentialsForRequestOptions\n  ) {\n    return this.dcqlService.selectCredentialsForRequest(dcqlQueryResult, options)\n  }\n\n  public async resolveIssuerMetadata(credentialIssuer: string): Promise<OpenId4VciMetadata> {\n    return await this.openId4VciHolderService.resolveIssuerMetadata(this.agentContext, credentialIssuer)\n  }\n\n  /**\n   * Resolves a credential offer given as credential offer URL, or issuance initiation URL,\n   * into a unified format with metadata.\n   *\n   * @param credentialOffer the credential offer to resolve\n   * @returns The uniform credential offer payload, the issuer metadata, protocol version, and the offered credentials with metadata.\n   */\n  public async resolveCredentialOffer(credentialOffer: string) {\n    return await this.openId4VciHolderService.resolveCredentialOffer(this.agentContext, credentialOffer)\n  }\n\n  /**\n   * This function is to be used to receive an credential in OpenID4VCI using the Authorization Code Flow.\n   *\n   * Not to be confused with the {@link resolveOpenId4VpAuthorizationRequest}, which is only used for OpenID4VP requests.\n   *\n   * It will generate an authorization session based on the provided options.\n   *\n   * There are two possible flows:\n   * - Oauth2Redirect: an authorization request URI is returend which can be used to obtain the authorization code.\n   *   This needs to be done manually (e.g. by opening a browser window)\n   * - PresentationDuringIssuance: an openid4vp presentation request needs to be handled. A oid4vpRequestUri is returned\n   *   which can be parsed using `resolveOpenId4VpAuthorizationRequest`. After the presentation session has been completed,\n   *   the resulting `presentationDuringIssuanceSession` can be used to obtain an authorization code\n   *\n   * Authorization to request credentials can only be requested through scopes.\n   *\n   * @param resolvedCredentialOffer Obtained through @see resolveCredentialOffer\n   * @param authCodeFlowOptions\n   * @returns The authorization request URI alongside the code verifier and original @param authCodeFlowOptions\n   */\n  public async resolveOpenId4VciAuthorizationRequest(\n    resolvedCredentialOffer: OpenId4VciResolvedCredentialOffer,\n    authCodeFlowOptions: OpenId4VciAuthCodeFlowOptions\n  ) {\n    return await this.openId4VciHolderService.resolveAuthorizationRequest(\n      this.agentContext,\n      resolvedCredentialOffer,\n      authCodeFlowOptions\n    )\n  }\n\n  /**\n   * Retrieve an authorization code using an `presentationDuringIssuanceSession`.\n   *\n   * The authorization code can be exchanged for an `accessToken` @see requestToken\n   */\n  public async retrieveAuthorizationCodeUsingPresentation(\n    options: OpenId4VciRetrieveAuthorizationCodeUsingPresentationOptions\n  ) {\n    return await this.openId4VciHolderService.retrieveAuthorizationCodeUsingPresentation(this.agentContext, options)\n  }\n\n  public parseAuthorizationCodeFromAuthorizationResponse(options: OpenId4VcParseAndVerifyAuthorizationResponseOptions) {\n    return this.openId4VciHolderService.parseAuthorizationCodeFromAuthorizationResponse(this.agentContext, options)\n  }\n\n  /**\n   * Requests the token to be used for credential requests.\n   */\n  public async requestToken(options: OpenId4VciRequestTokenOptions): Promise<OpenId4VciRequestTokenResponse> {\n    const { accessTokenResponse, authorizationServer, dpop } = await this.openId4VciHolderService.requestAccessToken(\n      this.agentContext,\n      options\n    )\n\n    return {\n      accessToken: accessTokenResponse.access_token,\n      refreshToken: accessTokenResponse.refresh_token,\n      cNonce: accessTokenResponse.c_nonce,\n      authorizationServer,\n      dpop,\n      accessTokenResponse,\n    }\n  }\n\n  /**\n   * Requests the token to be used for credential requests.\n   */\n  public async refreshToken(\n    options: OpenId4VciTokenRefreshOptions\n  ): Promise<Omit<OpenId4VciRequestTokenResponse, 'authorizationServer'>> {\n    const { accessTokenResponse, dpop } = await this.openId4VciHolderService.refreshAccessToken(\n      this.agentContext,\n      options\n    )\n\n    return {\n      accessToken: accessTokenResponse.access_token,\n      refreshToken: accessTokenResponse.refresh_token,\n      cNonce: accessTokenResponse.c_nonce,\n      dpop,\n      accessTokenResponse,\n    }\n  }\n\n  /**\n   * Request a set of credentials from the credential issuer.\n   * Can be used with both the pre-authorized code flow and the authorization code flow.\n   */\n  public async requestCredentials(options: OpenId4VciRequestCredentialOptions) {\n    const { resolvedCredentialOffer, cNonce, accessToken, dpop, clientId, ...credentialRequestOptions } = options\n\n    return this.openId4VciHolderService.acceptCredentialOffer(this.agentContext, {\n      resolvedCredentialOffer,\n      acceptCredentialOfferOptions: credentialRequestOptions,\n      accessToken,\n      cNonce,\n      dpop,\n      clientId,\n    })\n  }\n\n  /**\n   * Request a set of deferred credentials from the credential issuer.\n   */\n  public async requestDeferredCredentials(options: OpenId4VciDeferredCredentialRequestOptions) {\n    return this.openId4VciHolderService.retrieveDeferredCredentials(this.agentContext, options)\n  }\n\n  /**\n   * Send a notification event to the credential issuer\n   */\n  public async sendNotification(options: OpenId4VciSendNotificationOptions) {\n    return this.openId4VciHolderService.send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