surrealdb_core/sql/
access.rs

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