hyperlane_plugin_auth/struct.rs
1use super::*;
2
3/// JWT JWS header decoded from the first segment of a compact token.
4#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
5pub struct JwtHeader {
6 /// JWS algorithm identifier (must be `"HS256"`).
7 pub(crate) alg: String,
8 /// Optional token type (`"JWT"`).
9 #[serde(skip_serializing_if = "Option::is_none")]
10 #[get(type(clone))]
11 pub(crate) typ: Option<String>,
12 /// Optional key identifier used for zero-downtime key rotation.
13 #[serde(skip_serializing_if = "Option::is_none")]
14 #[get(type(clone))]
15 pub(crate) kid: Option<String>,
16}
17
18/// Standard and extensible JSON Web Token claims payload.
19#[derive(Clone, Data, Debug, Default, Deserialize, PartialEq, Serialize)]
20pub struct JwtClaims {
21 /// Subject identifier (`sub`).
22 pub(crate) sub: String,
23 /// Optional issuer (`iss`).
24 #[serde(skip_serializing_if = "Option::is_none")]
25 #[get(type(clone))]
26 pub(crate) iss: Option<String>,
27 /// Optional audience (`aud`).
28 #[serde(skip_serializing_if = "Option::is_none")]
29 #[get(type(clone))]
30 pub(crate) aud: Option<String>,
31 /// Optional expiration Unix timestamp in seconds (`exp`).
32 #[serde(skip_serializing_if = "Option::is_none")]
33 #[get(type(copy))]
34 pub(crate) exp: Option<u64>,
35 /// Optional not-before Unix timestamp in seconds (`nbf`).
36 #[serde(skip_serializing_if = "Option::is_none")]
37 #[get(type(copy))]
38 pub(crate) nbf: Option<u64>,
39 /// Optional issued-at Unix timestamp in seconds (`iat`).
40 #[serde(skip_serializing_if = "Option::is_none")]
41 #[get(type(copy))]
42 pub(crate) iat: Option<u64>,
43 /// Optional unique JWT identifier (`jti`).
44 #[serde(skip_serializing_if = "Option::is_none")]
45 #[get(type(clone))]
46 pub(crate) jti: Option<String>,
47 /// Optional role list used for RBAC authorization checks.
48 #[serde(default, skip_serializing_if = "Vec::is_empty")]
49 pub(crate) roles: Vec<String>,
50 /// Arbitrary additional custom claims flattened into the JWT JSON object.
51 #[serde(flatten, default, skip_serializing_if = "HashMap::is_empty")]
52 pub(crate) extra: HashMap<String, Value>,
53}
54
55/// High-performance HS256 JWT signer and verifier with `kid` key-rotation support.
56#[derive(Clone, Debug)]
57pub struct JwtCodec {
58 /// Primary signing secret used when no `kid` is specified.
59 pub(crate) default_secret: Vec<u8>,
60 /// Optional default `kid` stamped into minted tokens.
61 pub(crate) default_kid: Option<String>,
62 /// Key rotation keyring indexed by `kid` (`XxHash3_64`).
63 pub(crate) keyring: HashMapXxHash3_64<String, Vec<u8>>,
64 /// Clock-skew leeway in seconds applied to `exp` and `nbf`.
65 pub(crate) leeway_secs: u64,
66 /// Optional required `iss` value.
67 pub(crate) required_issuer: Option<String>,
68 /// Optional required `aud` value.
69 pub(crate) required_audience: Option<String>,
70}
71
72/// Verified caller identity attached to a Hyperlane [`Context`] by [`AuthHook`].
73#[derive(Clone, Data, Debug, PartialEq)]
74pub struct AuthPrincipal {
75 /// Authenticated user or service subject identifier.
76 pub(crate) subject: String,
77 /// Granted RBAC roles.
78 pub(crate) roles: Vec<String>,
79 /// Scheme that authenticated this principal.
80 #[get(type(copy))]
81 pub(crate) scheme: AuthScheme,
82 /// Decoded JWT claims when authenticated via [`AuthScheme::BearerJwt`].
83 #[get(type(clone))]
84 pub(crate) claims: Option<JwtClaims>,
85}
86
87/// Built-in [`Authenticator`] validating `Authorization: Bearer <jwt>` via [`JwtCodec`].
88#[derive(Clone, Debug)]
89pub struct JwtAuthenticator {
90 /// Compiled JWT codec and validation policy.
91 pub(crate) codec: JwtCodec,
92}
93
94/// Built-in [`Authenticator`] validating static or rotated API keys from a configured header.
95#[derive(Clone, Debug)]
96pub struct ApiKeyAuthenticator {
97 /// Header name carrying the API key (default `X-Api-Key`).
98 pub(crate) header_name: String,
99 /// Registered `(api_key, subject, roles)` records scanned with constant-time comparison.
100 pub(crate) entries: Vec<(Vec<u8>, String, Vec<String>)>,
101}
102
103/// Built-in [`Authenticator`] validating `Authorization: Basic <base64>` credentials.
104#[derive(Clone, Debug)]
105pub struct BasicAuthenticator {
106 /// Registered `(username, password_bytes, roles)` records.
107 pub(crate) users: HashMapXxHash3_64<String, (Vec<u8>, Vec<String>)>,
108}
109
110/// Composite [`Authenticator`] trying a sequence of authenticators until one succeeds.
111#[derive(Clone)]
112pub struct CompositeAuthenticator {
113 /// Ordered authenticator chain.
114 pub(crate) chain: Vec<SharedAuthenticator>,
115}
116
117/// Configuration policy for [`AuthPlugin`] and [`AuthHook`].
118#[derive(Clone, Data, Debug, Eq, PartialEq)]
119pub struct AuthConfig {
120 /// Exact paths bypassed without authentication (for example `"/healthz"`, `"/login"`).
121 pub(crate) exempt_paths: Vec<String>,
122 /// Optional roles required on every protected request (any match permits if `require_all_roles` is `false`).
123 pub(crate) required_roles: Vec<String>,
124 /// When `true`, caller must hold every role in `required_roles`; when `false`, any single match suffices.
125 #[get(type(copy))]
126 pub(crate) require_all_roles: bool,
127 /// `WWW-Authenticate` header value emitted on `401 Unauthorized`.
128 pub(crate) www_authenticate: String,
129}
130
131/// Process-wide [`AuthPlugin`] singleton storing [`AuthConfig`] and the active [`Authenticator`].
132#[derive(Clone)]
133pub struct AuthPlugin {
134 /// Authorization and path-exclusion configuration.
135 pub(crate) config: AuthConfig,
136 /// Active authenticator implementation.
137 pub(crate) authenticator: SharedAuthenticator,
138}
139
140/// Opt-in [`ServerHook`] authenticating incoming requests and populating [`ATTR_AUTH_PRINCIPAL`].
141#[derive(Clone, Copy, Debug, Default)]
142#[hyperlane::request_middleware(-90)]
143pub struct AuthHook;