surrealdb 3.0.5

A scalable, distributed, collaborative, document-graph database, for the realtime web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Authentication types

use std::fmt;

use serde::{Deserialize, Serialize};

use crate::types::{Kind, Object, SurrealValue, Value, kind};

/// A signup action
#[derive(Debug)]
pub struct Signup;

/// A signin action
#[derive(Debug)]
pub struct Signin;

/// Credentials for authenticating with the server
pub trait Credentials<Action>: SurrealValue {}

/// Credentials for the root user
#[derive(Debug, Clone, SurrealValue)]
#[surreal(crate = "crate::types")]
pub struct Root {
	/// The username of the root user
	#[surreal(rename = "user")]
	pub username: String,
	/// The password of the root user
	#[surreal(rename = "pass")]
	pub password: String,
}

impl Credentials<Signin> for Root {}

/// Credentials for the namespace user
#[derive(Debug, Clone, SurrealValue)]
#[surreal(crate = "crate::types")]
pub struct Namespace {
	/// The namespace the user has access to
	#[surreal(rename = "ns")]
	pub namespace: String,
	/// The username of the namespace user
	#[surreal(rename = "user")]
	pub username: String,
	/// The password of the namespace user
	#[surreal(rename = "pass")]
	pub password: String,
}

impl Credentials<Signin> for Namespace {}

/// Credentials for the database user
#[derive(Debug, Clone, SurrealValue)]
#[surreal(crate = "crate::types")]
pub struct Database {
	/// The namespace the user has access to
	#[surreal(rename = "ns")]
	pub namespace: String,
	/// The database the user has access to
	#[surreal(rename = "db")]
	pub database: String,
	/// The username of the database user
	#[surreal(rename = "user")]
	pub username: String,
	/// The password of the database user
	#[surreal(rename = "pass")]
	pub password: String,
}

impl Credentials<Signin> for Database {}

/// Credentials for the record user
#[derive(Debug)]
pub struct Record<P: SurrealValue> {
	/// The namespace the user has access to
	pub namespace: String,
	/// The database the user has access to
	pub database: String,
	/// The access method to use for signin and signup
	pub access: String,
	/// The additional params to use
	pub params: P,
}

impl<P: SurrealValue> SurrealValue for Record<P> {
	fn kind_of() -> Kind {
		kind!({ ns: string, db: string, ac: string, params: any })
	}

	fn into_value(self) -> Value {
		let mut obj = Object::new();
		obj.insert("ns".to_string(), Value::String(self.namespace));
		obj.insert("db".to_string(), Value::String(self.database));
		obj.insert("ac".to_string(), Value::String(self.access));

		// Flatten the params into the top level object
		if let Value::Object(params_obj) = self.params.into_value() {
			for (key, value) in params_obj {
				obj.insert(key, value);
			}
		}

		Value::Object(obj)
	}

	fn from_value(value: Value) -> Result<Self, crate::Error> {
		if let Value::Object(mut obj) = value {
			let namespace = obj
				.remove("ns")
				.and_then(|v| {
					if let Value::String(s) = v {
						Some(s)
					} else {
						None
					}
				})
				.ok_or_else(|| crate::Error::internal("Missing 'ns' field".to_string()))?;
			let database = obj
				.remove("db")
				.and_then(|v| {
					if let Value::String(s) = v {
						Some(s)
					} else {
						None
					}
				})
				.ok_or_else(|| crate::Error::internal("Missing 'db' field".to_string()))?;
			let access = obj
				.remove("ac")
				.and_then(|v| {
					if let Value::String(s) = v {
						Some(s)
					} else {
						None
					}
				})
				.ok_or_else(|| crate::Error::internal("Missing 'ac' field".to_string()))?;

			// The remaining fields go into params
			let params = P::from_value(Value::Object(obj))?;

			Ok(Record {
				namespace,
				database,
				access,
				params,
			})
		} else {
			Err(crate::Error::internal("Expected an object for Record".to_string()))
		}
	}
}

impl<T, P> Credentials<T> for Record<P> where P: SurrealValue {}

/// A token containing both access and optional refresh token for authentication.
///
/// This struct represents the complete authentication token response from
/// SurrealDB's signin and signup operations. It contains an access token
/// (required) and an optional refresh token for enhanced security.
///
/// The token structure supports both legacy single-token authentication
/// and modern refresh token flows:
/// - **Legacy mode**: Only `access` token is present, `refresh` is `None`
/// - **Refresh mode**: Both `access` and `refresh` tokens are present
///
/// # Security
///
/// Both access and refresh tokens are wrapped in secure containers that:
/// - Redact token values in debug output
/// - Prevent accidental exposure in logs
/// - Require explicit methods to access token strings
///
/// # Examples
///
/// ```rust
/// use surrealdb::opt::auth::{Token, AccessToken, RefreshToken};
///
/// // Create a token with only access token (legacy mode)
/// let legacy_token = Token {
///     access: AccessToken::from("access_token_string"),
///     refresh: None,
/// };
///
/// // Create a token with both access and refresh tokens
/// let modern_token = Token {
///     access: AccessToken::from("access_token_string"),
///     refresh: Some(RefreshToken::from("refresh_token_string")),
/// };
///
/// // Access token values securely
/// let access_value = modern_token.access.as_insecure_token();
/// if let Some(refresh_token) = &modern_token.refresh {
///     let refresh_value = refresh_token.as_insecure_token();
///     // Use refresh token to get new access token
/// }
/// ```
#[derive(Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Token {
	/// The access token used for API authentication.
	///
	/// This token is required and used to authenticate API requests.
	/// It typically has a shorter expiration time for security.
	pub access: AccessToken,
	/// An optional refresh token used to obtain new access tokens.
	///
	/// When present, this token can be used to refresh the access token
	/// without requiring the user to re-authenticate. This enables
	/// seamless long-term sessions while maintaining security.
	pub refresh: Option<RefreshToken>,
}

impl SurrealValue for Token {
	fn kind_of() -> Kind {
		kind!(string | { access: string, refresh: string })
	}

	fn into_value(self) -> Value {
		match self.refresh {
			Some(refresh) => {
				let mut obj = Object::new();
				obj.insert("access".to_string(), self.access.into_value());
				obj.insert("refresh".to_string(), refresh.into_value());
				Value::Object(obj)
			}
			None => self.access.into_value(),
		}
	}

	fn from_value(value: Value) -> Result<Self, crate::Error> {
		match value {
			Value::String(string) => Ok(Token::from(string)),
			value => {
				let mut obj = Object::from_value(value)?;
				let access = AccessToken::from_value(obj.remove("access").unwrap_or_default())?;
				let refresh = RefreshToken::from_value(obj.remove("refresh").unwrap_or_default())?;
				Ok(Token::from((access, refresh)))
			}
		}
	}
}

/// A JSON Web Token for authenticating with the server.
///
/// This struct represents a JSON Web Token (JWT) that can be used for
/// authentication purposes. It is important to note that this implementation
/// provide some security measures to protect the token:
/// * the debug implementation just prints `AccessToken(REDACTED)`,
/// * `Display` is not implemented so you can't call `.to_string()` on it
///
/// You can still have access to the token string using either
/// [`as_insecure_token`](AccessToken::as_insecure_token) or
/// [`into_insecure_token`](AccessToken::into_insecure_token) functions. However, you
/// should take care to ensure that only authorized users have access to the
/// JWT. For example, it can be stored in a secure cookie or encrypted in conjunction with other
/// encryption mechanisms.
#[derive(Debug, Clone, Serialize, Deserialize, SurrealValue)]
#[surreal(crate = "crate::types")]
pub struct AccessToken(pub(crate) SecureToken);

impl AccessToken {
	/// Returns the underlying token string.
	///
	/// ⚠️: It is important to note that the token should be handled securely
	/// and protected from unauthorized access.
	pub fn as_insecure_token(&self) -> &str {
		&self.0.0
	}

	/// Returns the underlying token string.
	///
	/// ⚠️: It is important to note that the token should be handled securely
	/// and protected from unauthorized access.
	pub fn into_insecure_token(self) -> String {
		self.0.0
	}
}

/// A refresh token used to obtain new access tokens without re-authentication.
///
/// Refresh tokens are long-lived tokens that can be used to obtain new access
/// tokens when the current access token expires. This enables seamless user
/// sessions while maintaining security through short-lived access tokens.
///
/// # Security Features
///
/// - **Debug Protection**: Token values are redacted in debug output
/// - **Secure Storage**: Wrapped in a secure container to prevent accidental exposure
/// - **Explicit Access**: Requires explicit method calls to access the token string
///
/// # Usage
///
/// Refresh tokens are typically used in the following flow:
/// 1. User authenticates and receives both access and refresh tokens
/// 2. Access token is used for API requests
/// 3. When access token expires, refresh token is used to get a new access token
/// 4. Process repeats until refresh token expires or user logs out
///
/// # Examples
///
/// ```rust
/// use surrealdb::opt::auth::RefreshToken;
///
/// // Create a refresh token
/// let refresh_token = RefreshToken::from("refresh_token_string");
///
/// // Access the token string securely
/// let token_string = refresh_token.as_insecure_token();
///
/// // Use the token string to request a new access token
/// // (implementation depends on your authentication flow)
/// ```
#[derive(Debug, Serialize, Deserialize, SurrealValue)]
#[surreal(crate = "crate::types")]
pub struct RefreshToken(pub(crate) SecureToken);

impl RefreshToken {
	/// Returns the underlying token string.
	///
	/// ⚠️: It is important to note that the token should be handled securely
	/// and protected from unauthorized access.
	pub fn as_insecure_token(&self) -> &str {
		&self.0.0
	}

	/// Returns the underlying token string.
	///
	/// ⚠️: It is important to note that the token should be handled securely
	/// and protected from unauthorized access.
	pub fn into_insecure_token(self) -> String {
		self.0.0
	}
}

/// A secure wrapper for token strings that prevents accidental exposure.
///
/// This internal struct wraps token strings to provide security features:
/// - Prevents accidental exposure in debug output
/// - Requires explicit method calls to access the underlying string
/// - Provides a clear API for secure token handling
///
/// The struct is marked as `pub(crate)` to keep it internal to the crate
/// while still allowing access from other modules within the same crate.
#[derive(Clone, Serialize, Deserialize, SurrealValue)]
#[surreal(crate = "crate::types")]
pub(crate) struct SecureToken(pub(crate) String);

impl fmt::Debug for SecureToken {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		write!(f, "REDACTED")
	}
}

impl From<AccessToken> for Token {
	fn from(access: AccessToken) -> Self {
		Self {
			access,
			refresh: None,
		}
	}
}

impl From<(AccessToken, RefreshToken)> for Token {
	fn from(token: (AccessToken, RefreshToken)) -> Self {
		Self {
			access: token.0,
			refresh: Some(token.1),
		}
	}
}

impl From<(AccessToken, Option<RefreshToken>)> for Token {
	fn from(token: (AccessToken, Option<RefreshToken>)) -> Self {
		Self {
			access: token.0,
			refresh: token.1,
		}
	}
}

impl From<String> for Token {
	fn from(token: String) -> Self {
		Self {
			access: AccessToken(SecureToken(token)),
			refresh: None,
		}
	}
}

impl<'a> From<&'a String> for Token {
	fn from(token: &'a String) -> Self {
		Self {
			access: AccessToken(SecureToken(token.to_owned())),
			refresh: None,
		}
	}
}

impl<'a> From<&'a str> for Token {
	fn from(token: &'a str) -> Self {
		Self {
			access: AccessToken(SecureToken(token.to_owned())),
			refresh: None,
		}
	}
}

impl From<String> for AccessToken {
	fn from(token: String) -> Self {
		Self(SecureToken(token))
	}
}

impl<'a> From<&'a String> for AccessToken {
	fn from(token: &'a String) -> Self {
		Self(SecureToken(token.to_owned()))
	}
}

impl<'a> From<&'a str> for AccessToken {
	fn from(token: &'a str) -> Self {
		Self(SecureToken(token.to_owned()))
	}
}

impl From<String> for RefreshToken {
	fn from(token: String) -> Self {
		Self(SecureToken(token))
	}
}

impl<'a> From<&'a String> for RefreshToken {
	fn from(token: &'a String) -> Self {
		Self(SecureToken(token.to_owned()))
	}
}

impl<'a> From<&'a str> for RefreshToken {
	fn from(token: &'a str) -> Self {
		Self(SecureToken(token.to_owned()))
	}
}

impl From<RefreshToken> for SecureToken {
	fn from(token: RefreshToken) -> Self {
		token.0
	}
}