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-06-20 11:29:06 +00:00
|
|
|
use surrealdb::sql::Value;
|
2022-08-23 22:44:13 +00:00
|
|
|
use surrealdb::Session;
|
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,
|
|
|
|
token: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Success {
|
|
|
|
fn new(token: String) -> Success {
|
|
|
|
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> {
|
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
|
2022-08-23 22:44:13 +00:00
|
|
|
Ok(Value::Object(vars)) => match crate::iam::signup::signup(&mut session, vars).await {
|
2022-07-04 00:01:24 +00:00
|
|
|
// Authentication was successful
|
2022-09-25 12:05:56 +00:00
|
|
|
Ok(v) => match output.as_deref() {
|
2022-10-19 22:54:41 +00:00
|
|
|
Some("application/json") => Ok(output::json(&Success::new(v.as_string()))),
|
|
|
|
Some("application/cbor") => Ok(output::cbor(&Success::new(v.as_string()))),
|
|
|
|
Some("application/msgpack") => Ok(output::pack(&Success::new(v.as_string()))),
|
|
|
|
Some("text/plain") => Ok(output::text(v.as_string())),
|
|
|
|
None => Ok(output::text(v.as_string())),
|
2022-09-25 12:05:56 +00:00
|
|
|
// An incorrect content-type was requested
|
|
|
|
_ => Err(warp::reject::custom(Error::InvalidType)),
|
|
|
|
},
|
2022-07-04 00:01:24 +00:00
|
|
|
// 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)),
|
|
|
|
}
|
|
|
|
}
|