diff options
author | Galen Guyer <galen@galenguyer.com> | 2022-04-11 20:10:38 -0400 |
---|---|---|
committer | Galen Guyer <galen@galenguyer.com> | 2022-04-11 20:10:38 -0400 |
commit | 177ca712d626599afc2bf45a9fccba173178b58e (patch) | |
tree | c878e11c034704b5a6ef7a148dac34b49ef58790 | |
parent | 6aa79e05bffd133e6d670559acc97310a0079c9d (diff) |
add route to check totp
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/routes/v1/users.rs | 38 |
2 files changed, 32 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs index a797c94..4046d1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,8 @@ async fn main() { "/users", Router::new() .route("/", post(routes::v1::users::create_user)) - .route("/all", get(routes::v1::users::get_all_users)), + .route("/all", get(routes::v1::users::get_all_users)) + .route("/totp", get(routes::v1::users::needs_totp)), ), ), ) diff --git a/src/routes/v1/users.rs b/src/routes/v1/users.rs index 39a6e56..1539285 100644 --- a/src/routes/v1/users.rs +++ b/src/routes/v1/users.rs @@ -1,12 +1,14 @@ use crate::db; use crate::extractors; use crate::routes::v1::requests; +use axum::extract::Query; use axum::http::StatusCode; use axum::response::IntoResponse; use axum::Extension; use extractors::Json; use serde_json::json; use sqlx::{Error, Pool, Postgres}; +use std::collections::HashMap; use std::sync::Arc; pub async fn create_user( @@ -19,7 +21,10 @@ pub async fn create_user( Ok(user) => (StatusCode::OK, Json(json!(user))), Err(err) => match err { Error::Database(e) if e.code().unwrap_or(std::borrow::Cow::Borrowed("")) == "23505" => { - (StatusCode::BAD_REQUEST, Json(json!({"error": "A user with that email already exists"}))) + ( + StatusCode::BAD_REQUEST, + Json(json!({"error": "A user with that email already exists"})), + ) } _ => ( StatusCode::INTERNAL_SERVER_ERROR, @@ -32,12 +37,29 @@ pub async fn create_user( pub async fn get_all_users(Extension(pool): Extension<Arc<Pool<Postgres>>>) -> impl IntoResponse { let users = db::users::get_all_users(&pool).await; match users { - Ok(users) => return (StatusCode::OK, Json(json!(users))), - Err(err) => { - return ( - StatusCode::INTERNAL_SERVER_ERROR, - Json(json!({"error": err.to_string()})), - ) - } + Ok(users) => (StatusCode::OK, Json(json!(users))), + Err(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({"error": err.to_string()})), + ), + } +} + +pub async fn needs_totp( + Query(params): Query<HashMap<String, String>>, + Extension(pool): Extension<Arc<Pool<Postgres>>>, +) -> impl IntoResponse { + match params.get("email") { + Some(email) => match db::users::get_user(&pool, email).await { + Ok(user) => ( + StatusCode::OK, + Json(json!({"totp": user.totp_secret.is_some()})), + ), + Err(_) => (StatusCode::OK, Json(json!({"totp": false}))), + }, + None => ( + StatusCode::BAD_REQUEST, + Json(json!({"error": "Missing query parameter `email`"})), + ), } } |