Add auth
This commit is contained in:
parent
e1ba3d44c7
commit
dda356ea00
8 changed files with 60 additions and 0 deletions
|
|
@ -1,11 +1,19 @@
|
||||||
use rand::distributions::Alphanumeric;
|
use rand::distributions::Alphanumeric;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
pub struct UserConfig {
|
pub struct UserConfig {
|
||||||
#[serde(default = "default_users")]
|
#[serde(default = "default_users")]
|
||||||
pub user_tokens: Vec<User>,
|
pub user_tokens: Vec<User>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl UserConfig {
|
||||||
|
pub fn get_user(&self, token: &str) -> Option<&User> {
|
||||||
|
self.user_tokens.iter().find(|u| u.token == token)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ use axum::response::{IntoResponse, Response};
|
||||||
use axum::{extract::DefaultBodyLimit, Extension};
|
use axum::{extract::DefaultBodyLimit, Extension};
|
||||||
use axum::{extract::WebSocketUpgrade, Json};
|
use axum::{extract::WebSocketUpgrade, Json};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
mod auth;
|
||||||
mod create_document;
|
mod create_document;
|
||||||
mod delete_document;
|
mod delete_document;
|
||||||
mod fetch_latest_document_version;
|
mod fetch_latest_document_version;
|
||||||
|
|
|
||||||
14
backend/sync_server/src/server/auth.rs
Normal file
14
backend/sync_server/src/server/auth.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
use crate::{
|
||||||
|
app_state::AppState,
|
||||||
|
config::user_config::User,
|
||||||
|
errors::{unauthorized_error, SyncServerError},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn auth(app_state: &AppState, token: &str) -> Result<User, SyncServerError> {
|
||||||
|
app_state
|
||||||
|
.config
|
||||||
|
.users
|
||||||
|
.get_user(token)
|
||||||
|
.cloned()
|
||||||
|
.ok_or_else(|| unauthorized_error(anyhow::anyhow!("Invalid token")))
|
||||||
|
}
|
||||||
|
|
@ -9,16 +9,23 @@ use anyhow::Context;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
use axum_extra::headers::authorization::Bearer;
|
||||||
|
use axum_extra::headers::Authorization;
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
use sync_lib::base64_to_bytes;
|
use sync_lib::base64_to_bytes;
|
||||||
|
|
||||||
|
use super::auth::auth;
|
||||||
use super::requests::CreateDocumentVersion;
|
use super::requests::CreateDocumentVersion;
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
pub async fn create_document(
|
pub async fn create_document(
|
||||||
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||||
Path(vault_id): Path<VaultId>,
|
Path(vault_id): Path<VaultId>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(request): Json<CreateDocumentVersion>,
|
Json(request): Json<CreateDocumentVersion>,
|
||||||
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
||||||
|
auth(&state, auth_header.token())?;
|
||||||
|
|
||||||
let new_version = StoredDocumentVersion {
|
let new_version = StoredDocumentVersion {
|
||||||
vault_id,
|
vault_id,
|
||||||
document_id: uuid::Uuid::new_v4(),
|
document_id: uuid::Uuid::new_v4(),
|
||||||
|
|
|
||||||
|
|
@ -10,15 +10,22 @@ use anyhow::Context;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
use axum_extra::headers::authorization::Bearer;
|
||||||
|
use axum_extra::headers::Authorization;
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
|
|
||||||
|
use super::auth::auth;
|
||||||
use super::requests::DeleteDocumentVersion;
|
use super::requests::DeleteDocumentVersion;
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
pub async fn delete_document(
|
pub async fn delete_document(
|
||||||
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||||
Path((vault_id, document_id)): Path<(VaultId, DocumentId)>,
|
Path((vault_id, document_id)): Path<(VaultId, DocumentId)>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(request): Json<DeleteDocumentVersion>,
|
Json(request): Json<DeleteDocumentVersion>,
|
||||||
) -> Result<(), SyncServerError> {
|
) -> Result<(), SyncServerError> {
|
||||||
|
auth(&state, auth_header.token())?;
|
||||||
|
|
||||||
let mut transaction = state
|
let mut transaction = state
|
||||||
.database
|
.database
|
||||||
.create_transaction()
|
.create_transaction()
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,20 @@ use anyhow::anyhow;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
use axum_extra::headers::authorization::Bearer;
|
||||||
|
use axum_extra::headers::Authorization;
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
|
|
||||||
|
use super::auth::auth;
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
pub async fn fetch_latest_document_version(
|
pub async fn fetch_latest_document_version(
|
||||||
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||||
Path((vault_id, document_id)): Path<(VaultId, DocumentId)>,
|
Path((vault_id, document_id)): Path<(VaultId, DocumentId)>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
) -> Result<Json<DocumentVersion>, SyncServerError> {
|
) -> Result<Json<DocumentVersion>, SyncServerError> {
|
||||||
|
auth(&state, auth_header.token())?;
|
||||||
|
|
||||||
let latest_version = state
|
let latest_version = state
|
||||||
.database
|
.database
|
||||||
.get_latest_document_version(&vault_id, &document_id, None)
|
.get_latest_document_version(&vault_id, &document_id, None)
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,20 @@ use crate::errors::SyncServerError;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
use axum_extra::headers::authorization::Bearer;
|
||||||
|
use axum_extra::headers::Authorization;
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
|
|
||||||
|
use super::auth::auth;
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
pub async fn fetch_latest_documents(
|
pub async fn fetch_latest_documents(
|
||||||
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||||
Path(vault_id): Path<VaultId>,
|
Path(vault_id): Path<VaultId>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
) -> Result<Json<Vec<DocumentVersionWithoutContent>>, SyncServerError> {
|
) -> Result<Json<Vec<DocumentVersionWithoutContent>>, SyncServerError> {
|
||||||
|
auth(&state, auth_header.token())?;
|
||||||
|
|
||||||
let latest_version = state
|
let latest_version = state
|
||||||
.database
|
.database
|
||||||
.get_latest_documents(&vault_id, None)
|
.get_latest_documents(&vault_id, None)
|
||||||
|
|
|
||||||
|
|
@ -12,17 +12,24 @@ use anyhow::Context;
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
use axum::extract::State;
|
use axum::extract::State;
|
||||||
use axum::Json;
|
use axum::Json;
|
||||||
|
use axum_extra::headers::authorization::Bearer;
|
||||||
|
use axum_extra::headers::Authorization;
|
||||||
|
use axum_extra::TypedHeader;
|
||||||
use sync_lib::base64_to_bytes;
|
use sync_lib::base64_to_bytes;
|
||||||
use sync_lib::base64_to_string;
|
use sync_lib::base64_to_string;
|
||||||
|
|
||||||
|
use super::auth::auth;
|
||||||
use super::requests::UpdateDocumentVersion;
|
use super::requests::UpdateDocumentVersion;
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
pub async fn update_document(
|
pub async fn update_document(
|
||||||
|
TypedHeader(auth_header): TypedHeader<Authorization<Bearer>>,
|
||||||
Path((vault_id, document_id)): Path<(VaultId, DocumentId)>,
|
Path((vault_id, document_id)): Path<(VaultId, DocumentId)>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(request): Json<UpdateDocumentVersion>,
|
Json(request): Json<UpdateDocumentVersion>,
|
||||||
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
) -> Result<Json<DocumentVersionWithoutContent>, SyncServerError> {
|
||||||
|
auth(&state, auth_header.token())?;
|
||||||
|
|
||||||
let parent = state
|
let parent = state
|
||||||
.database
|
.database
|
||||||
.get_document_version(&vault_id, &document_id, &request.parent_version_id, None)
|
.get_document_version(&vault_id, &document_id, &request.parent_version_id, None)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue