initial commit
This commit is contained in:
110
src/api/client_server/auth.rs
Normal file
110
src/api/client_server/auth.rs
Normal file
@ -0,0 +1,110 @@
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
extract::Query,
|
||||
http::StatusCode,
|
||||
routing::{get, post},
|
||||
Extension, Json,
|
||||
};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use crate::responses::registration::RegistrationResponse;
|
||||
use crate::{
|
||||
models::devices::Device,
|
||||
responses::{flow::Flows, registration::RegistrationSuccess},
|
||||
};
|
||||
use crate::{
|
||||
models::users::User,
|
||||
requests::registration::RegistrationRequest,
|
||||
responses::username_available::UsernameAvailable,
|
||||
types::{
|
||||
authentication_data::AuthenticationData, identifier::Identifier, matrix_user_id::UserId,
|
||||
},
|
||||
Config,
|
||||
};
|
||||
|
||||
pub fn routes() -> axum::Router {
|
||||
axum::Router::new()
|
||||
.route("/r0/login", get(get_login).post(post_login))
|
||||
.route("/r0/register", post(post_register))
|
||||
.route("/r0/register/available", get(get_username_available))
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
async fn get_login() -> Json<Flows> {
|
||||
Json(Flows::new())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn post_login(body: String) -> StatusCode {
|
||||
dbg!(body);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn get_username_available(
|
||||
Extension(config): Extension<Arc<Config>>,
|
||||
Extension(db): Extension<SqlitePool>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
) -> Json<UsernameAvailable> {
|
||||
let username = params.get("username").unwrap();
|
||||
let user_id = UserId::new(&username, &config.homeserver_name).unwrap();
|
||||
let exists = User::exists(&db, &user_id).await.unwrap();
|
||||
Json(UsernameAvailable::new(!exists))
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn post_register(
|
||||
Extension(config): Extension<Arc<Config>>,
|
||||
Extension(db): Extension<SqlitePool>,
|
||||
Json(body): Json<RegistrationRequest>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
) -> (StatusCode, Json<RegistrationResponse>) {
|
||||
// Client tries to get available flows
|
||||
if *&body.auth().is_none() {
|
||||
return (
|
||||
StatusCode::UNAUTHORIZED,
|
||||
Json(RegistrationResponse::user_interactive_authorization_info()),
|
||||
);
|
||||
}
|
||||
|
||||
let (user, device) = match &body.auth().unwrap() {
|
||||
AuthenticationData::Password(auth_data) => {
|
||||
// let username = match auth_data.identifier() {
|
||||
// Identifier::User(user_identifier) => user_identifier.user().unwrap(),
|
||||
// };
|
||||
let username = body.username().unwrap();
|
||||
let user_id = UserId::new(&username, &config.homeserver_name).unwrap();
|
||||
if User::exists(&db, &user_id).await.unwrap() {
|
||||
todo!("Error out")
|
||||
}
|
||||
let password = auth_data.password();
|
||||
let display_name = match *&body.initial_device_display_name() {
|
||||
Some(display_name) => display_name.as_ref(),
|
||||
None => "Random displayname",
|
||||
};
|
||||
|
||||
let user = User::create(&db, &user_id, &user_id.to_string(), &password)
|
||||
.await
|
||||
.unwrap();
|
||||
let device = Device::create(&db, &user, "test", display_name)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
(user, device)
|
||||
}
|
||||
};
|
||||
|
||||
// dont log in the user after registration
|
||||
if *&body.inhibit_login().is_some() && *&body.inhibit_login().unwrap() {
|
||||
let resp = RegistrationSuccess::new(None, device.device_id(), user.user_id());
|
||||
|
||||
(StatusCode::OK, Json(RegistrationResponse::Success(resp)))
|
||||
} else {
|
||||
let session = device.create_session(&db).await.unwrap();
|
||||
let resp =
|
||||
RegistrationSuccess::new(Some(session.value()), device.device_id(), user.user_id());
|
||||
|
||||
(StatusCode::OK, Json(RegistrationResponse::Success(resp)))
|
||||
}
|
||||
}
|
2
src/api/client_server/mod.rs
Normal file
2
src/api/client_server/mod.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod auth;
|
||||
pub mod versions;
|
12
src/api/client_server/versions.rs
Normal file
12
src/api/client_server/versions.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use axum::{routing::get, Json};
|
||||
|
||||
use crate::responses::versions::Versions;
|
||||
|
||||
pub fn routes() -> axum::Router {
|
||||
axum::Router::new().route("/versions", get(get_client_versions))
|
||||
}
|
||||
|
||||
#[tracing::instrument]
|
||||
async fn get_client_versions() -> Json<Versions> {
|
||||
Json(Versions::default())
|
||||
}
|
1
src/api/mod.rs
Normal file
1
src/api/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod client_server;
|
Reference in New Issue
Block a user