authkestra_engine/token/
jwk.rs1use crate::auth::error::AuthError;
2use jsonwebtoken::DecodingKey;
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Jwk {
7 pub kid: Option<String>,
8 pub kty: String,
9 pub alg: Option<String>,
10 pub n: Option<String>,
11 pub e: Option<String>,
12}
13
14impl Jwk {
15 pub fn to_decoding_key(&self) -> Result<DecodingKey, AuthError> {
16 if self.kty != "RSA" {
17 return Err(AuthError::Token(
18 "Only RSA keys are supported currently".to_string(),
19 ));
20 }
21
22 let n = self
23 .n
24 .as_ref()
25 .ok_or_else(|| AuthError::Token("Missing 'n' component in JWK".to_string()))?;
26 let e = self
27 .e
28 .as_ref()
29 .ok_or_else(|| AuthError::Token("Missing 'e' component in JWK".to_string()))?;
30
31 DecodingKey::from_rsa_components(n, e).map_err(|e| AuthError::Token(e.to_string()))
32 }
33}