|
7 | 7 | Rust implementation of the OpenID for Verifiable Presentations (OID4VP) specification. |
8 | 8 |
|
9 | 9 | <!-- cargo-rdme start --> |
| 10 | + |
| 11 | +This library provides a Rust implementation of [OID4VP 1.0]. |
| 12 | + |
| 13 | +[OID4VP 1.0]: <https://openid.net/specs/openid-4-verifiable-presentations-1_0.html> |
| 14 | + |
| 15 | +## Verifier Usage |
| 16 | + |
| 17 | +You can create a verifier implementation using the [`Verifier`] type as follows: |
| 18 | + |
| 19 | +```rust |
| 20 | +use openid4vp::verifier::Verifier; |
| 21 | +use openid4vp::core::dcql_query::{DcqlQuery, DcqlCredentialQuery}; |
| 22 | +use openid4vp::core::credential_format::ClaimFormatDesignation; |
| 23 | +use openid4vp::core::authorization_request::parameters::{ResponseMode, ResponseType}; |
| 24 | +use openid4vp::utils::NonEmptyVec; |
| 25 | + |
| 26 | +// Setup verifier. |
| 27 | +let verifier = Verifier::builder() |
| 28 | + .with_client(client) |
| 29 | + .with_session_store(session_store) |
| 30 | + .with_submission_endpoint(submission_url) |
| 31 | + .build() |
| 32 | + .await?; |
| 33 | + |
| 34 | +// Build a DCQL query for credential requirements. |
| 35 | +let credential_query = DcqlCredentialQuery::new( |
| 36 | + "mdl_credential".to_string(), |
| 37 | + ClaimFormatDesignation::MsoMDoc, |
| 38 | +); |
| 39 | +let dcql_query = DcqlQuery::new(NonEmptyVec::new(credential_query)); |
| 40 | + |
| 41 | +// Build an authorization request. |
| 42 | +let (uuid, request_url) = verifier |
| 43 | + .build_authorization_request() |
| 44 | + .with_dcql_query(dcql_query) |
| 45 | + .with_request_parameter(ResponseType::VpToken) |
| 46 | + .with_request_parameter(ResponseMode::DirectPost) |
| 47 | + .build(wallet_metadata) |
| 48 | + .await?; |
| 49 | + |
| 50 | +// Present the request to the wallet (e.g., as a QR code). |
| 51 | +let qr_code = generate_qr_code(&request_url)?; |
| 52 | + |
| 53 | +// Poll for the response status. |
| 54 | +let status = verifier.poll_status(uuid).await?; |
| 55 | + |
| 56 | +// When the wallet responds, verify the response. |
| 57 | +verifier.verify_response(uuid, authorization_response, |session, response| { |
| 58 | + Box::pin(async move { |
| 59 | + // Validate the VP token and verify claims. |
| 60 | + validate_vp_token(session, response).await |
| 61 | + }) |
| 62 | +}).await?; |
| 63 | +``` |
| 64 | + |
| 65 | +The verifier's behavior can be customized by implementing the [`Client`] and |
| 66 | +[`SessionStore`] traits. |
| 67 | + |
| 68 | +[`Verifier`]: https://docs.rs/openid4vp/latest/openid4vp/verifier/struct.Verifier.html |
| 69 | +[`Client`]: https://docs.rs/openid4vp/latest/openid4vp/verifier/client/trait.Client.html |
| 70 | +[`SessionStore`]: https://docs.rs/openid4vp/latest/openid4vp/verifier/session/trait.SessionStore.html |
| 71 | + |
| 72 | +## Wallet Usage |
| 73 | + |
| 74 | +Wallets can be implemented by using the [`Wallet`] trait: |
| 75 | + |
| 76 | +```rust |
| 77 | +use openid4vp::wallet::Wallet; |
| 78 | +use openid4vp::core::authorization_request::verification::RequestVerifier; |
| 79 | +use openid4vp::core::authorization_request::AuthorizationRequestObject; |
| 80 | +use openid4vp::core::metadata::WalletMetadata; |
| 81 | +use openid4vp::core::response::{AuthorizationResponse, UnencodedAuthorizationResponse}; |
| 82 | +use openid4vp::core::response::parameters::VpToken; |
| 83 | +use anyhow::Result; |
| 84 | +use async_trait::async_trait; |
| 85 | + |
| 86 | +struct MyWallet { |
| 87 | + metadata: WalletMetadata, |
| 88 | + http_client: MyHttpClient, |
| 89 | +} |
| 90 | + |
| 91 | +#[async_trait] |
| 92 | +impl Wallet for MyWallet { |
| 93 | + type HttpClient = MyHttpClient; |
| 94 | + |
| 95 | + fn metadata(&self) -> &WalletMetadata { |
| 96 | + &self.metadata |
| 97 | + } |
| 98 | + |
| 99 | + fn http_client(&self) -> &Self::HttpClient { |
| 100 | + &self.http_client |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Implement RequestVerifier for client identifier schemes per OID4VP Section 5.9 |
| 105 | +#[async_trait] |
| 106 | +impl RequestVerifier for MyWallet { |
| 107 | + async fn redirect_uri( |
| 108 | + &self, |
| 109 | + decoded_request: &AuthorizationRequestObject, |
| 110 | + request_jwt: Option<String>, |
| 111 | + ) -> Result<()> { |
| 112 | + // Verify redirect_uri scheme requests (unsigned JWTs per Section 5.9.3) |
| 113 | + Ok(()) |
| 114 | + } |
| 115 | + |
| 116 | + async fn decentralized_identifier( |
| 117 | + &self, |
| 118 | + decoded_request: &AuthorizationRequestObject, |
| 119 | + request_jwt: Option<String>, |
| 120 | + ) -> Result<()> { |
| 121 | + // Verify DID-based requests (signed with DID key per Section 5.9.3) |
| 122 | + Ok(()) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Parse and validate an authorization request. |
| 127 | +let request_object = wallet.validate_request(request_url).await?; |
| 128 | + |
| 129 | +// Create VP token with credentials matching the DCQL query. |
| 130 | +let vp_token = create_vp_token_for_request(&request_object)?; |
| 131 | +let response = AuthorizationResponse::Unencoded( |
| 132 | + UnencodedAuthorizationResponse::new(vp_token) |
| 133 | +); |
| 134 | + |
| 135 | +// Submit the authorization response. |
| 136 | +wallet.submit_response(request_object, response).await?; |
| 137 | +``` |
| 138 | + |
| 139 | +[`Wallet`]: https://docs.rs/openid4vp/latest/openid4vp/wallet/trait.Wallet.html |
| 140 | + |
| 141 | +## Protocol Overview |
| 142 | + |
| 143 | +Here is a simplified overview of the OID4VP protocol, referencing the |
| 144 | +various types and methods implementing it. |
| 145 | + |
| 146 | +### Authorization Request |
| 147 | + |
| 148 | +1. *Verifier creates request*: The verifier creates an [`AuthorizationRequest`] |
| 149 | + containing a DCQL query specifying what credentials are required. |
| 150 | +2. *Request delivery*: The request is delivered to the wallet, typically via QR code, |
| 151 | + deep link, or by reference. |
| 152 | +3. *Wallet validates request*: The wallet validates the request signature and metadata |
| 153 | + using a [`RequestVerifier`] implementation. |
| 154 | + |
| 155 | +All the code related to Authorization Requests is located in the |
| 156 | +[`core::authorization_request`] module. |
| 157 | + |
| 158 | +[`AuthorizationRequest`]: https://docs.rs/openid4vp/latest/openid4vp/core/authorization_request/struct.AuthorizationRequest.html |
| 159 | +[`RequestVerifier`]: https://docs.rs/openid4vp/latest/openid4vp/core/authorization_request/verification/trait.RequestVerifier.html |
| 160 | +[`core::authorization_request`]: https://docs.rs/openid4vp/latest/openid4vp/core/authorization_request/ |
| 161 | + |
| 162 | +### Credential Selection and Presentation |
| 163 | + |
| 164 | +4. *Wallet processes query*: The wallet evaluates the DCQL query against available credentials. |
| 165 | +5. *User consent*: The wallet presents matching credentials to the user for consent. |
| 166 | +6. *VP creation*: The wallet creates a Verifiable Presentation containing the |
| 167 | + selected credentials. |
| 168 | +7. *Response submission*: The wallet submits an [`AuthorizationResponse`] containing |
| 169 | + the VP token and presentation submission. |
| 170 | + |
| 171 | +All the code related to Authorization Responses is located in the |
| 172 | +[`core::response`] module. |
| 173 | + |
| 174 | +[`AuthorizationResponse`]: https://docs.rs/openid4vp/latest/openid4vp/core/response/enum.AuthorizationResponse.html |
| 175 | +[`core::response`]: https://docs.rs/openid4vp/latest/openid4vp/core/response/ |
| 176 | + |
| 177 | +### Verification |
| 178 | + |
| 179 | +8. *Verifier validates response*: The verifier validates the VP token signatures |
| 180 | + and checks the presentation submission against the original request. |
| 181 | +9. *Claim extraction and verification*: The verifier extracts and validates the |
| 182 | + requested claims from the credentials. |
| 183 | + |
| 184 | +## Credential Formats |
| 185 | + |
| 186 | +This library supports the credential formats defined in OID4VP v1.0 Appendix B: |
| 187 | +- **JWT VC** (`jwt_vc_json`): W3C Verifiable Credentials secured with JWT |
| 188 | +- **LDP VC** (`ldp_vc`): W3C Verifiable Credentials with Linked Data Proofs |
| 189 | +- **SD-JWT VC** (`dc+sd-jwt`): IETF SD-JWT Verifiable Credentials |
| 190 | +- **mso_mdoc** (`mso_mdoc`): ISO/IEC 18013-5 mobile documents (mDL, etc.) |
| 191 | + |
| 192 | +Format identifiers and handling are defined in the [`core::credential_format`] module. |
| 193 | + |
| 194 | +[`core::credential_format`]: https://docs.rs/openid4vp/latest/openid4vp/core/credential_format/ |
| 195 | + |
| 196 | +## Query Language |
| 197 | + |
| 198 | +This library uses **DCQL** (Digital Credentials Query Language) for specifying credential |
| 199 | +requirements. DCQL is the native query language of OID4VP v1.0 (Section 6) and provides |
| 200 | +precise control over credential selection, including support for: |
| 201 | +- Multiple credential formats in a single query |
| 202 | +- OR/AND logic via `credential_sets` |
| 203 | +- Selective disclosure with `claims` constraints |
| 204 | +- Trusted issuer specification via `trusted_authorities` |
| 205 | +- Value matching with `values` arrays |
| 206 | + |
| 207 | +DCQL support is provided in the [`core::dcql_query`] module. |
| 208 | + |
| 209 | +[`core::dcql_query`]: https://docs.rs/openid4vp/latest/openid4vp/core/dcql_query/ |
| 210 | + |
10 | 211 | <!-- cargo-rdme end --> |
11 | 212 |
|
12 | 213 | ## Install |
|
0 commit comments