openid_client/types/
callback_params.rs1use std::collections::HashMap;
2
3use josekit::{jwk::Jwk, jwt::JwtPayload};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::helpers::get_serde_value_as_string;
8
9#[derive(Default, Serialize, Debug)]
13pub struct CallbackParams {
14 pub access_token: Option<String>,
16 pub code: Option<String>,
18 pub error: Option<String>,
20 pub error_description: Option<String>,
22 pub error_uri: Option<String>,
24 pub expires_in: Option<String>,
26 pub id_token: Option<String>,
28 pub state: Option<String>,
30 pub token_type: Option<String>,
32 pub session_state: Option<String>,
34 pub response: Option<String>,
36 pub iss: Option<String>,
38 pub scope: Option<String>,
40 #[serde(flatten, skip_serializing_if = "Option::is_none")]
42 pub other: Option<HashMap<String, String>>,
43}
44
45impl CallbackParams {
46 pub fn access_token(mut self, access_token: Option<String>) -> Self {
48 self.access_token = access_token;
49 self
50 }
51
52 pub fn code(mut self, code: Option<String>) -> Self {
54 self.code = code;
55 self
56 }
57
58 pub fn error(mut self, error: Option<String>) -> Self {
60 self.error = error;
61 self
62 }
63
64 pub fn error_description(mut self, error_description: Option<String>) -> Self {
66 self.error_description = error_description;
67 self
68 }
69
70 pub fn error_uri(mut self, error_uri: Option<String>) -> Self {
72 self.error_uri = error_uri;
73 self
74 }
75
76 pub fn expires_in(mut self, expires_in: Option<String>) -> Self {
78 self.expires_in = expires_in;
79 self
80 }
81
82 pub fn id_token(mut self, id_token: Option<String>) -> Self {
84 self.id_token = id_token;
85 self
86 }
87
88 pub fn state(mut self, state: Option<String>) -> Self {
90 self.state = state;
91 self
92 }
93
94 pub fn token_type(mut self, token_type: Option<String>) -> Self {
96 self.token_type = token_type;
97 self
98 }
99
100 pub fn session_state(mut self, session_state: Option<String>) -> Self {
102 self.session_state = session_state;
103 self
104 }
105
106 pub fn response(mut self, response: Option<String>) -> Self {
108 self.response = response;
109 self
110 }
111
112 pub fn iss(mut self, iss: Option<String>) -> Self {
114 self.iss = iss;
115 self
116 }
117
118 pub fn scope(mut self, scope: Option<String>) -> Self {
120 self.scope = scope;
121 self
122 }
123
124 pub fn other(mut self, other: Option<HashMap<String, String>>) -> Self {
126 self.other = other;
127 self
128 }
129}
130
131impl CallbackParams {
132 pub(crate) fn from_jwt_payload(payload: &JwtPayload) -> Self {
133 let mut params = Self {
134 access_token: Self::json_value_to_string_option(payload.claim("access_token")),
135 code: Self::json_value_to_string_option(payload.claim("code")),
136 error: Self::json_value_to_string_option(payload.claim("error")),
137 error_description: Self::json_value_to_string_option(
138 payload.claim("error_description"),
139 ),
140 error_uri: Self::json_value_to_string_option(payload.claim("error_uri")),
141 expires_in: Self::json_value_to_string_option(payload.claim("exp")),
142 id_token: Self::json_value_to_string_option(payload.claim("id_token")),
143 state: Self::json_value_to_string_option(payload.claim("state")),
144 token_type: Self::json_value_to_string_option(payload.claim("token_type")),
145 session_state: Self::json_value_to_string_option(payload.claim("session_state")),
146 response: Self::json_value_to_string_option(payload.claim("response")),
147 iss: Self::json_value_to_string_option(payload.claim("iss")),
148 scope: Self::json_value_to_string_option(payload.claim("scope")),
149 other: None,
150 };
151
152 let mut other = HashMap::<String, String>::new();
153
154 for (k, v) in payload.claims_set().iter() {
155 if let Ok(v_string) = get_serde_value_as_string(v) {
156 other.insert(k.to_string(), v_string);
157 }
158 }
159
160 params.other = Some(other);
161
162 params
163 }
164
165 fn json_value_to_string_option(value: Option<&Value>) -> Option<String> {
166 if let Some(v) = value {
167 return get_serde_value_as_string(v).ok();
168 }
169
170 None
171 }
172}
173
174pub struct CallbackExtras {
177 pub exchange_body: Option<HashMap<String, String>>,
179 pub client_assertion_payload: Option<HashMap<String, Value>>,
182 pub dpop: Option<Jwk>,
184}
185
186#[derive(Default, Serialize, Deserialize)]
189pub struct OAuthCallbackChecks<'a> {
190 pub response_type: Option<&'a str>,
192 pub state: Option<&'a str>,
194 pub code_verifier: Option<&'a str>,
196 pub jarm: Option<bool>,
198}
199
200#[derive(Default, Serialize, Deserialize)]
203pub struct OpenIDCallbackChecks<'a> {
204 pub max_age: Option<u64>,
206 pub nonce: Option<&'a str>,
208 pub oauth_checks: Option<OAuthCallbackChecks<'a>>,
210}