1use crate::{
2 prelude::{OperationError, Url},
3 server::identity::Source,
4};
5use compact_jwt::JwsCompact;
6use crypto_glue::{s256::Sha256Output, x509::Certificate};
7use kanidm_proto::{
8 internal::UserAuthToken,
9 oauth2::{
10 AccessTokenIntrospectRequest, AccessTokenIntrospectResponse, AccessTokenRequest,
11 AccessTokenResponse, AuthorisationRequest,
12 },
13 v1::{
14 AuthAllowed, AuthCredential as ProtoAuthCredential, AuthIssueSession, AuthMech,
15 AuthStep as ProtoAuthStep,
16 },
17};
18use std::fmt;
19use webauthn_rs::prelude::PublicKeyCredential;
20
21#[derive(Debug)]
22pub enum AuthStep {
23 Init(String),
24 Init2 {
25 username: String,
26 issue: AuthIssueSession,
27 privileged: bool,
28 },
29 Begin(AuthMech),
30 Cred(AuthCredential),
31}
32
33impl From<ProtoAuthStep> for AuthStep {
34 fn from(proto: ProtoAuthStep) -> Self {
35 match proto {
36 ProtoAuthStep::Init(name) => Self::Init(name),
37 ProtoAuthStep::Init2 {
38 username,
39 issue,
40 privileged,
41 } => Self::Init2 {
42 username,
43 issue,
44 privileged,
45 },
46 ProtoAuthStep::Begin(mech) => Self::Begin(mech),
47 ProtoAuthStep::Cred(proto_cred) => Self::Cred(AuthCredential::from(proto_cred)),
48 }
49 }
50}
51
52pub enum AuthExternal {
53 OAuth2AuthorisationRequest {
54 authorisation_url: Url,
55 request: AuthorisationRequest,
56 },
57 OAuth2AccessTokenRequest {
58 token_url: Url,
59 client_id: String,
60 client_secret: String,
61 request: AccessTokenRequest,
62 },
63 OAuth2AccessTokenIntrospectionRequest {
64 introspection_url: Url,
65 client_id: String,
66 client_secret: String,
67 request: AccessTokenIntrospectRequest,
68 },
69}
70
71impl fmt::Debug for AuthExternal {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 Self::OAuth2AuthorisationRequest { .. } => write!(f, "OAuth2AuthorisationRequest"),
75 Self::OAuth2AccessTokenRequest { .. } => write!(f, "OAuth2AccessTokenRequest"),
76 Self::OAuth2AccessTokenIntrospectionRequest { .. } => {
77 write!(f, "OAuth2AccessTokenIntrospectionRequest")
78 }
79 }
80 }
81}
82
83#[allow(clippy::large_enum_variant)]
86pub enum AuthState {
87 Choose(Vec<AuthMech>),
88 Continue(Vec<AuthAllowed>),
89
90 External(AuthExternal),
94 Denied(String),
96 Success(Box<JwsCompact>, AuthIssueSession),
97}
98
99impl fmt::Debug for AuthState {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 match self {
102 AuthState::Choose(mechs) => write!(f, "AuthState::Choose({mechs:?})"),
103 AuthState::Continue(allow) => write!(f, "AuthState::Continue({allow:?})"),
104 AuthState::External(allow) => write!(f, "AuthState::External({allow:?})"),
105 AuthState::Denied(reason) => write!(f, "AuthState::Denied({reason:?})"),
106 AuthState::Success(_token, issue) => {
107 write!(f, "AuthState::Success({})", issue)
108 }
109 }
110 }
111}
112
113pub enum AuthCredential {
114 Anonymous,
115 Password(String),
116 Totp(u32),
117 SecurityKey(Box<PublicKeyCredential>),
118 BackupCode(String),
119 Passkey(Box<PublicKeyCredential>),
120
121 OAuth2AuthorisationResponse {
123 code: String,
124 state: Option<String>,
125 },
126 OAuth2AccessTokenResponse {
127 response: AccessTokenResponse,
128 },
129 OAuth2AccessTokenIntrospectResponse {
130 response: AccessTokenIntrospectResponse,
131 },
132}
133
134impl From<ProtoAuthCredential> for AuthCredential {
135 fn from(proto: ProtoAuthCredential) -> Self {
136 match proto {
137 ProtoAuthCredential::Anonymous => AuthCredential::Anonymous,
138 ProtoAuthCredential::Password(p) => AuthCredential::Password(p),
139 ProtoAuthCredential::Totp(t) => AuthCredential::Totp(t),
140 ProtoAuthCredential::SecurityKey(sk) => AuthCredential::SecurityKey(sk),
141 ProtoAuthCredential::BackupCode(bc) => AuthCredential::BackupCode(bc),
142 ProtoAuthCredential::Passkey(pkc) => AuthCredential::Passkey(pkc),
143 }
144 }
145}
146
147impl fmt::Debug for AuthCredential {
148 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
149 match self {
150 AuthCredential::Anonymous => write!(fmt, "Anonymous"),
151 AuthCredential::Password(_) => write!(fmt, "Password(_)"),
152 AuthCredential::Totp(_) => write!(fmt, "TOTP(_)"),
153 AuthCredential::SecurityKey(_) => write!(fmt, "SecurityKey(_)"),
154 AuthCredential::BackupCode(_) => write!(fmt, "BackupCode(_)"),
155 AuthCredential::Passkey(_) => write!(fmt, "Passkey(_)"),
156 AuthCredential::OAuth2AuthorisationResponse { .. } => {
157 write!(fmt, "OAuth2AuthorisationResponse{{..}}")
158 }
159 AuthCredential::OAuth2AccessTokenResponse { .. } => {
160 write!(fmt, "OAuth2AccessTokenResponse{{..}}")
161 }
162 AuthCredential::OAuth2AccessTokenIntrospectResponse { .. } => {
163 write!(fmt, "OAuth2AccessTokenIntrospectResponse{{..}}")
164 }
165 }
166 }
167}
168
169#[derive(Default)]
170pub enum ReauthRequest {
171 #[default]
172 VerifyCredentials,
173 GrantReadWrite,
174}
175
176#[derive(Debug, Clone, Default)]
177pub(crate) enum PreValidatedTokenStatus {
178 #[default]
179 None,
180 Valid(Box<UserAuthToken>),
181 NotAuthenticated,
182 SessionExpired,
183}
184
185#[derive(Debug, Clone)]
186pub struct ClientAuthInfo {
187 pub(crate) source: Source,
188 pub(crate) client_cert: Option<ClientCertInfo>,
189 pub(crate) bearer_token: Option<JwsCompact>,
190 pub(crate) basic_authz: Option<String>,
191 pub(crate) pre_validated_token: PreValidatedTokenStatus,
192}
193
194impl ClientAuthInfo {
195 pub fn new(
196 source: Source,
197 client_cert: Option<ClientCertInfo>,
198 bearer_token: Option<JwsCompact>,
199 basic_authz: Option<String>,
200 ) -> Self {
201 Self {
202 source,
203 client_cert,
204 bearer_token,
205 basic_authz,
206 pre_validated_token: Default::default(),
207 }
208 }
209
210 pub fn bearer_token(&self) -> Option<&JwsCompact> {
211 self.bearer_token.as_ref()
212 }
213
214 pub fn pre_validated_uat(&self) -> Result<&UserAuthToken, OperationError> {
215 match &self.pre_validated_token {
216 PreValidatedTokenStatus::Valid(uat) => Ok(uat),
217 PreValidatedTokenStatus::None => Err(OperationError::AU0008ClientAuthInfoPrevalidation),
218 PreValidatedTokenStatus::NotAuthenticated => Err(OperationError::NotAuthenticated),
219 PreValidatedTokenStatus::SessionExpired => Err(OperationError::SessionExpired),
220 }
221 }
222
223 pub(crate) fn set_pre_validated_uat(&mut self, status: PreValidatedTokenStatus) {
224 self.pre_validated_token = status
225 }
226}
227
228#[derive(Debug, Clone)]
229pub struct ClientCertInfo {
230 pub public_key_s256: Sha256Output,
231 pub certificate: Certificate,
232}
233
234#[cfg(test)]
235impl ClientAuthInfo {
236 pub(crate) fn none() -> Self {
237 ClientAuthInfo {
238 source: Source::Internal,
239 client_cert: None,
240 bearer_token: None,
241 basic_authz: None,
242 pre_validated_token: Default::default(),
243 }
244 }
245}
246
247#[cfg(test)]
248impl From<Source> for ClientAuthInfo {
249 fn from(value: Source) -> ClientAuthInfo {
250 ClientAuthInfo {
251 source: value,
252 client_cert: None,
253 bearer_token: None,
254 basic_authz: None,
255 pre_validated_token: Default::default(),
256 }
257 }
258}
259
260#[cfg(test)]
261impl From<JwsCompact> for ClientAuthInfo {
262 fn from(value: JwsCompact) -> ClientAuthInfo {
263 ClientAuthInfo {
264 source: Source::Internal,
265 client_cert: None,
266 bearer_token: Some(value),
267 basic_authz: None,
268 pre_validated_token: Default::default(),
269 }
270 }
271}
272
273#[cfg(test)]
274impl From<ClientCertInfo> for ClientAuthInfo {
275 fn from(value: ClientCertInfo) -> ClientAuthInfo {
276 ClientAuthInfo {
277 source: Source::Internal,
278 client_cert: Some(value),
279 bearer_token: None,
280 basic_authz: None,
281 pre_validated_token: Default::default(),
282 }
283 }
284}
285
286#[cfg(test)]
287impl From<&str> for ClientAuthInfo {
288 fn from(value: &str) -> ClientAuthInfo {
289 ClientAuthInfo {
290 source: Source::Internal,
291 client_cert: None,
292 bearer_token: None,
293 basic_authz: Some(value.to_string()),
294 pre_validated_token: Default::default(),
295 }
296 }
297}
298
299#[cfg(test)]
300impl ClientAuthInfo {
301 pub(crate) fn encode_basic(id: &str, secret: &str) -> ClientAuthInfo {
302 use base64::{engine::general_purpose, Engine as _};
303 let value = format!("{id}:{secret}");
304 let value = general_purpose::STANDARD.encode(&value);
305 ClientAuthInfo {
306 source: Source::Internal,
307 client_cert: None,
308 bearer_token: None,
309 basic_authz: Some(value),
310 pre_validated_token: Default::default(),
311 }
312 }
313}