surrealdb_core/iam/
access.rs

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
use crate::cnf::INSECURE_FORWARD_ACCESS_ERRORS;
use crate::ctx::MutableContext;
use crate::dbs::Session;
use crate::err::Error;
use crate::kvs::{Datastore, LockType::*, TransactionType::*};
use crate::sql::statements::access;
use crate::sql::{Base, Ident, Thing, Value};
use reblessive;

// Execute the AUTHENTICATE clause for a record access method
pub async fn authenticate_record(
	kvs: &Datastore,
	session: &Session,
	authenticate: &Value,
) -> Result<Thing, Error> {
	match kvs.evaluate(authenticate, session, None).await {
		Ok(val) => match val.record() {
			// If the AUTHENTICATE clause returns a record, authentication continues with that record
			Some(id) => Ok(id),
			// If the AUTHENTICATE clause returns anything else, authentication fails generically
			_ => {
				debug!("Authentication attempt as record user rejected by AUTHENTICATE clause");
				Err(Error::InvalidAuth)
			}
		},
		Err(e) => {
			match e {
				// If the AUTHENTICATE clause throws a specific error, authentication fails with that error
				Error::Thrown(_) => Err(e),
				// If the AUTHENTICATE clause failed due to an unexpected error, be more specific
				// This allows clients to handle these errors, which may be retryable
				Error::Tx(_) | Error::TxFailure | Error::TxRetryable => {
					debug!("Unexpected error found while executing AUTHENTICATE clause: {e}");
					Err(Error::UnexpectedAuth)
				}
				// Otherwise, return a generic error unless it should be forwarded
				e => {
					debug!("Authentication attempt failed due to an error in the AUTHENTICATE clause: {e}");
					if *INSECURE_FORWARD_ACCESS_ERRORS {
						Err(e)
					} else {
						Err(Error::InvalidAuth)
					}
				}
			}
		}
	}
}

// Execute the AUTHENTICATE clause for any other access method
pub async fn authenticate_generic(
	kvs: &Datastore,
	session: &Session,
	authenticate: &Value,
) -> Result<(), Error> {
	match kvs.evaluate(authenticate, session, None).await {
		Ok(val) => {
			match val {
				// If the AUTHENTICATE clause returns nothing, authentication continues
				Value::None => Ok(()),
				// If the AUTHENTICATE clause returns anything else, authentication fails generically
				_ => {
					debug!("Authentication attempt as system user rejected by AUTHENTICATE clause");
					Err(Error::InvalidAuth)
				}
			}
		}
		Err(e) => {
			match e {
				// If the AUTHENTICATE clause throws a specific error, authentication fails with that error
				Error::Thrown(_) => Err(e),
				// If the AUTHENTICATE clause failed due to an unexpected error, be more specific
				// This allows clients to handle these errors, which may be retryable
				Error::Tx(_) | Error::TxFailure | Error::TxRetryable => {
					debug!("Unexpected error found while executing an AUTHENTICATE clause: {e}");
					Err(Error::UnexpectedAuth)
				}
				// Otherwise, return a generic error unless it should be forwarded
				e => {
					debug!("Authentication attempt failed due to an error in the AUTHENTICATE clause: {e}");
					if *INSECURE_FORWARD_ACCESS_ERRORS {
						Err(e)
					} else {
						Err(Error::InvalidAuth)
					}
				}
			}
		}
	}
}

// Create a bearer key to act as refresh token for a record user
pub async fn create_refresh_token_record(
	kvs: &Datastore,
	ac: Ident,
	ns: &str,
	db: &str,
	rid: Thing,
) -> Result<String, Error> {
	let stmt = access::AccessStatementGrant {
		ac,
		base: Some(Base::Db),
		subject: access::Subject::Record(rid),
	};
	let sess = Session::owner().with_ns(ns).with_db(db);
	let opt = kvs.setup_options(&sess);
	// Create a new context with a writeable transaction
	let mut ctx = MutableContext::background();
	let tx = kvs.transaction(Write, Optimistic).await?.enclose();
	ctx.set_transaction(tx.clone());
	let ctx = ctx.freeze();
	// Create a bearer grant to act as the refresh token
	let grant = access::create_grant(&stmt, &ctx, &opt).await.map_err(|e| {
		warn!("Unexpected error when attempting to create a refresh token: {e}");
		Error::UnexpectedAuth
	})?;
	tx.commit().await?;
	// Return the key string from the bearer grant
	match grant.grant {
		access::Grant::Bearer(bearer) => Ok(bearer.key.as_string()),
		_ => Err(Error::AccessMethodMismatch),
	}
}

// Revoke a bearer key that acted as a refresh token for a record user
pub async fn revoke_refresh_token_record(
	kvs: &Datastore,
	gr: Ident,
	ac: Ident,
	ns: &str,
	db: &str,
) -> Result<(), Error> {
	let stmt = access::AccessStatementRevoke {
		ac,
		base: Some(Base::Db),
		gr: Some(gr),
		cond: None,
	};
	let sess = Session::owner().with_ns(ns).with_db(db);
	let opt = kvs.setup_options(&sess);
	// Create a new context with a writeable transaction
	let mut ctx = MutableContext::background();
	let tx = kvs.transaction(Write, Optimistic).await?.enclose();
	ctx.set_transaction(tx.clone());
	let ctx = ctx.freeze();
	// Create a bearer grant to act as the refresh token
	let mut stack = reblessive::tree::TreeStack::new();
	stack
		.enter(|stk| async {
			access::revoke_grant(&stmt, stk, &ctx, &opt).await.map_err(|e| {
				warn!("Unexpected error when attempting to revoke a refresh token: {e}");
				Error::UnexpectedAuth
			})
		})
		.finish()
		.await?;
	tx.commit().await?;
	Ok(())
}