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;
|
2023-07-19 14:35:56 +00:00
|
|
|
use axum::extract::DefaultBodyLimit;
|
|
|
|
use axum::response::IntoResponse;
|
|
|
|
use axum::routing::options;
|
|
|
|
use axum::{Extension, Router, TypedHeader};
|
2022-06-20 11:29:06 +00:00
|
|
|
use bytes::Bytes;
|
2023-07-19 14:35:56 +00:00
|
|
|
use http_body::Body as HttpBody;
|
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;
|
2023-07-19 14:35:56 +00:00
|
|
|
use tower_http::limit::RequestBodyLimitLayer;
|
2020-06-29 15:36:01 +00:00
|
|
|
|
2023-07-19 14:35:56 +00:00
|
|
|
use super::headers::Accept;
|
|
|
|
|
|
|
|
const MAX: usize = 1024; // 1 KiB
|
2022-05-10 00:07:03 +00:00
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 14:35:56 +00:00
|
|
|
pub(super) fn router<S, B>() -> Router<S, B>
|
|
|
|
where
|
|
|
|
B: HttpBody + Send + 'static,
|
|
|
|
B::Data: Send,
|
|
|
|
B::Error: std::error::Error + Send + Sync + 'static,
|
|
|
|
S: Clone + Send + Sync + 'static,
|
|
|
|
{
|
|
|
|
Router::new()
|
|
|
|
.route("/signup", options(|| async {}).post(handler))
|
|
|
|
.route_layer(DefaultBodyLimit::disable())
|
|
|
|
.layer(RequestBodyLimitLayer::new(MAX))
|
2020-06-29 15:36:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 12:05:56 +00:00
|
|
|
async fn handler(
|
2023-07-19 14:35:56 +00:00
|
|
|
Extension(mut session): Extension<Session>,
|
2023-12-12 13:51:43 +00:00
|
|
|
accept: Option<TypedHeader<Accept>>,
|
2022-09-25 12:05:56 +00:00
|
|
|
body: Bytes,
|
2023-07-19 14:35:56 +00:00
|
|
|
) -> Result<impl IntoResponse, impl IntoResponse> {
|
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
|
2023-12-12 13:51:43 +00:00
|
|
|
Ok(v) => match accept.as_deref() {
|
2023-06-09 13:45:07 +00:00
|
|
|
// Simple serialization
|
2023-07-19 14:35:56 +00:00
|
|
|
Some(Accept::ApplicationJson) => Ok(output::json(&Success::new(v))),
|
|
|
|
Some(Accept::ApplicationCbor) => Ok(output::cbor(&Success::new(v))),
|
|
|
|
Some(Accept::ApplicationPack) => Ok(output::pack(&Success::new(v))),
|
|
|
|
// Text serialization
|
|
|
|
Some(Accept::TextPlain) => Ok(output::text(v.unwrap_or_default())),
|
2023-12-12 13:51:43 +00:00
|
|
|
// Internal serialization
|
|
|
|
Some(Accept::Surrealdb) => Ok(output::full(&Success::new(v))),
|
2023-07-19 14:35:56 +00:00
|
|
|
// Return nothing
|
|
|
|
None => Ok(output::none()),
|
2023-06-09 13:45:07 +00:00
|
|
|
// An incorrect content-type was requested
|
2023-07-19 14:35:56 +00:00
|
|
|
_ => Err(Error::InvalidType),
|
2023-06-09 13:45:07 +00:00
|
|
|
},
|
|
|
|
// There was an error with authentication
|
2023-07-19 14:35:56 +00:00
|
|
|
Err(err) => Err(err),
|
2023-06-09 13:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-20 11:29:06 +00:00
|
|
|
// The provided value was not an object
|
2023-07-19 14:35:56 +00:00
|
|
|
_ => Err(Error::Request),
|
2022-06-20 11:29:06 +00:00
|
|
|
}
|
|
|
|
}
|