2023-06-09 13:45:07 +00:00
|
|
|
use crate::dbs::DB;
|
2022-06-20 11:29:06 +00:00
|
|
|
use crate::err::Error;
|
2022-10-30 23:28:57 +00:00
|
|
|
use crate::net::input::bytes_to_utf8;
|
2022-09-25 12:05:56 +00:00
|
|
|
use crate::net::output;
|
2022-08-23 22:44:13 +00:00
|
|
|
use crate::net::session;
|
2022-06-20 11:29:06 +00:00
|
|
|
use bytes::Bytes;
|
2022-09-25 12:05:56 +00:00
|
|
|
use serde::Serialize;
|
2022-12-30 08:23:19 +00:00
|
|
|
use surrealdb::dbs::Session;
|
2022-06-20 11:29:06 +00:00
|
|
|
use surrealdb::sql::Value;
|
2020-06-29 15:36:01 +00:00
|
|
|
use warp::Filter;
|
|
|
|
|
2022-05-10 00:07:03 +00:00
|
|
|
const MAX: u64 = 1024; // 1 KiB
|
|
|
|
|
2022-09-25 12:05:56 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct Success {
|
|
|
|
code: u16,
|
|
|
|
details: String,
|
2023-02-11 15:56:14 +00:00
|
|
|
token: Option<String>,
|
2022-09-25 12:05:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Success {
|
2023-02-11 15:56:14 +00:00
|
|
|
fn new(token: Option<String>) -> Success {
|
2022-09-25 12:05:56 +00:00
|
|
|
Success {
|
|
|
|
token,
|
|
|
|
code: 200,
|
|
|
|
details: String::from("Authentication succeeded"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 10:30:40 +00:00
|
|
|
#[allow(opaque_hidden_inferred_bound)]
|
2020-06-29 15:36:01 +00:00
|
|
|
pub fn config() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
|
|
|
// Set base path
|
|
|
|
let base = warp::path("signup").and(warp::path::end());
|
|
|
|
// Set opts method
|
|
|
|
let opts = base.and(warp::options()).map(warp::reply);
|
|
|
|
// Set post method
|
2022-06-20 11:29:06 +00:00
|
|
|
let post = base
|
|
|
|
.and(warp::post())
|
2022-09-25 12:05:56 +00:00
|
|
|
.and(warp::header::optional::<String>(http::header::ACCEPT.as_str()))
|
2022-06-20 11:29:06 +00:00
|
|
|
.and(warp::body::content_length_limit(MAX))
|
|
|
|
.and(warp::body::bytes())
|
2022-09-25 22:13:29 +00:00
|
|
|
.and(session::build())
|
2022-06-20 11:29:06 +00:00
|
|
|
.and_then(handler);
|
2020-06-29 15:36:01 +00:00
|
|
|
// Specify route
|
2022-07-23 12:49:26 +00:00
|
|
|
opts.or(post)
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 12:05:56 +00:00
|
|
|
async fn handler(
|
|
|
|
output: Option<String>,
|
|
|
|
body: Bytes,
|
2022-09-25 22:13:29 +00:00
|
|
|
mut session: Session,
|
2022-09-25 12:05:56 +00:00
|
|
|
) -> Result<impl warp::Reply, warp::Rejection> {
|
2023-06-09 13:45:07 +00:00
|
|
|
// Get a database reference
|
|
|
|
let kvs = DB.get().unwrap();
|
2022-07-04 00:01:24 +00:00
|
|
|
// Convert the HTTP body into text
|
2022-10-30 23:28:57 +00:00
|
|
|
let data = bytes_to_utf8(&body)?;
|
2022-07-04 00:01:24 +00:00
|
|
|
// Parse the provided data as JSON
|
2022-06-20 11:29:06 +00:00
|
|
|
match surrealdb::sql::json(data) {
|
|
|
|
// The provided value was an object
|
2023-06-09 13:45:07 +00:00
|
|
|
Ok(Value::Object(vars)) => {
|
2023-07-05 21:26:13 +00:00
|
|
|
match surrealdb::iam::signup::signup(kvs, &mut session, vars).await.map_err(Error::from)
|
2023-06-09 13:45:07 +00:00
|
|
|
{
|
|
|
|
// Authentication was successful
|
|
|
|
Ok(v) => match output.as_deref() {
|
2023-07-01 19:09:15 +00:00
|
|
|
// Return nothing
|
|
|
|
None => Ok(output::none()),
|
|
|
|
// Text serialization
|
|
|
|
Some("text/plain") => Ok(output::text(v.unwrap_or_default())),
|
2023-06-09 13:45:07 +00:00
|
|
|
// Simple serialization
|
|
|
|
Some("application/json") => Ok(output::json(&Success::new(v))),
|
|
|
|
Some("application/cbor") => Ok(output::cbor(&Success::new(v))),
|
|
|
|
Some("application/pack") => Ok(output::pack(&Success::new(v))),
|
|
|
|
// Internal serialization
|
2023-07-01 19:09:15 +00:00
|
|
|
Some("application/surrealdb") => Ok(output::full(&Success::new(v))),
|
2023-06-09 13:45:07 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
|
|
|
},
|
|
|
|
// There was an error with authentication
|
|
|
|
Err(e) => Err(warp::reject::custom(e)),
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 11:29:06 +00:00
|
|
|
// The provided value was not an object
|
|
|
|
_ => Err(warp::reject::custom(Error::Request)),
|
|
|
|
}
|
|
|
|
}
|