surrealdb_core/sql/
access.rs

1use crate::sql::{
2	escape::escape_ident, fmt::Fmt, strand::no_nul_bytes, Duration, Id, Ident, Thing,
3};
4use revision::revisioned;
5use serde::{Deserialize, Serialize};
6use std::fmt::{self, Display, Formatter};
7use std::ops::Deref;
8use std::str;
9
10#[revisioned(revision = 1)]
11#[derive(Debug, Serialize, Deserialize, Hash, Clone, Eq, PartialEq, PartialOrd)]
12#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
13// Durations representing the expiration of different elements of the access method
14// In this context, the None variant represents that the element does not expire
15pub struct AccessDuration {
16	// Duration after which the grants generated with the access method expire
17	// For access methods whose grants are tokens, this value is irrelevant
18	pub grant: Option<Duration>,
19	// Duration after which the tokens obtained with the access method expire
20	// For access methods that cannot issue tokens, this value is irrelevant
21	pub token: Option<Duration>,
22	// Duration after which the session authenticated with the access method expires
23	pub session: Option<Duration>,
24}
25
26impl Default for AccessDuration {
27	fn default() -> Self {
28		Self {
29			// By default, access grants expire in 30 days.
30			grant: Some(Duration::from_days(30)),
31			// By default, tokens expire after one hour
32			token: Some(Duration::from_hours(1)),
33			// By default, sessions do not expire
34			session: None,
35		}
36	}
37}
38
39#[revisioned(revision = 1)]
40#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
41#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
42#[non_exhaustive]
43pub struct Accesses(pub Vec<Access>);
44
45impl From<Access> for Accesses {
46	fn from(v: Access) -> Self {
47		Accesses(vec![v])
48	}
49}
50
51impl Deref for Accesses {
52	type Target = Vec<Access>;
53	fn deref(&self) -> &Self::Target {
54		&self.0
55	}
56}
57
58impl Display for Accesses {
59	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
60		Display::fmt(&Fmt::comma_separated(&self.0), f)
61	}
62}
63
64#[revisioned(revision = 1)]
65#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
66#[serde(rename = "$surrealdb::private::sql::Access")]
67#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
68#[non_exhaustive]
69pub struct Access(#[serde(with = "no_nul_bytes")] pub String);
70
71impl From<String> for Access {
72	fn from(v: String) -> Self {
73		Self(v)
74	}
75}
76
77impl From<&str> for Access {
78	fn from(v: &str) -> Self {
79		Self::from(String::from(v))
80	}
81}
82
83impl From<Ident> for Access {
84	fn from(v: Ident) -> Self {
85		Self(v.0)
86	}
87}
88
89impl Deref for Access {
90	type Target = String;
91	fn deref(&self) -> &Self::Target {
92		&self.0
93	}
94}
95
96impl Access {
97	pub fn generate(&self) -> Thing {
98		Thing {
99			tb: self.0.to_owned(),
100			id: Id::rand(),
101		}
102	}
103}
104
105impl Display for Access {
106	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
107		Display::fmt(&escape_ident(&self.0), f)
108	}
109}