aep-agent 0.2.0

Agent-side workflows for the Agent Enrollment Protocol.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use aep_core::{
    AUTHORIZATION_SCHEME, AgentStatus, BuiltInGrantResponse, Command, EnrollRequest,
    EnrollResponse, GrantRequest, GrantType, HttpRequest, MEDIA_TYPE, PROBLEM_MEDIA_TYPE,
    RevokeRequest, RevokeResponse, StatusResponse, StringBoolean, parse_built_in_grant_response,
    parse_enroll_response, parse_grant_request, parse_problem_details, parse_revoke_response,
    parse_status_response, validate_enroll_request, validate_revoke_request,
};
use http::{HeaderMap, HeaderValue, Method, header};
use serde::Serialize;
use serde_json::Value;
use time::{OffsetDateTime, format_description::well_known::Rfc3339};

use crate::http_response::media_type_matches;
use crate::{
    AgentError, AgentIdentity, AssertionSigner, CommandResult, CredentialRecord, EnrollOptions,
    GrantOptions, GrantResult, Inspection, OperationKey, RevokeOptions, Session, WaitOptions,
    assertion_operation,
};

impl Session {
    pub async fn enroll(
        &self,
        options: EnrollOptions,
    ) -> Result<CommandResult<EnrollResponse>, AgentError> {
        let (inspection, identity, signer) = self.command_context(&Command::Enroll, true).await?;
        let required = inspection
            .document
            .claims
            .as_ref()
            .map_or(&[][..], |claims| claims.required.as_slice());
        let missing = aep_core::missing_required_claim_names(required, options.claims.as_ref());
        if !missing.is_empty() {
            return Err(AgentError::claims(missing));
        }
        let key = self
            .idempotency_key(
                &inspection,
                &Command::Enroll,
                None,
                None,
                options.idempotency_key,
            )
            .await?;
        let body = EnrollRequest {
            agent_did: identity.agent_did.clone(),
            claims: options.claims,
            idempotency_key: Some(key.clone()),
            additional: Default::default(),
        };
        validate_enroll_request(&body).map_err(aep_core::CoreError::from)?;
        self.execute_command(
            CommandRequest {
                inspection: &inspection,
                identity: &identity,
                signer: signer.as_ref(),
                command: &Command::Enroll,
                method: Method::POST,
                body: Some(&body),
                idempotency_key: Some(&key),
            },
            parse_enroll_response,
        )
        .await
    }

    pub async fn status(&self) -> Result<CommandResult<StatusResponse>, AgentError> {
        let (inspection, identity, signer) = self.command_context(&Command::Status, true).await?;
        self.execute_command::<Value, StatusResponse>(
            CommandRequest {
                inspection: &inspection,
                identity: &identity,
                signer: signer.as_ref(),
                command: &Command::Status,
                method: Method::GET,
                body: None,
                idempotency_key: None,
            },
            parse_status_response,
        )
        .await
    }

    pub async fn wait_for_active(
        &self,
        options: WaitOptions,
    ) -> Result<CommandResult<StatusResponse>, AgentError> {
        if options.interval.is_zero() || options.timeout.is_zero() {
            return Err(AgentError::InvalidConfiguration(
                "AEP Status polling interval and timeout must be positive".to_owned(),
            ));
        }
        let started = self.client.clock.now();
        // The elapsed-time bound below is the one the caller asked for. It relies on the supplied
        // clock advancing as the supplied delay sleeps, so polling is also bounded by the number
        // of intervals that fit in the timeout; a clock that does not advance then stops here
        // instead of polling forever.
        let mut remaining_attempts = options
            .timeout
            .as_nanos()
            .checked_div(options.interval.as_nanos())
            .unwrap_or_default()
            .saturating_add(1);
        loop {
            let result = self.status().await?;
            if result.body.status == AgentStatus::Active {
                return Ok(result);
            }
            if matches!(
                result.body.status,
                AgentStatus::Rejected | AgentStatus::Suspended | AgentStatus::Terminated
            ) {
                return Err(AgentError::EnrollmentState {
                    status: result.body.status,
                });
            }
            let elapsed = self.client.clock.now() - started;
            let timeout = time::Duration::try_from(options.timeout).map_err(|_| {
                AgentError::InvalidConfiguration(
                    "AEP Status polling timeout is too large".to_owned(),
                )
            })?;
            remaining_attempts = remaining_attempts.saturating_sub(1);
            if elapsed >= timeout || remaining_attempts == 0 {
                return Err(AgentError::PollingTimeout);
            }
            self.client.delay.sleep(options.interval).await;
        }
    }

    pub async fn grant(
        &self,
        options: GrantOptions,
    ) -> Result<CommandResult<GrantResult>, AgentError> {
        let (inspection, identity, signer) = self.command_context(&Command::Grant, false).await?;
        let grant_type = select_grant_type(
            &inspection,
            options.grant_type.as_ref(),
            &options.preferred_grant_types,
        )?;
        if !inspection
            .document
            .commands
            .supported
            .contains(&Command::Status)
        {
            return Err(AgentError::CommandNotAdvertised(
                "status required by grant".to_owned(),
            ));
        }
        let status = self
            .execute_command::<Value, StatusResponse>(
                CommandRequest {
                    inspection: &inspection,
                    identity: &identity,
                    signer: signer.as_ref(),
                    command: &Command::Status,
                    method: Method::GET,
                    body: None,
                    idempotency_key: None,
                },
                parse_status_response,
            )
            .await?;
        if status.body.status != AgentStatus::Active {
            // Grant never implicitly enrolls, so an identity the Service has not activated is
            // refused before the request is sent. CORE-ERR-009 pairs `not_recognized` with 401,
            // which is the answer the Service would give.
            return Err(AgentError::Command {
                status: 401,
                problem: Some(Box::new(aep_core::new_problem_details(
                    aep_core::ErrorCode::NotRecognized,
                    "Grant requires an active enrollment",
                    401,
                ))),
            });
        }
        let key = self
            .idempotency_key(
                &inspection,
                &Command::Grant,
                Some(grant_type.clone()),
                None,
                options.idempotency_key,
            )
            .await?;
        let body = GrantRequest {
            grant_type: grant_type.clone(),
            requested_scopes: options.requested_scopes,
            additional: Default::default(),
        };
        parse_grant_request(&serde_json::to_vec(&body)?)?;
        let raw = self
            .execute_raw(CommandRequest {
                inspection: &inspection,
                identity: &identity,
                signer: signer.as_ref(),
                command: &Command::Grant,
                method: Method::POST,
                body: Some(&body),
                idempotency_key: Some(&key),
            })
            .await?;
        let value: Value = serde_json::from_slice(&raw.body)?;
        if !value.is_object() {
            return Err(AgentError::Credential(
                "AEP Grant response must be a JSON object".to_owned(),
            ));
        }
        if value
            .get("credential_id")
            .and_then(Value::as_str)
            .is_none_or(str::is_empty)
        {
            return Err(AgentError::Credential(
                "AEP Grant response must contain a non-empty credential_id".to_owned(),
            ));
        }
        let credential = match grant_type {
            GrantType::OAuthBearer | GrantType::ApiKey | GrantType::Basic => {
                Some(parse_built_in_grant_response(&grant_type, &raw.body)?)
            }
            GrantType::Other(_) => None,
        };
        if let Some(credential) = credential.as_ref() {
            self.client
                .credential_store
                .save(credential_record(
                    credential,
                    value.clone(),
                    &inspection,
                    self.client.clock.now(),
                )?)
                .await?;
        }
        Ok(CommandResult {
            body: GrantResult {
                credential,
                grant_type,
                raw: value,
            },
            status: raw.status,
            url: raw.url,
        })
    }

    pub async fn revoke(
        &self,
        options: RevokeOptions,
    ) -> Result<CommandResult<RevokeResponse>, AgentError> {
        if options.all_grant_types
            && (options.grant_type.is_some() || options.credential_id.is_some())
        {
            return Err(AgentError::InvalidConfiguration(
                "AEP all-grant-types Revoke cannot include a grant type or credential ID"
                    .to_owned(),
            ));
        }
        let (inspection, identity, signer) = self.command_context(&Command::Revoke, true).await?;
        // CORE-RVK-002: Revoke may only name a grant type the Service advertises.
        if let Some(grant_type) = options.grant_type.as_ref()
            && !inspection
                .document
                .commands
                .grant_types
                .contains(grant_type)
        {
            return Err(AgentError::NoCompatibleGrantType);
        }
        let body = RevokeRequest {
            grant_type: options.grant_type.clone(),
            credential_id: options.credential_id.clone(),
            all_grant_types: options.all_grant_types.then_some(StringBoolean::True),
            additional: Default::default(),
        };
        validate_revoke_request(&body).map_err(aep_core::CoreError::from)?;
        if body.credential_id.is_some() {
            let advertised = body
                .grant_type
                .as_ref()
                .and_then(|grant_type| {
                    inspection
                        .document
                        .commands
                        .grant_types_config
                        .get(grant_type.as_str())
                })
                .and_then(|config| config.supports_per_credential_revoke)
                == Some(StringBoolean::True);
            if !advertised {
                return Err(AgentError::Credential(
                    "AEP Service does not advertise per-credential Revoke".to_owned(),
                ));
            }
        }
        let key = self
            .idempotency_key(
                &inspection,
                &Command::Revoke,
                body.grant_type.clone(),
                body.credential_id.clone(),
                options.idempotency_key,
            )
            .await?;
        let result = self
            .execute_command(
                CommandRequest {
                    inspection: &inspection,
                    identity: &identity,
                    signer: signer.as_ref(),
                    command: &Command::Revoke,
                    method: Method::POST,
                    body: Some(&body),
                    idempotency_key: Some(&key),
                },
                parse_revoke_response,
            )
            .await?;
        self.delete_revoked_credentials(&inspection.document.service.did, &body)
            .await?;
        Ok(result)
    }

    async fn command_context(
        &self,
        command: &Command,
        create_identity: bool,
    ) -> Result<
        (
            Inspection,
            AgentIdentity,
            std::sync::Arc<dyn AssertionSigner>,
        ),
        AgentError,
    > {
        let inspection = self.inspect().await?;
        if !inspection.document.commands.supported.contains(command) {
            return Err(AgentError::CommandNotAdvertised(
                command.as_str().to_owned(),
            ));
        }
        let identity = self.resolve_identity(&inspection, create_identity).await?;
        let signer = self.client.identity_provider.signer_for(&identity).await?;
        Ok((inspection, identity, signer))
    }

    async fn execute_command<B: Serialize + ?Sized, T>(
        &self,
        request: CommandRequest<'_, B>,
        parser: fn(&[u8]) -> Result<T, aep_core::ParseError>,
    ) -> Result<CommandResult<T>, AgentError> {
        let raw = self.execute_raw(request).await?;
        Ok(CommandResult {
            body: parser(&raw.body)?,
            status: raw.status,
            url: raw.url,
        })
    }

    async fn execute_raw<B: Serialize + ?Sized>(
        &self,
        request: CommandRequest<'_, B>,
    ) -> Result<RawCommandResult, AgentError> {
        let url = request.inspection.command_url(request.command)?;
        let operation = assertion_operation(request.command)
            .ok_or_else(|| AgentError::CommandNotAdvertised(request.command.as_str().to_owned()))?;
        let assertion = self
            .client
            .sign_assertion(
                request.inspection,
                request.identity,
                request.signer,
                operation,
                None,
            )
            .await?;
        let mut headers = HeaderMap::new();
        headers.insert(header::ACCEPT, HeaderValue::from_static(MEDIA_TYPE));
        headers.insert(
            header::AUTHORIZATION,
            HeaderValue::from_str(&format!("{AUTHORIZATION_SCHEME} {assertion}")).map_err(
                |_| {
                    AgentError::Identity("AEP assertion is not a valid HTTP field value".to_owned())
                },
            )?,
        );
        let encoded = request
            .body
            .map(serde_json::to_vec)
            .transpose()?
            .unwrap_or_default();
        if request.body.is_some() {
            headers.insert(header::CONTENT_TYPE, HeaderValue::from_static(MEDIA_TYPE));
        }
        if let Some(key) = request.idempotency_key {
            headers.insert(
                "idempotency-key",
                HeaderValue::from_str(key).map_err(|_| {
                    AgentError::InvalidConfiguration(
                        "AEP idempotency key is not a valid HTTP field value".to_owned(),
                    )
                })?,
            );
        }
        let response = self
            .client
            .command_transport
            .send(HttpRequest {
                method: request.method,
                url: url.clone(),
                headers,
                body: encoded,
            })
            .await
            .map_err(|error| AgentError::Transport(error.to_string()))?;
        if response.final_url != url {
            return Err(AgentError::Transport(
                "AEP command redirects are not allowed".to_owned(),
            ));
        }
        if response.body.len() > self.client.maximum_response_bytes {
            return Err(AgentError::Transport(
                "AEP command response exceeds the configured limit".to_owned(),
            ));
        }
        if !response.status.is_success() {
            // CORE-ERR-001 serves an AEP error as Problem Details under its own media type, and
            // RFC 9457 carries the response status inside the document. A body that disagrees is
            // reported as the bare status rather than as a structured AEP problem.
            let problem = media_type_matches(&response.headers, PROBLEM_MEDIA_TYPE)
                .then(|| parse_problem_details(&response.body).ok())
                .flatten()
                .filter(|problem| problem.status == i64::from(response.status.as_u16()))
                .map(Box::new);
            return Err(AgentError::Command {
                status: response.status.as_u16(),
                problem,
            });
        }
        if !media_type_matches(&response.headers, MEDIA_TYPE) {
            return Err(AgentError::Transport(
                "AEP command response media type is invalid".to_owned(),
            ));
        }
        Ok(RawCommandResult {
            body: response.body,
            status: response.status.as_u16(),
            url,
        })
    }

    async fn idempotency_key(
        &self,
        inspection: &Inspection,
        command: &Command,
        grant_type: Option<GrantType>,
        credential_id: Option<String>,
        provided: Option<String>,
    ) -> Result<String, AgentError> {
        if let Some(value) = provided {
            // CORE-IDP-003 treats an empty field value as a missing key, and a value that is only
            // whitespace renders as one.
            if value.trim().is_empty() {
                return Err(AgentError::InvalidConfiguration(
                    "AEP idempotency key must not be empty".to_owned(),
                ));
            }
            return Ok(value);
        }
        let value = self
            .client
            .idempotency_keys
            .create_key(&OperationKey {
                command: command.clone(),
                credential_id,
                grant_type,
                service_did: inspection.document.service.did.clone(),
                service_url: self.service_url.clone(),
            })
            .await?;
        if value.trim().is_empty() {
            return Err(AgentError::InvalidConfiguration(
                "AEP idempotency key provider returned an empty key".to_owned(),
            ));
        }
        Ok(value)
    }

    async fn delete_revoked_credentials(
        &self,
        service_did: &str,
        selector: &RevokeRequest,
    ) -> Result<(), AgentError> {
        for record in self.client.credential_store.list(service_did).await? {
            let matches = selector.all_grant_types == Some(StringBoolean::True)
                || selector.credential_id.as_deref() == Some(record.credential_id.as_str())
                || (selector.credential_id.is_none()
                    && selector.grant_type.as_ref() == Some(&record.grant_type));
            if matches {
                self.client
                    .credential_store
                    .delete(service_did, &record.credential_id)
                    .await?;
            }
        }
        Ok(())
    }
}

struct RawCommandResult {
    body: Vec<u8>,
    status: u16,
    url: url::Url,
}

struct CommandRequest<'a, B: ?Sized> {
    inspection: &'a Inspection,
    identity: &'a AgentIdentity,
    signer: &'a dyn AssertionSigner,
    command: &'a Command,
    method: Method,
    body: Option<&'a B>,
    idempotency_key: Option<&'a str>,
}

fn select_grant_type(
    inspection: &Inspection,
    selected: Option<&GrantType>,
    preferred: &[GrantType],
) -> Result<GrantType, AgentError> {
    let advertised = &inspection.document.commands.grant_types;
    if let Some(selected) = selected {
        return advertised
            .contains(selected)
            .then(|| selected.clone())
            .ok_or(AgentError::NoCompatibleGrantType);
    }
    let candidates = if preferred.is_empty() {
        advertised
    } else {
        preferred
    };
    candidates
        .iter()
        .find(|candidate| advertised.contains(candidate))
        .cloned()
        .ok_or(AgentError::NoCompatibleGrantType)
}

fn credential_record(
    credential: &BuiltInGrantResponse,
    payload: Value,
    inspection: &Inspection,
    issued_at: OffsetDateTime,
) -> Result<CredentialRecord, AgentError> {
    let (credential_id, expires_at) = match credential {
        BuiltInGrantResponse::OAuthBearer(value) => (&value.credential_id, &value.expires_at),
        BuiltInGrantResponse::ApiKey(value) => (&value.credential_id, &value.expires_at),
        BuiltInGrantResponse::Basic(value) => (&value.credential_id, &value.expires_at),
    };
    let expires_at = OffsetDateTime::parse(expires_at, &Rfc3339).map_err(|_| {
        AgentError::Credential("AEP credential expiration is not RFC 3339".to_owned())
    })?;
    Ok(CredentialRecord {
        credential_id: credential_id.clone(),
        expires_at,
        grant_type: credential.grant_type(),
        issued_at,
        payload,
        service_did: inspection.document.service.did.clone(),
        service_url: inspection.service_url.clone(),
    })
}