1use fastauth_core::plugin_prelude::*;
4use jsonwebtoken::jwk::JwkSet;
5use serde_json::{Map, Value};
6
7use crate::verify::JwksVerifier;
8use crate::{Jwt, keys};
9
10#[derive(Clone, Debug, Default, Serialize, Deserialize)]
12pub struct Jwks {
13 pub keys: Vec<Map<String, Value>>,
14}
15
16#[derive(Clone, Copy, Debug, Default)]
19pub struct GetJwks;
20
21impl<D: Adapter> Endpoint<D> for GetJwks {
22 const METHOD: Method = Method::GET;
23 const PATH: &'static str = "/jwks";
24 type Access = Public;
25 type Input = Map<String, Value>;
26 type Output = Jwks;
27
28 async fn call(&self, ctx: Ctx<D>, _: Map<String, Value>) -> Result<Jwks, AuthError> {
29 let jwt = ctx.plugin::<Jwt>();
30 keys::current(&ctx, jwt).await?;
31 let all = keys::all(&ctx).await?;
32 let keys = keys::published(&all, jwt, ctx.now())
33 .into_iter()
34 .map(|key| keys::public_jwk(key, jwt))
35 .collect::<Result<_, _>>()?;
36 Ok(Jwks { keys })
37 }
38}
39
40#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
42pub struct TokenOutput {
43 pub token: String,
44}
45
46#[derive(Clone, Copy, Debug, Default)]
48pub struct GetToken;
49
50impl<D: Adapter> Endpoint<D> for GetToken {
51 const METHOD: Method = Method::GET;
52 const PATH: &'static str = "/token";
53 type Access = Authenticated;
54 type Input = Map<String, Value>;
55 type Output = TokenOutput;
56
57 async fn call(
58 &self,
59 ctx: Ctx<D, Authenticated>,
60 _: Map<String, Value>,
61 ) -> Result<TokenOutput, AuthError> {
62 let token = keys::session_token(&ctx, ctx.plugin::<Jwt>(), ctx.session()).await?;
63 Ok(TokenOutput { token })
64 }
65}
66
67#[derive(Clone, Debug, Default, Serialize, Deserialize)]
69pub struct SignJwtInput {
70 pub payload: Map<String, Value>,
71}
72
73#[derive(Clone, Copy, Debug, Default)]
75pub struct SignJwt;
76
77impl<D: Adapter> Endpoint<D> for SignJwt {
78 const METHOD: Method = Method::POST;
79 const PATH: &'static str = "/sign-jwt";
80 type Access = Public;
81 type Input = SignJwtInput;
82 type Output = TokenOutput;
83
84 async fn call(&self, ctx: Ctx<D>, input: SignJwtInput) -> Result<TokenOutput, AuthError> {
85 let token = keys::sign(&ctx, ctx.plugin::<Jwt>(), input.payload).await?;
86 Ok(TokenOutput { token })
87 }
88}
89
90#[derive(Clone, Debug, Default, Serialize, Deserialize)]
92pub struct VerifyJwtInput {
93 pub token: String,
94 #[serde(default, skip_serializing_if = "Option::is_none")]
96 pub issuer: Option<String>,
97}
98
99#[derive(Clone, Debug, Default, Serialize, Deserialize)]
101pub struct VerifyJwtOutput {
102 pub payload: Option<Map<String, Value>>,
103}
104
105#[derive(Clone, Copy, Debug, Default)]
108pub struct VerifyJwt;
109
110impl<D: Adapter> Endpoint<D> for VerifyJwt {
111 const METHOD: Method = Method::POST;
112 const PATH: &'static str = "/verify-jwt";
113 type Access = Public;
114 type Input = VerifyJwtInput;
115 type Output = VerifyJwtOutput;
116
117 async fn call(&self, ctx: Ctx<D>, input: VerifyJwtInput) -> Result<VerifyJwtOutput, AuthError> {
118 let jwt = ctx.plugin::<Jwt>();
119 let all = keys::all(&ctx).await?;
120 let keys = keys::published(&all, jwt, ctx.now())
121 .into_iter()
122 .map(|key| keys::public_jwk(key, jwt))
123 .collect::<Result<Vec<_>, _>>()?;
124 let jwks: JwkSet = serde_json::from_value(serde_json::json!({ "keys": keys }))
125 .map_err(AuthError::internal)?;
126 let base_url = ctx
127 .config()
128 .base_url
129 .as_str()
130 .trim_end_matches('/')
131 .to_owned();
132 let issuer = input
133 .issuer
134 .or_else(|| jwt.issuer.clone())
135 .unwrap_or_else(|| base_url.clone());
136 let audience = jwt.audience.clone().unwrap_or(base_url);
137 let payload = JwksVerifier::new(jwks, issuer, audience)
138 .verify(&input.token)
139 .ok();
140 Ok(VerifyJwtOutput { payload })
141 }
142}
143
144#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
146pub struct RotatedKey {
147 pub kid: Id,
148}
149
150#[derive(Clone, Copy, Debug, Default)]
153pub struct RotateKeys;
154
155impl<D: Adapter> Endpoint<D> for RotateKeys {
156 const METHOD: Method = Method::POST;
157 const PATH: &'static str = "/jwks/rotate";
158 type Access = Public;
159 type Input = Empty;
160 type Output = RotatedKey;
161
162 async fn call(&self, ctx: Ctx<D>, _: Empty) -> Result<RotatedKey, AuthError> {
163 let key = keys::rotate(&ctx, ctx.plugin::<Jwt>()).await?;
164 Ok(RotatedKey { kid: key.id })
165 }
166}