Skip to main content

authnz_common/types/tokens/
lmpaat.rs

1//! Light MPAAT module.
2
3use serde::{Deserialize, Serialize};
4
5#[cfg(any(feature = "pqc-utils", feature = "ed25519-utils"))]
6use crate::{MResult, ServerError};
7
8#[derive(Deserialize, Serialize, PartialEq, Eq, Hash, Clone)]
9/// LMPAAT header: unencrypted metadata.
10pub struct LightMPAATHeader<U> {
11  /// Client-side public key. May be empty if CBA is disabled.
12  pub cli_pkey: Vec<u8>,
13  #[serde(flatten)]
14  /// Common public fields.
15  pub common_public_fields: Option<U>,
16}
17
18#[derive(Deserialize, Serialize, PartialEq, Eq, Hash, Clone)]
19/// LMPAAT payload: unencrypted metadata.
20pub struct LightMPAATPayload<T> {
21  /// Token expiration timestamp.
22  pub exp: chrono::DateTime<chrono::Utc>,
23  #[serde(flatten)]
24  /// Payload container.
25  pub container: T,
26}
27
28#[derive(Deserialize, Serialize, PartialEq, Eq, Hash, Clone)]
29/// LMPAAT signature: unencrypted signature of header and payload.
30pub struct LightMPAATSignature {
31  /// Ed25519 signature.
32  pub sig: Vec<u8>,
33}
34
35#[cfg(any(feature = "pqc-utils", feature = "ed25519-utils"))]
36impl<U: serde::Serialize> LightMPAATHeader<U> {
37  /// Signs LMPAAT header and payload.
38  pub fn sign_lmpaat<T: serde::Serialize>(&self, payload: &LightMPAATPayload<T>, keypair: &crate::SignKeypair) -> MResult<Vec<u8>> {
39    let mut data = rmp_serde::to_vec(self).map_err(ServerError::from_private)?;
40    data.extend_from_slice(&rmp_serde::to_vec(payload).map_err(ServerError::from_private)?);
41    Ok(keypair.sign_raw(&data))
42  }
43}