Skip to main content

reifydb_auth/
error.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use reifydb_value::{
5	error::{Diagnostic, Error, IntoDiagnostic},
6	fragment::Fragment,
7};
8
9pub type Result<T> = std::result::Result<T, Error>;
10
11#[derive(Debug, thiserror::Error)]
12pub enum AuthError {
13	#[error("password is required for password authentication")]
14	PasswordRequired,
15
16	#[error("stored authentication is missing hash")]
17	MissingHash,
18
19	#[error("stored authentication is missing salt")]
20	MissingSalt,
21
22	#[error("stored authentication is missing token")]
23	MissingToken,
24
25	#[error("unknown authentication method: {method}")]
26	UnknownMethod {
27		method: String,
28	},
29
30	#[error("failed to serialize authentication properties: {reason}")]
31	SerializeProperties {
32		reason: String,
33	},
34
35	#[error("password hashing failed: {reason}")]
36	HashingFailed {
37		reason: String,
38	},
39
40	#[error("stored hash is invalid or corrupted: {reason}")]
41	InvalidHash {
42		reason: String,
43	},
44
45	#[error("password verification failed: {reason}")]
46	VerificationFailed {
47		reason: String,
48	},
49
50	#[error("guest login takes no credentials, got: {keys}")]
51	GuestTakesNoCredentials {
52		keys: String,
53	},
54
55	#[error("guest identities have no credentials")]
56	GuestHasNoCredentials,
57}
58
59#[derive(Debug, thiserror::Error)]
60pub enum SolanaError {
61	#[error("public key is required for solana authentication")]
62	MissingPublicKey,
63
64	#[error("invalid public key: {reason}")]
65	InvalidPublicKey {
66		reason: String,
67	},
68
69	#[error("invalid signature: {reason}")]
70	InvalidSignature {
71		reason: String,
72	},
73
74	#[error("challenge payload is missing the sign-in message")]
75	MissingChallengeMessage,
76}
77
78#[derive(Debug, thiserror::Error)]
79pub enum GithubError {
80	#[error("user_id is required for github authentication")]
81	MissingUserId,
82
83	#[error("invalid github user_id: {reason}")]
84	InvalidUserId {
85		reason: String,
86	},
87
88	#[error("github oauth code exchange failed: {reason}")]
89	ExchangeFailed {
90		reason: String,
91	},
92
93	#[error("github api request failed: {reason}")]
94	ApiFailed {
95		reason: String,
96	},
97}
98
99impl IntoDiagnostic for AuthError {
100	fn into_diagnostic(self) -> Diagnostic {
101		match self {
102			AuthError::PasswordRequired => Diagnostic {
103				code: "AU_001".to_string(),
104				rql: None,
105				message: "password is required for password authentication".to_string(),
106				fragment: Fragment::None,
107				label: Some("missing password".to_string()),
108				help: Some("provide a password in the authentication configuration".to_string()),
109				column: None,
110				notes: vec![],
111				cause: None,
112				operator_chain: None,
113			},
114
115			AuthError::MissingHash => Diagnostic {
116				code: "AU_002".to_string(),
117				rql: None,
118				message: "stored authentication is missing hash".to_string(),
119				fragment: Fragment::None,
120				label: Some("missing hash".to_string()),
121				help: Some("the stored authentication record is corrupted or incomplete".to_string()),
122				column: None,
123				notes: vec![],
124				cause: None,
125				operator_chain: None,
126			},
127
128			AuthError::MissingSalt => Diagnostic {
129				code: "AU_003".to_string(),
130				rql: None,
131				message: "stored authentication is missing salt".to_string(),
132				fragment: Fragment::None,
133				label: Some("missing salt".to_string()),
134				help: Some("the stored authentication record is corrupted or incomplete".to_string()),
135				column: None,
136				notes: vec![],
137				cause: None,
138				operator_chain: None,
139			},
140
141			AuthError::MissingToken => Diagnostic {
142				code: "AU_004".to_string(),
143				rql: None,
144				message: "stored authentication is missing token".to_string(),
145				fragment: Fragment::None,
146				label: Some("missing token".to_string()),
147				help: Some("the stored authentication record is corrupted or incomplete".to_string()),
148				column: None,
149				notes: vec![],
150				cause: None,
151				operator_chain: None,
152			},
153
154			AuthError::SerializeProperties {
155				reason,
156			} => Diagnostic {
157				code: "AU_006".to_string(),
158				rql: None,
159				message: format!("failed to serialize authentication properties: {}", reason),
160				fragment: Fragment::None,
161				label: Some("serialization failed".to_string()),
162				help: Some("ensure authentication properties are valid".to_string()),
163				column: None,
164				notes: vec![],
165				cause: None,
166				operator_chain: None,
167			},
168
169			AuthError::UnknownMethod {
170				method,
171			} => Diagnostic {
172				code: "AU_005".to_string(),
173				rql: None,
174				message: format!("unknown authentication method: {}", method),
175				fragment: Fragment::None,
176				label: Some("unknown method".to_string()),
177				help: Some("supported authentication methods are: password, token, solana, github"
178					.to_string()),
179				column: None,
180				notes: vec![],
181				cause: None,
182				operator_chain: None,
183			},
184
185			AuthError::HashingFailed {
186				reason,
187			} => Diagnostic {
188				code: "AU_007".to_string(),
189				rql: None,
190				message: format!("password hashing failed: {}", reason),
191				fragment: Fragment::None,
192				label: Some("hashing failed".to_string()),
193				help: Some("an internal error occurred during password hashing".to_string()),
194				column: None,
195				notes: vec![],
196				cause: None,
197				operator_chain: None,
198			},
199
200			AuthError::InvalidHash {
201				reason,
202			} => Diagnostic {
203				code: "AU_008".to_string(),
204				rql: None,
205				message: format!("stored hash is invalid or corrupted: {}", reason),
206				fragment: Fragment::None,
207				label: Some("invalid hash".to_string()),
208				help: Some("the stored authentication record is corrupted or incomplete".to_string()),
209				column: None,
210				notes: vec![],
211				cause: None,
212				operator_chain: None,
213			},
214
215			AuthError::VerificationFailed {
216				reason,
217			} => Diagnostic {
218				code: "AU_009".to_string(),
219				rql: None,
220				message: format!("password verification failed: {}", reason),
221				fragment: Fragment::None,
222				label: Some("verification failed".to_string()),
223				help: Some("an internal error occurred during password verification".to_string()),
224				column: None,
225				notes: vec![],
226				cause: None,
227				operator_chain: None,
228			},
229			AuthError::GuestTakesNoCredentials {
230				keys,
231			} => Diagnostic {
232				code: "AU_018".to_string(),
233				rql: None,
234				message: format!("guest login takes no credentials, got: {}", keys),
235				fragment: Fragment::None,
236				label: Some("unexpected credentials".to_string()),
237				help: Some("call authenticate(\"guest\") with empty credentials".to_string()),
238				column: None,
239				notes: vec![],
240				cause: None,
241				operator_chain: None,
242			},
243
244			AuthError::GuestHasNoCredentials => Diagnostic {
245				code: "AU_019".to_string(),
246				rql: None,
247				message: "guest identities have no credentials".to_string(),
248				fragment: Fragment::None,
249				label: Some("credential not allowed".to_string()),
250				help: Some("a guest gets its session from authenticate(\"guest\")".to_string()),
251				column: None,
252				notes: vec![],
253				cause: None,
254				operator_chain: None,
255			},
256		}
257	}
258}
259
260impl From<AuthError> for Error {
261	fn from(err: AuthError) -> Self {
262		Error(Box::new(err.into_diagnostic()))
263	}
264}
265
266impl IntoDiagnostic for SolanaError {
267	fn into_diagnostic(self) -> Diagnostic {
268		match self {
269			SolanaError::MissingPublicKey => Diagnostic {
270				code: "AU_010".to_string(),
271				rql: None,
272				message: "public key is required for solana authentication".to_string(),
273				fragment: Fragment::None,
274				label: Some("missing public key".to_string()),
275				help: Some("provide a public_key in the authentication configuration".to_string()),
276				column: None,
277				notes: vec![],
278				cause: None,
279				operator_chain: None,
280			},
281
282			SolanaError::InvalidPublicKey {
283				reason,
284			} => Diagnostic {
285				code: "AU_011".to_string(),
286				rql: None,
287				message: format!("invalid public key: {}", reason),
288				fragment: Fragment::None,
289				label: Some("invalid public key".to_string()),
290				help: Some("provide a valid base58-encoded 32-byte Solana public key".to_string()),
291				column: None,
292				notes: vec![],
293				cause: None,
294				operator_chain: None,
295			},
296
297			SolanaError::InvalidSignature {
298				reason,
299			} => Diagnostic {
300				code: "AU_012".to_string(),
301				rql: None,
302				message: format!("invalid signature: {}", reason),
303				fragment: Fragment::None,
304				label: Some("invalid signature".to_string()),
305				help: Some("provide a valid base58-encoded 64-byte ed25519 signature".to_string()),
306				column: None,
307				notes: vec![],
308				cause: None,
309				operator_chain: None,
310			},
311
312			SolanaError::MissingChallengeMessage => Diagnostic {
313				code: "AU_017".to_string(),
314				rql: None,
315				message: "challenge payload is missing the sign-in message".to_string(),
316				fragment: Fragment::None,
317				label: Some("missing challenge message".to_string()),
318				help: Some("the stored sign-in challenge is corrupted or incomplete".to_string()),
319				column: None,
320				notes: vec![],
321				cause: None,
322				operator_chain: None,
323			},
324		}
325	}
326}
327
328impl From<SolanaError> for Error {
329	fn from(err: SolanaError) -> Self {
330		Error(Box::new(err.into_diagnostic()))
331	}
332}
333
334impl IntoDiagnostic for GithubError {
335	fn into_diagnostic(self) -> Diagnostic {
336		match self {
337			GithubError::MissingUserId => Diagnostic {
338				code: "AU_013".to_string(),
339				rql: None,
340				message: "user_id is required for github authentication".to_string(),
341				fragment: Fragment::None,
342				label: Some("missing user_id".to_string()),
343				help: Some("provide a user_id in the authentication configuration".to_string()),
344				column: None,
345				notes: vec![],
346				cause: None,
347				operator_chain: None,
348			},
349
350			GithubError::InvalidUserId {
351				reason,
352			} => Diagnostic {
353				code: "AU_014".to_string(),
354				rql: None,
355				message: format!("invalid github user_id: {}", reason),
356				fragment: Fragment::None,
357				label: Some("invalid user_id".to_string()),
358				help: Some("provide the numeric github account id as user_id".to_string()),
359				column: None,
360				notes: vec![],
361				cause: None,
362				operator_chain: None,
363			},
364
365			GithubError::ExchangeFailed {
366				reason,
367			} => Diagnostic {
368				code: "AU_015".to_string(),
369				rql: None,
370				message: format!("github oauth code exchange failed: {}", reason),
371				fragment: Fragment::None,
372				label: Some("code exchange failed".to_string()),
373				help: Some("verify the github oauth client id, client secret, and redirect uri"
374					.to_string()),
375				column: None,
376				notes: vec![],
377				cause: None,
378				operator_chain: None,
379			},
380
381			GithubError::ApiFailed {
382				reason,
383			} => Diagnostic {
384				code: "AU_016".to_string(),
385				rql: None,
386				message: format!("github api request failed: {}", reason),
387				fragment: Fragment::None,
388				label: Some("github api failed".to_string()),
389				help: Some("github may be unreachable; retry the sign-in".to_string()),
390				column: None,
391				notes: vec![],
392				cause: None,
393				operator_chain: None,
394			},
395		}
396	}
397}
398
399impl From<GithubError> for Error {
400	fn from(err: GithubError) -> Self {
401		Error(Box::new(err.into_diagnostic()))
402	}
403}