How inji/inji-openid4vp-ios-swift works
1. Repository purpose
- This is a Swift Package implementing the OpenID for Verifiable Presentations (OpenID4VP) wallet-side flow.
- It is not an app; it is a reusable library that validates verifier authorization requests, builds verifiable presentation responses, encrypts/signs where needed, and optionally posts results back to the verifier.
2. Top-level façade
- Sources/OpenID4VP/OpenID4VP.swift is the main entry point.
- The public API is centered around a wallet instance initialized with traceabilityId and optional walletMetadata.
- All major flows hang off this façade: authenticate verifier, construct unsigned VP tokens, construct/send VP responses, and send error responses.
3. Authorization request subsystem
- AuthorizationRequest/ handles decoding and validation of incoming verifier requests.
- It supports both request-by-value and request-by-reference (request_uri) and validates draft 21 vs draft 23 rules.
- The request handler is split by client_id_scheme into concrete handlers for did, pre-registered, and redirect_uri, with a shared ClientIdSchemeBasedAuthorizationRequestHandler orchestrating selection.
4. Verifier trust and client validation
- Trusted verifier configuration is represented by dto/Verifier.swift.
- The request pipeline compares client_id and response_uri against the trusted verifier list, and optionally checks request signatures using the verifier’s jwks_uri.
- This boundary is important in diagrams because it is the main trust gate before any presentation processing happens.
5. Metadata and presentation-definition model
- AuthorizationRequest/ClientMetadata and WalletMetadata.swift model the capability exchange between wallet and verifier.
- PresentationDefinition/ contains the object model for input descriptors, constraints, fields, filters, and limit-disclosure semantics.
- The library uses this metadata both to validate incoming requests and to shape what the wallet can advertise back to the verifier when request_uri_method=post.
6. Authorization response construction
- AuthorizationResponse/ turns selected credentials into a response payload aligned to the request’s response mode.
- The response side has two main layers:
- VPToken/ builds concrete verifiable presentation tokens for supported formats.
- PresentationSubmission/ maps selected credentials back to the verifier’s input descriptors.
- The output is either an unencrypted body with vp_token plus presentation_submission, or an encrypted wrapper containing the response payload.
7. Unsigned token and signing workflow
- The library exposes two token construction paths:
- constructUnsignedVPToken / constructVPResponse for the older, format-grouped flow.
- constructUnsignedVPTokenV2 / constructVPResponseV2 for the newer flattened signing flow.
- Format-specific implementations exist for ldp_vc, mso_mdoc, sd_jwt, and vc+sd-jwt.
- This is a key architectural boundary: token construction is separate from wallet signing, so a wallet can sign externally and feed signatures back in.
8. Format-specific token implementations
- Under AuthorizationResponse/VPToken/types and UnsignedVPToken/types, each credential family has its own token model and builder.
- LDP VC uses proof/JWS-related structures, mdoc uses device-authentication data, and SD-JWT variants carry their own signing material.
- These submodules are where credential-format differences are isolated from the generic OpenID4VP orchestration.
9. Response modes and transport
- ResponseModeHandler/ selects how the response is delivered based on response_mode.
- Supported paths include direct_post, direct_post.jwt, and the iar-post variants described in the README.
- The handlers decide whether the response is plain form-urlencoded content or an encrypted JWE payload before transport.
10. Crypto and JWT layer
- jwt/ contains the cryptographic primitives used by the response pipeline.
- JWS handles signing-related JWT operations, while JWE handles encryption, including AES-GCM and ECDH-ES/X25519 key agreement support.
- KeyResolver/ resolves public keys from DID-based identifiers (did:jwk, did:key, did:web) for request verification and related cryptographic operations.
11. Network boundary
- NetworkManager/ is the only explicit outbound HTTP layer.
- It is used for fetching request_uri content and for posting VP responses or error responses to the verifier’s response_uri.
- This creates a clean boundary between protocol logic and transport concerns.
12. Shared utilities and validation
- Common/ holds reusable infrastructure: Base64url helpers, JSON encoding, CBOR utilities, nonce/UUID generation, field validation, and generic conversion helpers.
- These utilities support both request parsing and response construction and keep low-level data handling out of the protocol-specific modules.
13. Constants and error model
- Constants/ centralizes protocol enums and identifiers such as client-id schemes, response modes, signature/encryption algorithms, and DID methods.
- Exceptions/ defines the library’s error taxonomy, and the README shows that errors are often mapped into verifier-facing error responses.
- For diagrams, this is the control-plane layer that constrains and validates the whole flow.
14. Tests and coverage
- Tests/OpenID4VPTests/ mirrors the source structure closely, with dedicated tests for request handling, response mode selection, token builders, crypto utilities, key resolution, and network behavior.
- The test layout reflects the architecture: each major subsystem is validated independently, which reinforces the library’s modular boundaries.
15. End-to-end data flow
- The main flow is: verifier request enters the façade, request parsing/authentication validates trust and capabilities, selected credentials are transformed into unsigned VP token(s), wallet signing occurs, the signed result is packaged into a VP response, and the response is optionally POSTed to the verifier.
- Error handling can branch off at multiple points, and the same network boundary is reused to notify the verifier of failures when possible.
Main entry points: AuthorizationRequest.swift Auth Request · AuthorizationResponseHandler.swift Response Handler · AuthorizationResponse.swift Auth Response · ClientIdSchemeBasedAuthorizationRequestHandler.swift Scheme Router
Components 23
Public API
Wallet-facing entrypoints and shared protocol model
- OpenID4VP · Main wallet-facing API for request handling and response construction
Sources/OpenID4VP/OpenID4VP.swift
Authorization Request
Decode, validate, and trust incoming verifier requests
- Auth Request · Entry point for parsing and validating verifier authorization requests
Sources/OpenID4VP/AuthorizationRequest/AuthorizationRequest.swift - Scheme Router · Selects the client_id_scheme-specific request handler
Sources/OpenID4VP/AuthorizationRequest/AuthorizationRequestHandler/ClientIdSchemeBasedAuthorizationRequestHandler.swift - DID Handler · Validates did-based authorization requests
Sources/OpenID4VP/AuthorizationRequest/AuthorizationRequestHandler/types/DidSchemeAuthorizationRequestHandler.swift - Pre-reg Handler · Validates pre-registered client requests
Sources/OpenID4VP/AuthorizationRequest/AuthorizationRequestHandler/types/PreRegisteredSchemeAuthorizationRequestHandler.swift - Redirect Handler · Validates redirect_uri-based authorization requests
Sources/OpenID4VP/AuthorizationRequest/AuthorizationRequestHandler/types/RedirectUriSchemeAuthorizationRequestHandler.swift - Client Metadata · Verifier capability metadata exchanged during authorization
Sources/OpenID4VP/AuthorizationRequest/ClientMetadata/ClientMetadata.swift - Wallet Metadata · Wallet capabilities advertised to the verifier
Sources/OpenID4VP/AuthorizationRequest/WalletMetadata.swift - Presentation Def · Input descriptors, constraints, filters, and disclosure rules
Sources/OpenID4VP/AuthorizationRequest/PresentationDefinition/PresentationDefinition.swift
Authorization Response
Build VP tokens, submissions, and response payloads
- Auth Response · Builds VP response payloads and error responses
Sources/OpenID4VP/AuthorizationResponse/AuthorizationResponse.swift - Response Handler · Coordinates response payload assembly for the selected mode
Sources/OpenID4VP/AuthorizationResponse/AuthorizationResponseHandler.swift - Presentation Submission · Maps selected credentials back to input descriptors
Sources/OpenID4VP/AuthorizationResponse/PresentationSubmission/PresentationSubmission.swift - VP Token Factory · Creates format-specific VP token builders and models
Sources/OpenID4VP/AuthorizationResponse/VPToken/VPTokenFactory.swift - Unsigned Tokens · Constructs unsigned VP token variants before wallet signing
Sources/OpenID4VP/AuthorizationResponse/UnsignedVPToken/UnsignedVPToken.swift - Signing Result · Carries wallet-produced signatures into the response flow
Sources/OpenID4VP/AuthorizationResponse/VPTokenSigningResult/VpTokenSigningResult.swift
Crypto & Keys
JWT, JWE, signing, encryption, and key resolution
- JWS / JWE · Signing and encryption primitives used by response packaging
Sources/OpenID4VP/jwt/JWE/JWEHandler.swift - Key Resolver · Resolves public keys from DID-based identifiers
Sources/OpenID4VP/jwt/KeyResolver/PublicKeyResolver.swift
Infrastructure
Networking, common utilities, constants, and errors
- Verifier · Trusted verifier configuration used as the trust gate
Sources/OpenID4VP/dto/Verifier.swift - Response Mode · Selects direct post and JWT response transport behavior
Sources/OpenID4VP/ResponseModeHandler/ResponseModeBasedHandler.swift - Network · Outbound HTTP for request_uri fetches and verifier posts
Sources/OpenID4VP/NetworkManager/NetworkManager.swift - Common Utils · Shared helpers for encoding, validation, nonce, UUID, and CBOR
Sources/OpenID4VP/Common/Utils.swift - Constants · Protocol enums and identifiers used across the library
Sources/OpenID4VP/Constants/ResponseMode.swift - Errors · Library error taxonomy and protocol-facing failures
Sources/OpenID4VP/Exceptions/OpenId4VPExceptions.swift
Connections 27
- OpenID4VP → Auth Request: authenticate request · The façade enters the verifier request pipeline
- OpenID4VP → Auth Response: build response · The façade exposes VP response construction
- OpenID4VP → Network: send error/response · The façade can post verifier-facing outcomes
- Auth Request → Scheme Router: dispatch · Routes by client_id_scheme
- Scheme Router → DID Handler: did · DID-based validation path
- Scheme Router → Pre-reg Handler: pre-registered · Trusted pre-registered client path
- Scheme Router → Redirect Handler: redirect_uri · Redirect URI client path
- Auth Request → Verifier: trust gate · Matches client_id and response_uri against trusted verifier config
- Auth Request → Client Metadata: read metadata · Consumes verifier metadata and request capabilities
- Auth Request → Presentation Def: parse definition · Validates requested presentation constraints
- Auth Request → Network: fetch request_uri · Retrieves request-by-reference content
- Auth Request → JWS / JWE: verify request sig · Uses JWT crypto to check signed requests when present
- Auth Response → Response Handler: orchestrate · Coordinates response assembly
- Response Handler → Unsigned Tokens: construct unsigned · Builds unsigned VP token payloads
- Response Handler → VP Token Factory: select format · Chooses the supported credential-format implementation
- Response Handler → Presentation Submission: map selection · Creates descriptor mapping for chosen credentials
- Response Handler → Signing Result: accept signature · Ingests wallet-produced signing material
- Response Handler → Response Mode: apply mode · Determines transport and wrapper form
- Response Mode → Network: post result · Sends direct_post and related verifier payloads
- Response Handler → JWS / JWE: encrypt/sign · Wraps the response when encryption or JWT packaging is required
- JWS / JWE → Key Resolver: resolve keys · Fetches DID-based public keys for crypto operations
- Auth Request → Common Utils: validate/parse · Shares decoding and low-level helpers
- Auth Response → Common Utils: encode/build · Uses shared utilities for payload construction
- Auth Request → Constants: protocol rules · Relies on protocol enums and identifiers
- Auth Response → Constants: protocol rules · Uses protocol enums and identifiers
- Auth Request → Errors: raise failures · Protocol validation failures map to the error taxonomy
- Auth Response → Errors: emit failures · Response construction and transport errors use the same taxonomy