use super::access::{
authenticate_generic, authenticate_record, create_refresh_token_record,
revoke_refresh_token_record,
};
use super::verify::{verify_db_creds, verify_ns_creds, verify_root_creds};
use super::{Actor, Level, Role};
use crate::cnf::{INSECURE_FORWARD_ACCESS_ERRORS, SERVER_NAME};
use crate::dbs::capabilities::ExperimentalTarget;
use crate::dbs::Session;
use crate::err::Error;
use crate::iam::issue::{config, expiration};
use crate::iam::token::{Claims, HEADER};
use crate::iam::Auth;
use crate::kvs::{Datastore, LockType::*, TransactionType::*};
use crate::sql::statements::{access, AccessGrant, DefineAccessStatement};
use crate::sql::{access_type, AccessType, Datetime, Object, Value};
use chrono::Utc;
use jsonwebtoken::{encode, EncodingKey, Header};
use md5::Digest;
use revision::revisioned;
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use std::str::FromStr;
use std::sync::Arc;
use subtle::ConstantTimeEq;
use uuid::Uuid;
#[revisioned(revision = 1)]
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[non_exhaustive]
pub struct SigninData {
pub token: String,
pub refresh: Option<String>,
}
impl From<SigninData> for Value {
fn from(v: SigninData) -> Value {
let mut out = Object::default();
out.insert("token".to_string(), v.token.into());
if let Some(refresh) = v.refresh {
out.insert("refresh".to_string(), refresh.into());
}
out.into()
}
}
pub async fn signin(
kvs: &Datastore,
session: &mut Session,
vars: Object,
) -> Result<SigninData, Error> {
vars.validate_computed()?;
let ns = vars.get("NS").or_else(|| vars.get("ns"));
let db = vars.get("DB").or_else(|| vars.get("db"));
let ac = vars.get("AC").or_else(|| vars.get("ac"));
match (ns, db, ac) {
(Some(ns), Some(db), Some(ac)) => {
let ns = ns.to_raw_string();
let db = db.to_raw_string();
let ac = ac.to_raw_string();
super::signin::db_access(kvs, session, ns, db, ac, vars).await
}
(Some(ns), Some(db), None) => {
let user = vars.get("user");
let pass = vars.get("pass");
match (user, pass) {
(Some(user), Some(pass)) => {
let ns = ns.to_raw_string();
let db = db.to_raw_string();
let user = user.to_raw_string();
let pass = pass.to_raw_string();
super::signin::db_user(kvs, session, ns, db, user, pass).await
}
_ => Err(Error::MissingUserOrPass),
}
}
(Some(ns), None, Some(ac)) => {
let ns = ns.to_raw_string();
let ac = ac.to_raw_string();
super::signin::ns_access(kvs, session, ns, ac, vars).await
}
(Some(ns), None, None) => {
let user = vars.get("user");
let pass = vars.get("pass");
match (user, pass) {
(Some(user), Some(pass)) => {
let ns = ns.to_raw_string();
let user = user.to_raw_string();
let pass = pass.to_raw_string();
super::signin::ns_user(kvs, session, ns, user, pass).await
}
_ => Err(Error::MissingUserOrPass),
}
}
(None, None, None) => {
let user = vars.get("user");
let pass = vars.get("pass");
match (user, pass) {
(Some(user), Some(pass)) => {
let user = user.to_raw_string();
let pass = pass.to_raw_string();
super::signin::root_user(kvs, session, user, pass).await
}
_ => Err(Error::MissingUserOrPass),
}
}
_ => Err(Error::NoSigninTarget),
}
}
pub async fn db_access(
kvs: &Datastore,
session: &mut Session,
ns: String,
db: String,
ac: String,
vars: Object,
) -> Result<SigninData, Error> {
let tx = kvs.transaction(Read, Optimistic).await?;
let access = tx.get_db_access(&ns, &db, &ac).await;
tx.cancel().await?;
match access {
Ok(av) => {
match av.kind.clone() {
AccessType::Record(at) => {
let iss = match &at.jwt.issue {
Some(iss) => iss.clone(),
_ => return Err(Error::AccessMethodMismatch),
};
if let Some(bearer) = &at.bearer {
if let Some(key) = vars.get("refresh") {
return signin_bearer(
kvs,
session,
Some(ns),
Some(db),
av,
bearer,
key.to_raw_string(),
)
.await;
}
};
match &at.signin {
Some(val) => {
let vars = Some(vars.0);
let mut sess = Session::editor().with_ns(&ns).with_db(&db);
sess.ip.clone_from(&session.ip);
sess.or.clone_from(&session.or);
match kvs.evaluate(val, &sess, vars).await {
Ok(val) => {
match val.record() {
Some(mut rid) => {
let key = config(iss.alg, &iss.key)?;
let claims = Claims {
iss: Some(SERVER_NAME.to_owned()),
iat: Some(Utc::now().timestamp()),
nbf: Some(Utc::now().timestamp()),
exp: expiration(av.duration.token)?,
jti: Some(Uuid::new_v4().to_string()),
ns: Some(ns.to_owned()),
db: Some(db.to_owned()),
ac: Some(ac.to_owned()),
id: Some(rid.to_raw()),
..Claims::default()
};
if let Some(au) = &av.authenticate {
let mut sess =
Session::editor().with_ns(&ns).with_db(&db);
sess.rd = Some(rid.clone().into());
sess.tk = Some((&claims).into());
sess.ip.clone_from(&session.ip);
sess.or.clone_from(&session.or);
rid = authenticate_record(kvs, &sess, au).await?;
}
let refresh = match &at.bearer {
Some(_) => {
if !kvs.get_capabilities().allows_experimental(
&ExperimentalTarget::BearerAccess,
) {
debug!("Will not create refresh token with disabled bearer access feature");
None
} else {
Some(
create_refresh_token_record(
kvs,
av.name.clone(),
&ns,
&db,
rid.clone(),
)
.await?,
)
}
}
None => None,
};
trace!(
"Signing in to database with access method `{}`",
ac
);
let enc =
encode(&Header::new(iss.alg.into()), &claims, &key);
session.tk = Some((&claims).into());
session.ns = Some(ns.to_owned());
session.db = Some(db.to_owned());
session.ac = Some(ac.to_owned());
session.rd = Some(Value::from(rid.to_owned()));
session.exp = expiration(av.duration.session)?;
session.au = Arc::new(Auth::new(Actor::new(
rid.to_string(),
Default::default(),
Level::Record(ns, db, rid.to_string()),
)));
match enc {
Ok(token) => Ok(SigninData {
token,
refresh,
}),
_ => Err(Error::TokenMakingFailed),
}
}
_ => Err(Error::NoRecordFound),
}
}
Err(e) => match e {
Error::Thrown(_) => Err(e),
Error::Tx(_) | Error::TxFailure | Error::TxRetryable => {
debug!("Unexpected error found while executing a SIGNIN clause: {e}");
Err(Error::UnexpectedAuth)
}
e => {
debug!("Record user signin query failed: {e}");
if *INSECURE_FORWARD_ACCESS_ERRORS {
Err(e)
} else {
Err(Error::AccessRecordSigninQueryFailed)
}
}
},
}
}
_ => Err(Error::AccessRecordNoSignin),
}
}
AccessType::Bearer(at) => {
let key = match vars.get("key") {
Some(key) => key.to_raw_string(),
None => return Err(Error::AccessBearerMissingKey),
};
signin_bearer(kvs, session, Some(ns), Some(db), av, &at, key).await
}
_ => Err(Error::AccessMethodMismatch),
}
}
_ => Err(Error::AccessNotFound),
}
}
pub async fn db_user(
kvs: &Datastore,
session: &mut Session,
ns: String,
db: String,
user: String,
pass: String,
) -> Result<SigninData, Error> {
match verify_db_creds(kvs, &ns, &db, &user, &pass).await {
Ok(u) => {
let key = EncodingKey::from_secret(u.code.as_ref());
let val = Claims {
iss: Some(SERVER_NAME.to_owned()),
iat: Some(Utc::now().timestamp()),
nbf: Some(Utc::now().timestamp()),
exp: expiration(u.duration.token)?,
jti: Some(Uuid::new_v4().to_string()),
ns: Some(ns.to_owned()),
db: Some(db.to_owned()),
id: Some(user),
..Claims::default()
};
trace!("Signing in to database `{ns}/{db}`");
let enc = encode(&HEADER, &val, &key);
session.tk = Some((&val).into());
session.ns = Some(ns.to_owned());
session.db = Some(db.to_owned());
session.exp = expiration(u.duration.session)?;
session.au = Arc::new((&u, Level::Database(ns.to_owned(), db.to_owned())).try_into()?);
match enc {
Ok(tk) => Ok(SigninData {
token: tk,
refresh: None,
}),
_ => Err(Error::TokenMakingFailed),
}
}
Err(e) => {
debug!("Failed to verify signin credentials for user `{user}` in database `{ns}/{db}`: {e}");
Err(Error::InvalidAuth)
}
}
}
pub async fn ns_access(
kvs: &Datastore,
session: &mut Session,
ns: String,
ac: String,
vars: Object,
) -> Result<SigninData, Error> {
let tx = kvs.transaction(Read, Optimistic).await?;
let access = tx.get_ns_access(&ns, &ac).await;
tx.cancel().await?;
match access {
Ok(av) => {
match av.kind.clone() {
AccessType::Bearer(at) => {
let key = match vars.get("key") {
Some(key) => key.to_raw_string(),
None => return Err(Error::AccessBearerMissingKey),
};
signin_bearer(kvs, session, Some(ns), None, av, &at, key).await
}
_ => Err(Error::AccessMethodMismatch),
}
}
_ => Err(Error::AccessNotFound),
}
}
pub async fn ns_user(
kvs: &Datastore,
session: &mut Session,
ns: String,
user: String,
pass: String,
) -> Result<SigninData, Error> {
match verify_ns_creds(kvs, &ns, &user, &pass).await {
Ok(u) => {
let key = EncodingKey::from_secret(u.code.as_ref());
let val = Claims {
iss: Some(SERVER_NAME.to_owned()),
iat: Some(Utc::now().timestamp()),
nbf: Some(Utc::now().timestamp()),
exp: expiration(u.duration.token)?,
jti: Some(Uuid::new_v4().to_string()),
ns: Some(ns.to_owned()),
id: Some(user),
..Claims::default()
};
trace!("Signing in to namespace `{ns}`");
let enc = encode(&HEADER, &val, &key);
session.tk = Some((&val).into());
session.ns = Some(ns.to_owned());
session.exp = expiration(u.duration.session)?;
session.au = Arc::new((&u, Level::Namespace(ns.to_owned())).try_into()?);
match enc {
Ok(tk) => Ok(SigninData {
token: tk,
refresh: None,
}),
_ => Err(Error::TokenMakingFailed),
}
}
Err(e) => {
debug!(
"Failed to verify signin credentials for user `{user}` in namespace `{ns}`: {e}"
);
Err(Error::InvalidAuth)
}
}
}
pub async fn root_user(
kvs: &Datastore,
session: &mut Session,
user: String,
pass: String,
) -> Result<SigninData, Error> {
match verify_root_creds(kvs, &user, &pass).await {
Ok(u) => {
let key = EncodingKey::from_secret(u.code.as_ref());
let val = Claims {
iss: Some(SERVER_NAME.to_owned()),
iat: Some(Utc::now().timestamp()),
nbf: Some(Utc::now().timestamp()),
exp: expiration(u.duration.token)?,
jti: Some(Uuid::new_v4().to_string()),
id: Some(user),
..Claims::default()
};
trace!("Signing in as root");
let enc = encode(&HEADER, &val, &key);
session.tk = Some(val.into());
session.exp = expiration(u.duration.session)?;
session.au = Arc::new((&u, Level::Root).try_into()?);
match enc {
Ok(tk) => Ok(SigninData {
token: tk,
refresh: None,
}),
_ => Err(Error::TokenMakingFailed),
}
}
Err(e) => {
debug!("Failed to verify signin credentials for user `{user}` in root: {e}");
Err(Error::InvalidAuth)
}
}
}
pub async fn root_access(
kvs: &Datastore,
session: &mut Session,
ac: String,
vars: Object,
) -> Result<SigninData, Error> {
let tx = kvs.transaction(Read, Optimistic).await?;
let access = tx.get_root_access(&ac).await;
tx.cancel().await?;
match access {
Ok(av) => {
match av.kind.clone() {
AccessType::Bearer(at) => {
let key = match vars.get("key") {
Some(key) => key.to_raw_string(),
None => return Err(Error::AccessBearerMissingKey),
};
signin_bearer(kvs, session, None, None, av, &at, key).await
}
_ => Err(Error::AccessMethodMismatch),
}
}
_ => Err(Error::AccessNotFound),
}
}
pub async fn signin_bearer(
kvs: &Datastore,
session: &mut Session,
ns: Option<String>,
db: Option<String>,
av: Arc<DefineAccessStatement>,
at: &access_type::BearerAccess,
key: String,
) -> Result<SigninData, Error> {
if !kvs.get_capabilities().allows_experimental(&ExperimentalTarget::BearerAccess) {
debug!("Error attempting to authenticate with disabled bearer access feature");
return Err(Error::InvalidAuth);
}
let iss = match &at.jwt.issue {
Some(iss) => iss.clone(),
_ => return Err(Error::AccessMethodMismatch),
};
let kid = validate_grant_bearer(&key)?;
let tx = kvs.transaction(Read, Optimistic).await?;
let gr = match (&ns, &db) {
(Some(ns), Some(db)) => tx.get_db_access_grant(ns, db, &av.name, &kid).await,
(Some(ns), None) => tx.get_ns_access_grant(ns, &av.name, &kid).await,
(None, None) => tx.get_root_access_grant(&av.name, &kid).await,
(None, Some(_)) => return Err(Error::NsEmpty),
}
.map_err(|e| {
debug!("Error retrieving bearer access grant: {e}");
Error::InvalidAuth
})?;
tx.cancel().await?;
verify_grant_bearer(&gr, key)?;
let roles = if let access::Subject::User(user) = &gr.subject {
let tx = kvs.transaction(Read, Optimistic).await?;
let user = match (&ns, &db) {
(Some(ns), Some(db)) => tx.get_db_user(ns, db, user).await.map_err(|e| {
debug!("Error retrieving user for bearer access to database `{ns}/{db}`: {e}");
Error::InvalidAuth
}),
(Some(ns), None) => tx.get_ns_user(ns, user).await.map_err(|e| {
debug!("Error retrieving user for bearer access to namespace `{ns}`: {e}");
Error::InvalidAuth
}),
(None, None) => tx.get_root_user(user).await.map_err(|e| {
debug!("Error retrieving user for bearer access to root: {e}");
Error::InvalidAuth
}),
(None, Some(_)) => return Err(Error::NsEmpty),
}?;
tx.cancel().await?;
user.roles.clone()
} else {
vec![]
};
let key = config(iss.alg, &iss.key)?;
let claims = Claims {
iss: Some(SERVER_NAME.to_owned()),
iat: Some(Utc::now().timestamp()),
nbf: Some(Utc::now().timestamp()),
exp: expiration(av.duration.token)?,
jti: Some(Uuid::new_v4().to_string()),
ns: ns.to_owned(),
db: db.to_owned(),
ac: Some(av.name.to_string()),
id: match &gr.subject {
access::Subject::User(user) => Some(user.to_raw()),
access::Subject::Record(rid) => Some(rid.to_raw()),
},
roles: match &gr.subject {
access::Subject::User(_) => Some(roles.iter().map(|v| v.to_string()).collect()),
access::Subject::Record(_) => Default::default(),
},
..Claims::default()
};
if let Some(au) = &av.authenticate {
let mut sess = match (&ns, &db) {
(Some(ns), Some(db)) => Session::editor().with_ns(ns).with_db(db),
(Some(ns), None) => Session::editor().with_ns(ns),
(None, None) => Session::editor(),
(None, Some(_)) => return Err(Error::NsEmpty),
};
sess.tk = Some((&claims).into());
sess.ip.clone_from(&session.ip);
sess.or.clone_from(&session.or);
authenticate_generic(kvs, &sess, au).await?;
}
let refresh = match at.kind {
access_type::BearerAccessType::Refresh => {
match &gr.subject {
access::Subject::Record(rid) => {
if let (Some(ns), Some(db)) = (&ns, &db) {
revoke_refresh_token_record(kvs, gr.id.clone(), gr.ac.clone(), ns, db)
.await?;
let refresh =
create_refresh_token_record(kvs, gr.ac.clone(), ns, db, rid.clone())
.await?;
Some(refresh)
} else {
debug!("Invalid attempt to authenticate as a record without a namespace and database");
return Err(Error::InvalidAuth);
}
}
access::Subject::User(_) => {
debug!(
"Invalid attempt to authenticatea as a system user with a refresh token"
);
return Err(Error::InvalidAuth);
}
}
}
_ => None,
};
trace!("Signing in to database with bearer access method `{}`", av.name);
let enc = encode(&Header::new(iss.alg.into()), &claims, &key);
session.tk = Some((&claims).into());
session.ns = ns.to_owned();
session.db = db.to_owned();
session.ac = Some(av.name.to_string());
session.exp = expiration(av.duration.session)?;
match &gr.subject {
access::Subject::User(user) => {
session.au = Arc::new(Auth::new(Actor::new(
user.to_string(),
roles.iter().map(Role::try_from).collect::<Result<_, _>>()?,
match (ns, db) {
(Some(ns), Some(db)) => Level::Database(ns, db),
(Some(ns), None) => Level::Namespace(ns),
(None, None) => Level::Root,
(None, Some(_)) => return Err(Error::NsEmpty),
},
)));
}
access::Subject::Record(rid) => {
session.au = Arc::new(Auth::new(Actor::new(
rid.to_string(),
Default::default(),
if let (Some(ns), Some(db)) = (ns, db) {
Level::Record(ns, db, rid.to_string())
} else {
debug!("Invalid attempt to authenticate as a record without a namespace and database");
return Err(Error::InvalidAuth);
},
)));
session.rd = Some(Value::from(rid.to_owned()));
}
};
match enc {
Ok(token) => Ok(SigninData {
token,
refresh,
}),
_ => Err(Error::TokenMakingFailed),
}
}
pub fn validate_grant_bearer(key: &str) -> Result<String, Error> {
let parts: Vec<&str> = key.split("-").collect();
if parts.len() != 4 {
return Err(Error::AccessGrantBearerInvalid);
}
access_type::BearerAccessType::from_str(parts[1])?;
let kid = parts[2];
if kid.len() != access::GRANT_BEARER_ID_LENGTH {
return Err(Error::AccessGrantBearerInvalid);
};
let key = parts[3];
if key.len() != access::GRANT_BEARER_KEY_LENGTH {
return Err(Error::AccessGrantBearerInvalid);
};
Ok(kid.to_string())
}
pub fn verify_grant_bearer(
gr: &Arc<AccessGrant>,
key: String,
) -> Result<&access::GrantBearer, Error> {
match (&gr.expiration, &gr.revocation) {
(None, None) => {}
(Some(exp), None) => {
if exp < &Datetime::default() {
debug!("Bearer access grant `{}` for method `{}` is expired", gr.id, gr.ac);
return Err(Error::InvalidAuth);
}
}
(_, Some(_)) => {
debug!("Bearer access grant `{}` for method `{}` is revoked", gr.id, gr.ac);
return Err(Error::InvalidAuth);
}
}
match &gr.grant {
access::Grant::Bearer(bearer) => {
let mut hasher = Sha256::new();
hasher.update(key);
let hash = hasher.finalize();
let hash_hex = format!("{hash:x}");
let signin_key_bytes: &[u8] = hash_hex.as_bytes();
let bearer_key_bytes: &[u8] = bearer.key.as_bytes();
let ok: bool = bearer_key_bytes.ct_eq(signin_key_bytes).into();
if ok {
Ok(bearer)
} else {
debug!("Bearer access grant `{}` for method `{}` is invalid", gr.id, gr.ac);
Err(Error::InvalidAuth)
}
}
_ => Err(Error::AccessMethodMismatch),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{dbs::Capabilities, iam::Role};
use chrono::Duration;
use jsonwebtoken::{decode, Algorithm, DecodingKey, Validation};
use regex::Regex;
use std::collections::HashMap;
struct TestLevel {
level: &'static str,
ns: Option<&'static str>,
db: Option<&'static str>,
}
const AVAILABLE_ROLES: [Role; 3] = [Role::Viewer, Role::Editor, Role::Owner];
#[tokio::test]
async fn test_signin_record() {
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
SIGNUP (
CREATE user CONTENT {
name: $user,
pass: crypto::argon2::generate($pass)
}
)
DURATION FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to signin with credentials: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
}
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
SIGNUP (
CREATE user CONTENT {
name: $user,
pass: crypto::argon2::generate($pass)
}
)
DURATION FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "incorrect".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
assert!(res.is_err(), "Unexpected successful signin: {:?}", res);
}
}
#[tokio::test]
async fn test_signin_record_with_refresh() {
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
DURATION FOR GRANT 1w, FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
match res {
Ok(data) => {
assert!(data.refresh.is_none(), "Refresh token was unexpectedly returned")
}
Err(e) => panic!("Failed to signin with credentials: {e}"),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
WITH REFRESH
DURATION FOR GRANT 1w, FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to signin with credentials: {:?}", res);
let refresh = match res {
Ok(data) => match data.refresh {
Some(refresh) => refresh,
None => panic!("Refresh token was not returned"),
},
Err(e) => panic!("Failed to signin with credentials: {e}"),
};
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("refresh", refresh.clone().into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
match res {
Ok(data) => match data.refresh {
Some(new_refresh) => assert!(
new_refresh != refresh,
"New refresh token is identical to used one"
),
None => panic!("Refresh token was not returned"),
},
Err(e) => panic!("Failed to signin with credentials: {e}"),
};
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("refresh", refresh.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
match res {
Ok(data) => panic!("Unexpected successful signin: {:?}", data),
Err(Error::InvalidAuth) => {} Err(e) => panic!("Expected InvalidAuth, but got: {e}"),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
WITH REFRESH
DURATION FOR GRANT 1s, FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
let refresh = match res {
Ok(data) => match data.refresh {
Some(refresh) => refresh,
None => panic!("Refresh token was not returned"),
},
Err(e) => panic!("Failed to signin with credentials: {e}"),
};
std::thread::sleep(Duration::seconds(2).to_std().unwrap());
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("refresh", refresh.clone().into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
match res {
Ok(data) => panic!("Unexpected successful signin: {:?}", data),
Err(Error::InvalidAuth) => {} Err(e) => panic!("Expected InvalidAuth, but got: {e}"),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
WITH REFRESH
DURATION FOR GRANT 1w, FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to signin with credentials: {:?}", res);
let refresh = match res {
Ok(data) => match data.refresh {
Some(refresh) => refresh,
None => panic!("Refresh token was not returned"),
},
Err(e) => panic!("Failed to signin with credentials: {e}"),
};
let id = refresh.split("-").collect::<Vec<&str>>()[2];
let ok = Regex::new(r"surreal-refresh-[a-zA-Z0-9]{12}-[a-zA-Z0-9]{24}").unwrap();
assert!(ok.is_match(&refresh), "Output '{}' doesn't match regex '{}'", refresh, ok);
let tx = ds.transaction(Read, Optimistic).await.unwrap().enclose();
let grant = tx.get_db_access_grant("test", "test", "user", id).await.unwrap();
let key = match &grant.grant {
access::Grant::Bearer(grant) => grant.key.clone(),
_ => panic!("Incorrect grant type returned, expected a bearer grant"),
};
tx.cancel().await.unwrap();
let ok = Regex::new(r"[0-9a-f]{64}").unwrap();
assert!(ok.is_match(&key), "Output '{}' doesn't match regex '{}'", key, ok);
}
}
#[tokio::test]
async fn test_signin_record_with_jwt_issuer() {
{
let public_key = r#"-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1SU1LfVLPHCozMxH2Mo
4lgOEePzNm0tRgeLezV6ffAt0gunVTLw7onLRnrq0/IzW7yWR7QkrmBL7jTKEn5u
+qKhbwKfBstIs+bMY2Zkp18gnTxKLxoS2tFczGkPLPgizskuemMghRniWaoLcyeh
kd3qqGElvW/VDL5AaWTg0nLVkjRo9z+40RQzuVaE8AkAFmxZzow3x+VJYKdjykkJ
0iT9wCS0DRTXu269V264Vf/3jvredZiKRkgwlL9xNAwxXFg0x/XFw005UWVRIkdg
cKWTjpBP2dPwVZ4WWC+9aGVd+Gyn1o0CLelf4rEjGoXbAAEgAqeGUxrcIlbjXfbc
mwIDAQAB
-----END PUBLIC KEY-----"#;
let private_key = r#"-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQC7VJTUt9Us8cKj
MzEfYyjiWA4R4/M2bS1GB4t7NXp98C3SC6dVMvDuictGeurT8jNbvJZHtCSuYEvu
NMoSfm76oqFvAp8Gy0iz5sxjZmSnXyCdPEovGhLa0VzMaQ8s+CLOyS56YyCFGeJZ
qgtzJ6GR3eqoYSW9b9UMvkBpZODSctWSNGj3P7jRFDO5VoTwCQAWbFnOjDfH5Ulg
p2PKSQnSJP3AJLQNFNe7br1XbrhV//eO+t51mIpGSDCUv3E0DDFcWDTH9cXDTTlR
ZVEiR2BwpZOOkE/Z0/BVnhZYL71oZV34bKfWjQIt6V/isSMahdsAASACp4ZTGtwi
VuNd9tybAgMBAAECggEBAKTmjaS6tkK8BlPXClTQ2vpz/N6uxDeS35mXpqasqskV
laAidgg/sWqpjXDbXr93otIMLlWsM+X0CqMDgSXKejLS2jx4GDjI1ZTXg++0AMJ8
sJ74pWzVDOfmCEQ/7wXs3+cbnXhKriO8Z036q92Qc1+N87SI38nkGa0ABH9CN83H
mQqt4fB7UdHzuIRe/me2PGhIq5ZBzj6h3BpoPGzEP+x3l9YmK8t/1cN0pqI+dQwY
dgfGjackLu/2qH80MCF7IyQaseZUOJyKrCLtSD/Iixv/hzDEUPfOCjFDgTpzf3cw
ta8+oE4wHCo1iI1/4TlPkwmXx4qSXtmw4aQPz7IDQvECgYEA8KNThCO2gsC2I9PQ
DM/8Cw0O983WCDY+oi+7JPiNAJwv5DYBqEZB1QYdj06YD16XlC/HAZMsMku1na2T
N0driwenQQWzoev3g2S7gRDoS/FCJSI3jJ+kjgtaA7Qmzlgk1TxODN+G1H91HW7t
0l7VnL27IWyYo2qRRK3jzxqUiPUCgYEAx0oQs2reBQGMVZnApD1jeq7n4MvNLcPv
t8b/eU9iUv6Y4Mj0Suo/AU8lYZXm8ubbqAlwz2VSVunD2tOplHyMUrtCtObAfVDU
AhCndKaA9gApgfb3xw1IKbuQ1u4IF1FJl3VtumfQn//LiH1B3rXhcdyo3/vIttEk
48RakUKClU8CgYEAzV7W3COOlDDcQd935DdtKBFRAPRPAlspQUnzMi5eSHMD/ISL
DY5IiQHbIH83D4bvXq0X7qQoSBSNP7Dvv3HYuqMhf0DaegrlBuJllFVVq9qPVRnK
xt1Il2HgxOBvbhOT+9in1BzA+YJ99UzC85O0Qz06A+CmtHEy4aZ2kj5hHjECgYEA
mNS4+A8Fkss8Js1RieK2LniBxMgmYml3pfVLKGnzmng7H2+cwPLhPIzIuwytXywh
2bzbsYEfYx3EoEVgMEpPhoarQnYPukrJO4gwE2o5Te6T5mJSZGlQJQj9q4ZB2Dfz
et6INsK0oG8XVGXSpQvQh3RUYekCZQkBBFcpqWpbIEsCgYAnM3DQf3FJoSnXaMhr
VBIovic5l0xFkEHskAjFTevO86Fsz1C2aSeRKSqGFoOQ0tmJzBEs1R6KqnHInicD
TQrKhArgLXX4v3CddjfTRJkFWDbE/CkvKZNOrcf1nhaGCPspRJj2KUkj1Fhl9Cnc
dn/RsYEONbwQSjIfMPkvxF+8HQ==
-----END PRIVATE KEY-----"#;
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
&format!(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass)
)
SIGNUP (
CREATE user CONTENT {{
name: $user,
pass: crypto::argon2::generate($pass)
}}
)
WITH JWT ALGORITHM RS256 KEY '{public_key}'
WITH ISSUER KEY '{private_key}'
DURATION FOR SESSION 2h, FOR TOKEN 15m
;
CREATE user:test CONTENT {{
name: 'user',
pass: crypto::argon2::generate('pass')
}}
"#
),
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to signin with credentials: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_sess_exp =
(Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_sess_exp =
(Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_sess_exp && exp < max_sess_exp,
"Session expiration is expected to follow the defined duration"
);
if let Ok(sd) = res {
let val = Validation::new(Algorithm::RS256);
let token_data = decode::<Claims>(
&sd.token,
&DecodingKey::from_rsa_pem(public_key.as_ref()).unwrap(),
&val,
)
.unwrap();
assert_eq!(token_data.header.alg, Algorithm::RS256);
let exp = match token_data.claims.exp {
Some(exp) => exp,
_ => panic!("Token is missing expiration claim"),
};
let min_tk_exp =
(Utc::now() + Duration::minutes(15) - Duration::seconds(10)).timestamp();
let max_tk_exp =
(Utc::now() + Duration::minutes(15) + Duration::seconds(10)).timestamp();
assert!(
exp > min_tk_exp && exp < max_tk_exp,
"Token expiration is expected to follow the defined duration"
);
assert_eq!(token_data.claims.ns, Some("test".to_string()));
assert_eq!(token_data.claims.db, Some("test".to_string()));
assert_eq!(token_data.claims.id, Some("user:test".to_string()));
assert_eq!(token_data.claims.ac, Some("user".to_string()));
} else {
panic!("Token could not be extracted from result")
}
}
}
#[tokio::test]
async fn test_signin_user() {
#[derive(Debug)]
struct TestCase {
title: &'static str,
password: &'static str,
roles: Vec<Role>,
token_expiration: Option<Duration>,
session_expiration: Option<Duration>,
expect_ok: bool,
}
let test_cases = vec![
TestCase {
title: "without roles or expiration",
password: "pass",
roles: vec![Role::Viewer],
token_expiration: None,
session_expiration: None,
expect_ok: true,
},
TestCase {
title: "with roles and expiration",
password: "pass",
roles: vec![Role::Editor, Role::Owner],
token_expiration: Some(Duration::days(365)),
session_expiration: Some(Duration::days(1)),
expect_ok: true,
},
TestCase {
title: "with invalid password",
password: "invalid",
roles: vec![],
token_expiration: None,
session_expiration: None,
expect_ok: false,
},
];
let test_levels = vec![
TestLevel {
level: "ROOT",
ns: None,
db: None,
},
TestLevel {
level: "NS",
ns: Some("test"),
db: None,
},
TestLevel {
level: "DB",
ns: Some("test"),
db: Some("test"),
},
];
for level in &test_levels {
for case in &test_cases {
println!("Test case: {} level {}", level.level, case.title);
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
let roles_clause = if case.roles.is_empty() {
String::new()
} else {
let roles: Vec<&str> = case
.roles
.iter()
.map(|r| match r {
Role::Viewer => "VIEWER",
Role::Editor => "EDITOR",
Role::Owner => "OWNER",
})
.collect();
format!("ROLES {}", roles.join(", "))
};
let mut duration_clause = String::new();
if case.token_expiration.is_some() || case.session_expiration.is_some() {
duration_clause = "DURATION".to_owned()
}
if let Some(duration) = case.token_expiration {
duration_clause =
format!("{} FOR TOKEN {}s", duration_clause, duration.num_seconds())
}
if let Some(duration) = case.session_expiration {
duration_clause =
format!("{} FOR SESSION {}s", duration_clause, duration.num_seconds())
}
let define_user_query = format!(
"DEFINE USER user ON {} PASSWORD 'pass' {} {}",
level.level, roles_clause, duration_clause,
);
ds.execute(&define_user_query, &sess, None).await.unwrap();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let res = match level.level {
"ROOT" => {
root_user(&ds, &mut sess, "user".to_string(), case.password.to_string())
.await
}
"NS" => {
ns_user(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"user".to_string(),
case.password.to_string(),
)
.await
}
"DB" => {
db_user(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"user".to_string(),
case.password.to_string(),
)
.await
}
_ => panic!("Unsupported level"),
};
if case.expect_ok {
assert!(res.is_ok(), "Failed to signin: {:?}", res);
assert_eq!(sess.ns, level.ns.map(|s| s.to_string()));
assert_eq!(sess.db, level.db.map(|s| s.to_string()));
assert_eq!(sess.au.level().ns(), level.ns);
assert_eq!(sess.au.level().db(), level.db);
assert_eq!(sess.au.id(), "user");
match level.level {
"ROOT" => assert!(sess.au.is_root()),
"NS" => assert!(sess.au.is_ns()),
"DB" => assert!(sess.au.is_db()),
_ => panic!("Unsupported level"),
}
for role in AVAILABLE_ROLES {
let has_role = sess.au.has_role(role);
let should_have_role = case.roles.contains(&role);
assert_eq!(has_role, should_have_role, "Role {:?} check failed", role);
}
if let Some(exp_duration) = case.session_expiration {
let exp = sess.exp.unwrap();
let min_exp =
(Utc::now() + exp_duration - Duration::seconds(10)).timestamp();
let max_exp =
(Utc::now() + exp_duration + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to match the defined duration"
);
} else {
assert_eq!(sess.exp, None, "Session expiration is expected to be None");
}
if let Ok(sd) = res {
let token_data =
decode::<Claims>(&sd.token, &DecodingKey::from_secret(&[]), &{
let mut validation =
Validation::new(jsonwebtoken::Algorithm::HS256);
validation.insecure_disable_signature_validation();
validation.validate_nbf = false;
validation.validate_exp = false;
validation
})
.unwrap();
if let Some(exp_duration) = case.token_expiration {
let exp = match token_data.claims.exp {
Some(exp) => exp,
_ => panic!("Token is missing expiration claim"),
};
let min_exp =
(Utc::now() + exp_duration - Duration::seconds(10)).timestamp();
let max_exp =
(Utc::now() + exp_duration + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to match the defined duration"
);
} else {
assert_eq!(sess.exp, None, "Session expiration is expected to be None");
}
assert_eq!(token_data.claims.ns, level.ns.map(|s| s.to_string()));
assert_eq!(token_data.claims.db, level.db.map(|s| s.to_string()));
assert_eq!(token_data.claims.id, Some("user".to_string()));
} else {
panic!("Token could not be extracted from result")
}
} else {
assert!(res.is_err(), "Unexpected successful signin: {:?}", res);
}
}
}
}
#[tokio::test]
async fn test_signin_record_and_authenticate_clause() {
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM type::thing('user', $id)
)
AUTHENTICATE (
-- Simple example increasing the record identifier by one
SELECT * FROM type::thing('user', record::id($auth) + 1)
)
DURATION FOR SESSION 2h
;
CREATE user:1, user:2;
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("id", 1.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to signin with credentials: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.ac, Some("user".to_string()));
assert_eq!(sess.au.id(), "user:2");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:2"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
}
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS owner ON DATABASE TYPE RECORD
SIGNUP (
-- Allow anyone to sign up as a new company
-- This automatically creates an owner with the same credentials
CREATE company CONTENT {
email: $email,
pass: crypto::argon2::generate($pass),
owner: (CREATE employee CONTENT {
email: $email,
pass: $pass,
}),
}
)
SIGNIN (
-- Allow company owners to log in directly with the company account
SELECT * FROM company WHERE email = $email AND crypto::argon2::compare(pass, $pass)
)
AUTHENTICATE (
-- If logging in with a company account, the session will be authenticated as the first owner
IF record::tb($auth) = "company" {
RETURN SELECT VALUE owner FROM company WHERE id = $auth
}
)
DURATION FOR SESSION 2h
;
CREATE company:1 CONTENT {
email: "info@example.com",
pass: crypto::argon2::generate("company-password"),
owner: employee:2,
};
CREATE employee:1 CONTENT {
email: "member@example.com",
pass: crypto::argon2::generate("member-password"),
};
CREATE employee:2 CONTENT {
email: "owner@example.com",
pass: crypto::argon2::generate("owner-password"),
};
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("email", "info@example.com".into());
vars.insert("pass", "company-password".into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"owner".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to signin with credentials: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.ac, Some("owner".to_string()));
assert_eq!(sess.au.id(), "employee:2");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("employee:2"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
}
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM type::thing('user', $id)
)
AUTHENTICATE {
-- Not just signin, this clause runs across signin, signup and authenticate, which makes it a nice place to centralize logic
IF !$auth.enabled {
THROW "This user is not enabled";
};
-- Always need to return the user id back, otherwise auth generically fails
RETURN $auth;
}
DURATION FOR SESSION 2h
;
CREATE user:1 SET enabled = false;
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("id", 1.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::Thrown(e)) if e == "This user is not enabled" => {} res => panic!(
"Expected authentication to failed due to user not being enabled, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM type::thing('user', $id)
)
AUTHENTICATE {}
DURATION FOR SESSION 2h
;
CREATE user:1;
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("id", 1.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected authentication to generally fail, but instead received: {:?}",
res
),
}
}
}
#[tokio::test]
#[ignore = "flaky"]
async fn test_signin_record_transaction_conflict() {
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN {
-- Concurrently write to the same document
UPSERT count:1 SET count += 1;
-- Increase the duration of the transaction
sleep(500ms);
-- Continue with authentication
RETURN (SELECT * FROM user WHERE name = $user AND crypto::argon2::compare(pass, $pass))
}
SIGNUP (
CREATE user CONTENT {
name: $user,
pass: crypto::argon2::generate($pass)
}
)
DURATION FOR SESSION 2h
;
CREATE user:test CONTENT {
name: 'user',
pass: crypto::argon2::generate('pass')
}
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess1 = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut sess2 = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("user", "user".into());
vars.insert("pass", "pass".into());
let (res1, res2) = tokio::join!(
db_access(
&ds,
&mut sess1,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.clone().into(),
),
db_access(
&ds,
&mut sess2,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
);
match (res1, res2) {
(Ok(r1), Ok(r2)) => panic!("Expected authentication to fail in one instance, but instead received: {:?} and {:?}", r1, r2),
(Err(e1), Err(e2)) => panic!("Expected authentication to fail in one instance, but instead received: {:?} and {:?}", e1, e2),
(Err(e1), Ok(_)) => match &e1 {
Error::UnexpectedAuth => {} e => panic!("Expected authentication to return an UnexpectedAuth error, but insted got: {e}")
}
(Ok(_), Err(e2)) => match &e2 {
Error::UnexpectedAuth => {} e => panic!("Expected authentication to return an UnexpectedAuth error, but insted got: {e}")
}
}
}
{
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
ds.execute(
r#"
DEFINE ACCESS user ON DATABASE TYPE RECORD
SIGNIN (
SELECT * FROM type::thing('user', $id)
)
AUTHENTICATE {
-- Concurrently write to the same document
UPSERT count:1 SET count += 1;
-- Increase the duration of the transaction
sleep(500ms);
-- Continue with authentication
$auth.id -- Continue with authentication
}
DURATION FOR SESSION 2h
;
CREATE user:1;
"#,
&sess,
None,
)
.await
.unwrap();
let mut sess1 = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut sess2 = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("id", 1.into());
let (res1, res2) = tokio::join!(
db_access(
&ds,
&mut sess1,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.clone().into(),
),
db_access(
&ds,
&mut sess2,
"test".to_string(),
"test".to_string(),
"user".to_string(),
vars.into(),
)
);
match (res1, res2) {
(Ok(r1), Ok(r2)) => panic!("Expected authentication to fail in one instance, but instead received: {:?} and {:?}", r1, r2),
(Err(e1), Err(e2)) => panic!("Expected authentication to fail in one instance, but instead received: {:?} and {:?}", e1, e2),
(Err(e1), Ok(_)) => match &e1 {
Error::UnexpectedAuth => {} e => panic!("Expected authentication to return an UnexpectedAuth error, but insted got: {e}")
}
(Ok(_), Err(e2)) => match &e2 {
Error::UnexpectedAuth => {} e => panic!("Expected authentication to return an UnexpectedAuth error, but insted got: {e}")
}
}
}
}
#[tokio::test]
async fn test_signin_bearer_for_user() {
let test_levels = vec![
TestLevel {
level: "ROOT",
ns: None,
db: None,
},
TestLevel {
level: "NS",
ns: Some("test"),
db: None,
},
TestLevel {
level: "DB",
ns: Some("test"),
db: Some("test"),
},
];
let plain_text_regex =
Regex::new("surreal-bearer-[a-zA-Z0-9]{12}-[a-zA-Z0-9]{24}").unwrap();
let sha_256_regex = Regex::new(r"[0-9a-f]{64}").unwrap();
for level in &test_levels {
println!("Test level: {}", level.level);
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
assert!(res.is_ok(), "Failed to sign in with bearer key: {:?}", res);
assert_eq!(sess.ns, level.ns.map(|s| s.to_string()));
assert_eq!(sess.db, level.db.map(|s| s.to_string()));
match level.level {
"ROOT" => assert!(sess.au.is_root()),
"NS" => assert!(sess.au.is_ns()),
"DB" => assert!(sess.au.is_db()),
_ => panic!("Unsupported level"),
}
assert_eq!(sess.au.level().ns(), level.ns);
assert_eq!(sess.au.level().db(), level.db);
assert!(
!sess.au.has_role(Role::Viewer),
"Auth user expected to not have Viewer role"
);
assert!(
sess.au.has_role(Role::Editor),
"Auth user expected to have Editor role"
);
assert!(
!sess.au.has_role(Role::Owner),
"Auth user expected to not have Owner role"
);
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to match the defined duration",
);
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
AUTHENTICATE {{
RETURN NONE
}}
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
assert!(res.is_ok(), "Failed to sign in with bearer key: {:?}", res);
assert_eq!(sess.ns, level.ns.map(|s| s.to_string()));
assert_eq!(sess.db, level.db.map(|s| s.to_string()));
match level.level {
"ROOT" => assert!(sess.au.is_root()),
"NS" => assert!(sess.au.is_ns()),
"DB" => assert!(sess.au.is_db()),
_ => panic!("Unsupported level"),
}
assert_eq!(sess.au.level().ns(), level.ns);
assert_eq!(sess.au.level().db(), level.db);
assert!(
!sess.au.has_role(Role::Viewer),
"Auth user expected to not have Viewer role"
);
assert!(
sess.au.has_role(Role::Editor),
"Auth user expected to have Editor role"
);
assert!(
!sess.au.has_role(Role::Owner),
"Auth user expected to not have Owner role"
);
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to match the defined duration",
);
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
AUTHENTICATE {{
THROW "Test authentication error";
}}
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level,
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::Thrown(e)) => {
assert_eq!(e, "Test authentication error")
}
res => panic!(
"Expected a thrown authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR GRANT 1s FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
std::thread::sleep(Duration::seconds(2).to_std().unwrap());
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR GRANT 1s FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let kid = key.split("-").collect::<Vec<&str>>()[2];
ds.execute(
&format!("ACCESS api ON {} REVOKE GRANT {kid}", level.level),
&sess,
None,
)
.await
.unwrap();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR GRANT 1s FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
ds.execute(format!("REMOVE ACCESS api ON {}", level.level).as_str(), &sess, None)
.await
.unwrap();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::AccessNotFound) => {} res => panic!(
"Expected an access method not found error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let _key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let vars: HashMap<&str, Value> = HashMap::new();
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::AccessBearerMissingKey) => {} res => panic!(
"Expected a missing key authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key["surreal-".len() + 2] = '_';
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::AccessGrantBearerInvalid) => {} res => panic!(
"Expected an invalid key authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key.truncate(invalid_key.len() - 1);
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::AccessGrantBearerInvalid) => {} res => panic!(
"Expected an invalid key authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key[access_type::BearerAccessType::Bearer.prefix().len() + 2] = '_';
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key[valid_key.len() - 2] = '_';
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = match level.level {
"DB" => {
db_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"NS" => {
ns_access(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"api".to_string(),
vars.into(),
)
.await
}
"ROOT" => root_access(&ds, &mut sess, "api".to_string(), vars.into()).await,
_ => panic!("Unsupported level"),
};
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default()
.with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
&format!(
r#"
DEFINE ACCESS api ON {} TYPE BEARER FOR USER
DURATION FOR SESSION 2h
;
DEFINE USER tobie ON {} ROLES EDITOR;
ACCESS api ON {} GRANT FOR USER tobie;
"#,
level.level, level.level, level.level
),
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let id = grant.get("id").unwrap().clone().as_string();
let key = grant.get("key").unwrap().clone().as_string();
assert!(
plain_text_regex.is_match(&key),
"Output '{}' doesn't match regex '{}'",
key,
plain_text_regex
);
let tx = ds.transaction(Read, Optimistic).await.unwrap().enclose();
let grant = match level.level {
"DB" => tx
.get_db_access_grant(level.ns.unwrap(), level.db.unwrap(), "api", &id)
.await
.unwrap(),
"NS" => tx.get_ns_access_grant(level.ns.unwrap(), "api", &id).await.unwrap(),
"ROOT" => tx.get_root_access_grant("api", &id).await.unwrap(),
_ => panic!("Unsupported level"),
};
let key = match &grant.grant {
access::Grant::Bearer(grant) => grant.key.clone(),
_ => panic!("Incorrect grant type returned, expected a bearer grant"),
};
tx.cancel().await.unwrap();
assert!(
sha_256_regex.is_match(&key),
"Output '{}' doesn't match regex '{}'",
key,
sha_256_regex
);
}
}
}
#[tokio::test]
async fn test_signin_bearer_for_record() {
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
CREATE user:test;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to sign in with bearer key: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to sign in with bearer key: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
AUTHENTICATE {{
RETURN NONE
}}
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
assert!(res.is_ok(), "Failed to sign in with bearer key: {:?}", res);
assert_eq!(sess.ns, Some("test".to_string()));
assert_eq!(sess.db, Some("test".to_string()));
assert_eq!(sess.au.id(), "user:test");
assert!(sess.au.is_record());
assert_eq!(sess.au.level().ns(), Some("test"));
assert_eq!(sess.au.level().db(), Some("test"));
assert_eq!(sess.au.level().id(), Some("user:test"));
assert!(!sess.au.has_role(Role::Viewer), "Auth user expected to not have Viewer role");
assert!(!sess.au.has_role(Role::Editor), "Auth user expected to not have Editor role");
assert!(!sess.au.has_role(Role::Owner), "Auth user expected to not have Owner role");
let exp = sess.exp.unwrap();
let min_exp = (Utc::now() + Duration::hours(2) - Duration::seconds(10)).timestamp();
let max_exp = (Utc::now() + Duration::hours(2) + Duration::seconds(10)).timestamp();
assert!(
exp > min_exp && exp < max_exp,
"Session expiration is expected to follow the defined duration"
);
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
AUTHENTICATE {{
THROW "Test authentication error";
}}
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::Thrown(e)) => {
assert_eq!(e, "Test authentication error")
}
res => panic!(
"Expected a thrown authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR GRANT 1s FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
std::thread::sleep(Duration::seconds(2).to_std().unwrap());
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR GRANT 1s FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
let kid = key.split("-").collect::<Vec<&str>>()[2];
ds.execute(&format!("ACCESS api ON DATABASE REVOKE GRANT {kid}"), &sess, None)
.await
.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR GRANT 1s FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let key = grant.get("key").unwrap().clone().as_string();
ds.execute("REMOVE ACCESS api ON DATABASE", &sess, None).await.unwrap();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::AccessNotFound) => {} res => panic!(
"Expected an access method not found error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let _key = grant.get("key").unwrap().clone().as_string();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let vars: HashMap<&str, Value> = HashMap::new();
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::AccessBearerMissingKey) => {} res => panic!(
"Expected a missing key authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key["surreal-".len() + 2] = '_';
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::AccessGrantBearerInvalid) => {} res => panic!(
"Expected an invalid key authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key.truncate(invalid_key.len() - 1);
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::AccessGrantBearerInvalid) => {} res => panic!(
"Expected an invalid key authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key[access_type::BearerAccessType::Bearer.prefix().len() + 2] = '_';
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let valid_key = grant.get("key").unwrap().clone().as_string();
let mut invalid_key: Vec<char> = valid_key.chars().collect();
invalid_key[valid_key.len() - 2] = '_';
let key: String = invalid_key.into_iter().collect();
let mut sess = Session {
ns: Some("test".to_string()),
db: Some("test".to_string()),
..Default::default()
};
let mut vars: HashMap<&str, Value> = HashMap::new();
vars.insert("key", key.into());
let res = db_access(
&ds,
&mut sess,
"test".to_string(),
"test".to_string(),
"api".to_string(),
vars.into(),
)
.await;
match res {
Err(Error::InvalidAuth) => {} res => panic!(
"Expected a generic authentication error, but instead received: {:?}",
res
),
}
}
{
let ds = Datastore::new("memory").await.unwrap().with_capabilities(
Capabilities::default().with_experimental(ExperimentalTarget::BearerAccess.into()),
);
let sess = Session::owner().with_ns("test").with_db("test");
let res = ds
.execute(
r#"
DEFINE ACCESS api ON DATABASE TYPE BEARER FOR RECORD
DURATION FOR SESSION 2h
;
ACCESS api ON DATABASE GRANT FOR RECORD user:test;
"#,
&sess,
None,
)
.await
.unwrap();
let result = if let Ok(res) = &res.last().unwrap().result {
res.clone()
} else {
panic!("Unable to retrieve bearer key grant");
};
let grant = result
.coerce_to_object()
.unwrap()
.get("grant")
.unwrap()
.clone()
.coerce_to_object()
.unwrap();
let id = grant.get("id").unwrap().clone().as_string();
let key = grant.get("key").unwrap().clone().as_string();
let ok = Regex::new(r"surreal-bearer-[a-zA-Z0-9]{12}-[a-zA-Z0-9]{24}").unwrap();
assert!(ok.is_match(&key), "Output '{}' doesn't match regex '{}'", key, ok);
let tx = ds.transaction(Read, Optimistic).await.unwrap().enclose();
let grant = tx.get_db_access_grant("test", "test", "api", &id).await.unwrap();
let key = match &grant.grant {
access::Grant::Bearer(grant) => grant.key.clone(),
_ => panic!("Incorrect grant type returned, expected a bearer grant"),
};
tx.cancel().await.unwrap();
let ok = Regex::new(r"[0-9a-f]{64}").unwrap();
assert!(ok.is_match(&key), "Output '{}' doesn't match regex '{}'", key, ok);
}
}
#[tokio::test]
async fn test_signin_nonexistent_role() {
use crate::iam::Error as IamError;
use crate::sql::{
statements::{define::DefineStatement, DefineUserStatement},
user::UserDuration,
Base, Statement,
};
let test_levels = vec![
TestLevel {
level: "ROOT",
ns: None,
db: None,
},
TestLevel {
level: "NS",
ns: Some("test"),
db: None,
},
TestLevel {
level: "DB",
ns: Some("test"),
db: Some("test"),
},
];
for level in &test_levels {
let ds = Datastore::new("memory").await.unwrap();
let sess = Session::owner().with_ns("test").with_db("test");
let base = match level.level {
"ROOT" => Base::Root,
"NS" => Base::Ns,
"DB" => Base::Db,
_ => panic!("Unsupported level"),
};
let user = DefineUserStatement {
base,
name: "user".into(),
hash: "$argon2id$v=19$m=16,t=2,p=1$VUlHTHVOYjc5d0I1dGE3OQ$sVtmRNH+Xtiijk0uXL2+4w"
.to_string(),
code: "dummy".to_string(),
roles: vec!["nonexistent".into()],
duration: UserDuration::default(),
comment: None,
if_not_exists: false,
overwrite: false,
};
ds.process(Statement::Define(DefineStatement::User(user)).into(), &sess, None)
.await
.unwrap();
let mut sess = Session {
ns: level.ns.map(String::from),
db: level.db.map(String::from),
..Default::default()
};
let res = match level.level {
"ROOT" => root_user(&ds, &mut sess, "user".to_string(), "pass".to_string()).await,
"NS" => {
ns_user(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
"user".to_string(),
"pass".to_string(),
)
.await
}
"DB" => {
db_user(
&ds,
&mut sess,
level.ns.unwrap().to_string(),
level.db.unwrap().to_string(),
"user".to_string(),
"pass".to_string(),
)
.await
}
_ => panic!("Unsupported level"),
};
match res {
Err(Error::IamError(IamError::InvalidRole(_))) => {} res => {
panic!("Expected an invalid role IAM error, but instead received: {:?}", res)
}
}
}
}
}