Added initial authorization layer.
This commit is contained in:
+32
-8
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n SELECT *\n FROM tokens\n WHERE access_token_hash = $1\n AND access_expires_at > now()\n AND revoked_at IS NULL\n ",
|
"query": "\n SELECT\n id,\n user_id,\n user_app_id,\n access_token_hash,\n refresh_token_hash,\n access_expires_at,\n refresh_expires_at,\n refreshed_at,\n revoked_at,\n created_at\n FROM tokens\n WHERE access_token_hash = $1\n AND access_expires_at > now()\n AND revoked_at IS NULL\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
@@ -16,6 +16,28 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 1,
|
"ordinal": 1,
|
||||||
|
"name": "user_id",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "tokens",
|
||||||
|
"name": "user_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "user_app_id",
|
||||||
|
"type_info": "Uuid",
|
||||||
|
"origin": {
|
||||||
|
"Table": {
|
||||||
|
"table": "tokens",
|
||||||
|
"name": "user_app_id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
"name": "access_token_hash",
|
"name": "access_token_hash",
|
||||||
"type_info": "Bytea",
|
"type_info": "Bytea",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -26,7 +48,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 2,
|
"ordinal": 4,
|
||||||
"name": "refresh_token_hash",
|
"name": "refresh_token_hash",
|
||||||
"type_info": "Bytea",
|
"type_info": "Bytea",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -37,7 +59,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 3,
|
"ordinal": 5,
|
||||||
"name": "access_expires_at",
|
"name": "access_expires_at",
|
||||||
"type_info": "Timestamptz",
|
"type_info": "Timestamptz",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -48,7 +70,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 4,
|
"ordinal": 6,
|
||||||
"name": "refresh_expires_at",
|
"name": "refresh_expires_at",
|
||||||
"type_info": "Timestamptz",
|
"type_info": "Timestamptz",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -59,7 +81,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 5,
|
"ordinal": 7,
|
||||||
"name": "refreshed_at",
|
"name": "refreshed_at",
|
||||||
"type_info": "Timestamptz",
|
"type_info": "Timestamptz",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -70,7 +92,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 6,
|
"ordinal": 8,
|
||||||
"name": "revoked_at",
|
"name": "revoked_at",
|
||||||
"type_info": "Timestamptz",
|
"type_info": "Timestamptz",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -81,7 +103,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ordinal": 7,
|
"ordinal": 9,
|
||||||
"name": "created_at",
|
"name": "created_at",
|
||||||
"type_info": "Timestamptz",
|
"type_info": "Timestamptz",
|
||||||
"origin": {
|
"origin": {
|
||||||
@@ -100,6 +122,8 @@
|
|||||||
"nullable": [
|
"nullable": [
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
false,
|
false,
|
||||||
@@ -108,5 +132,5 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "458c87cbcf275d5b6f05abb9e5bec0633eff66b73beed646069ad4db7450fd33"
|
"hash": "755b789df9d3d7f92243481e8fc8b724cae757c55579da8f86adb778eefdbc48"
|
||||||
}
|
}
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
{
|
|
||||||
"db_name": "PostgreSQL",
|
|
||||||
"query": "\n INSERT INTO tokens (\n id, access_token_hash, refresh_token_hash,\n access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8)\n ",
|
|
||||||
"describe": {
|
|
||||||
"columns": [],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Uuid",
|
|
||||||
"Bytea",
|
|
||||||
"Bytea",
|
|
||||||
"Timestamptz",
|
|
||||||
"Timestamptz",
|
|
||||||
"Timestamptz",
|
|
||||||
"Timestamptz",
|
|
||||||
"Timestamptz"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"nullable": []
|
|
||||||
},
|
|
||||||
"hash": "8f11aa4a2d8b3208181f12fc5b6f87f80fbc7eade88f4ceaf17f9716622ad12a"
|
|
||||||
}
|
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "\n INSERT INTO tokens (\n id, user_id, user_app_id, access_token_hash, refresh_token_hash,\n access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at\n )\n VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)\n ",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Uuid",
|
||||||
|
"Bytea",
|
||||||
|
"Bytea",
|
||||||
|
"Timestamptz",
|
||||||
|
"Timestamptz",
|
||||||
|
"Timestamptz",
|
||||||
|
"Timestamptz",
|
||||||
|
"Timestamptz"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "f448274b39bb13ec500a7634fab177916a194192f3ea14f8439bd42c467844a0"
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
CREATE TABLE tokens (
|
CREATE TABLE tokens (
|
||||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid (),
|
||||||
|
user_id UUID NOT NULL CONSTRAINT fk_tokens_user REFERENCES users (id) ON DELETE CASCADE,
|
||||||
|
user_app_id UUID CONSTRAINT fk_tokens_app REFERENCES user_apps (id) ON DELETE CASCADE,
|
||||||
access_token_hash BYTEA NOT NULL UNIQUE CHECK (
|
access_token_hash BYTEA NOT NULL UNIQUE CHECK (
|
||||||
octet_length(access_token_hash) = 32
|
octet_length(access_token_hash) = 32
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
pub struct TokenRow {
|
pub struct TokenRow {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub user_app_id: Option<Uuid>,
|
||||||
pub access_token_hash: Vec<u8>,
|
pub access_token_hash: Vec<u8>,
|
||||||
pub refresh_token_hash: Vec<u8>,
|
pub refresh_token_hash: Vec<u8>,
|
||||||
pub access_expires_at: DateTime<Utc>,
|
pub access_expires_at: DateTime<Utc>,
|
||||||
|
|||||||
@@ -12,7 +12,17 @@ where
|
|||||||
let row = sqlx::query_as!(
|
let row = sqlx::query_as!(
|
||||||
TokenRow,
|
TokenRow,
|
||||||
r#"
|
r#"
|
||||||
SELECT *
|
SELECT
|
||||||
|
id,
|
||||||
|
user_id,
|
||||||
|
user_app_id,
|
||||||
|
access_token_hash,
|
||||||
|
refresh_token_hash,
|
||||||
|
access_expires_at,
|
||||||
|
refresh_expires_at,
|
||||||
|
refreshed_at,
|
||||||
|
revoked_at,
|
||||||
|
created_at
|
||||||
FROM tokens
|
FROM tokens
|
||||||
WHERE access_token_hash = $1
|
WHERE access_token_hash = $1
|
||||||
AND access_expires_at > now()
|
AND access_expires_at > now()
|
||||||
@@ -34,12 +44,14 @@ where
|
|||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO tokens (
|
INSERT INTO tokens (
|
||||||
id, access_token_hash, refresh_token_hash,
|
id, user_id, user_app_id, access_token_hash, refresh_token_hash,
|
||||||
access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at
|
access_expires_at, refresh_expires_at, refreshed_at, revoked_at, created_at
|
||||||
)
|
)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||||
"#,
|
"#,
|
||||||
token.id,
|
token.id,
|
||||||
|
token.user_id,
|
||||||
|
token.user_app_id,
|
||||||
token.access_token_hash,
|
token.access_token_hash,
|
||||||
token.refresh_token_hash,
|
token.refresh_token_hash,
|
||||||
token.access_expires_at,
|
token.access_expires_at,
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ use uuid::Uuid;
|
|||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub struct Token {
|
pub struct Token {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub user_app_id: Option<Uuid>,
|
||||||
pub access_token_hash: Vec<u8>,
|
pub access_token_hash: Vec<u8>,
|
||||||
pub refresh_token_hash: Vec<u8>,
|
pub refresh_token_hash: Vec<u8>,
|
||||||
pub access_expires_at: DateTime<Utc>,
|
pub access_expires_at: DateTime<Utc>,
|
||||||
|
|||||||
@@ -60,6 +60,8 @@ where
|
|||||||
fn decode(token: TokenRow) -> Result<Token, RepositoryError> {
|
fn decode(token: TokenRow) -> Result<Token, RepositoryError> {
|
||||||
Ok(Token {
|
Ok(Token {
|
||||||
id: token.id,
|
id: token.id,
|
||||||
|
user_id: token.user_id,
|
||||||
|
user_app_id: token.user_app_id,
|
||||||
access_token_hash: token.access_token_hash,
|
access_token_hash: token.access_token_hash,
|
||||||
refresh_token_hash: token.refresh_token_hash,
|
refresh_token_hash: token.refresh_token_hash,
|
||||||
access_expires_at: token.access_expires_at,
|
access_expires_at: token.access_expires_at,
|
||||||
@@ -73,6 +75,8 @@ fn decode(token: TokenRow) -> Result<Token, RepositoryError> {
|
|||||||
fn encode(token: Token) -> TokenRow {
|
fn encode(token: Token) -> TokenRow {
|
||||||
TokenRow {
|
TokenRow {
|
||||||
id: token.id,
|
id: token.id,
|
||||||
|
user_id: token.user_id,
|
||||||
|
user_app_id: token.user_app_id,
|
||||||
access_token_hash: token.access_token_hash,
|
access_token_hash: token.access_token_hash,
|
||||||
refresh_token_hash: token.refresh_token_hash,
|
refresh_token_hash: token.refresh_token_hash,
|
||||||
access_expires_at: token.access_expires_at,
|
access_expires_at: token.access_expires_at,
|
||||||
|
|||||||
@@ -7,10 +7,7 @@ use super::{
|
|||||||
dtos::account::{AccountRequest, AccountResponse},
|
dtos::account::{AccountRequest, AccountResponse},
|
||||||
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{error::AppError, repository};
|
||||||
error::AppError,
|
|
||||||
repository::{self},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn create_router() -> axum::Router<PgPool> {
|
pub fn create_router() -> axum::Router<PgPool> {
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
@@ -37,24 +34,34 @@ async fn list_accounts(
|
|||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<AccountResponse>>, AppError> {
|
) -> Result<Json<Vec<AccountResponse>>, AppError> {
|
||||||
|
let mut tx = pool
|
||||||
|
.begin()
|
||||||
|
.await
|
||||||
|
.map_err(repository::RepositoryError::from)?;
|
||||||
|
|
||||||
let query = repository::accounts::Query {
|
let query = repository::accounts::Query {
|
||||||
name: query.name,
|
name: query.name,
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
offset: query.offset,
|
offset: query.offset,
|
||||||
};
|
};
|
||||||
|
|
||||||
let accounts = repository::accounts::list(&pool, query)
|
let accounts = repository::accounts::list(&mut *tx, query)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(AccountResponse::from)
|
.map(AccountResponse::from)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
tx.commit()
|
||||||
|
.await
|
||||||
|
.map_err(repository::RepositoryError::from)?;
|
||||||
|
|
||||||
Ok(Json(accounts))
|
Ok(Json(accounts))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_account(
|
async fn get_account(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<Json<AccountResponse>, AppError> {
|
) -> Result<Json<AccountResponse>, AppError> {
|
||||||
@@ -68,6 +75,7 @@ async fn get_account(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool, account))]
|
#[tracing::instrument(skip(pool, account))]
|
||||||
async fn upsert_account(
|
async fn upsert_account(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(account): Json<AccountRequest>,
|
Json(account): Json<AccountRequest>,
|
||||||
@@ -87,6 +95,7 @@ async fn upsert_account(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_account(
|
async fn delete_account(
|
||||||
|
Auth(auth): Auth,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
|||||||
@@ -35,12 +35,30 @@ async fn issue_token(
|
|||||||
client_secret,
|
client_secret,
|
||||||
} => {
|
} => {
|
||||||
let secret_hash = hash_secret(&client_secret);
|
let secret_hash = hash_secret(&client_secret);
|
||||||
if !repository::user_app_secrets::verify(&pool, client_id, &secret_hash).await? {
|
let generated = generate_token_pair()?;
|
||||||
|
|
||||||
|
let mut tx = pool
|
||||||
|
.begin()
|
||||||
|
.await
|
||||||
|
.map_err(repository::RepositoryError::from)?;
|
||||||
|
|
||||||
|
if !repository::user_app_secrets::verify(&mut *tx, client_id, &secret_hash).await? {
|
||||||
return Err(AppError::Unauthorized);
|
return Err(AppError::Unauthorized);
|
||||||
}
|
}
|
||||||
|
|
||||||
let generated = generate_token_pair()?;
|
let user_app = repository::user_apps::get_by_id(&mut *tx, client_id)
|
||||||
repository::tokens::issue(&pool, token(&generated)).await?;
|
.await?
|
||||||
|
.ok_or(AppError::Unauthorized)?;
|
||||||
|
|
||||||
|
repository::tokens::issue(
|
||||||
|
&mut *tx,
|
||||||
|
token(user_app.user_id, Some(user_app.id), &generated),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
tx.commit()
|
||||||
|
.await
|
||||||
|
.map_err(repository::RepositoryError::from)?;
|
||||||
|
|
||||||
Ok(Json(token_response(
|
Ok(Json(token_response(
|
||||||
generated.access_token,
|
generated.access_token,
|
||||||
@@ -87,7 +105,7 @@ async fn revoke_token(
|
|||||||
Ok(repository::tokens::revoke(&pool, &hash_secret(&request.token)).await?)
|
Ok(repository::tokens::revoke(&pool, &hash_secret(&request.token)).await?)
|
||||||
}
|
}
|
||||||
|
|
||||||
struct GeneratedTokenPair {
|
struct TokenPair {
|
||||||
access_token: String,
|
access_token: String,
|
||||||
refresh_token: String,
|
refresh_token: String,
|
||||||
access_token_hash: Vec<u8>,
|
access_token_hash: Vec<u8>,
|
||||||
@@ -96,11 +114,11 @@ struct GeneratedTokenPair {
|
|||||||
refresh_expires_at: chrono::DateTime<Utc>,
|
refresh_expires_at: chrono::DateTime<Utc>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_token_pair() -> Result<GeneratedTokenPair, AppError> {
|
fn generate_token_pair() -> Result<TokenPair, AppError> {
|
||||||
let access_token = generate_secret().map_err(|_| AppError::Internal)?;
|
let access_token = generate_secret().map_err(|_| AppError::Internal)?;
|
||||||
let refresh_token = generate_secret().map_err(|_| AppError::Internal)?;
|
let refresh_token = generate_secret().map_err(|_| AppError::Internal)?;
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
Ok(GeneratedTokenPair {
|
Ok(TokenPair {
|
||||||
access_token_hash: hash_secret(&access_token),
|
access_token_hash: hash_secret(&access_token),
|
||||||
refresh_token_hash: hash_secret(&refresh_token),
|
refresh_token_hash: hash_secret(&refresh_token),
|
||||||
access_expires_at: now + Duration::seconds(ACCESS_TOKEN_LIFETIME_SECONDS),
|
access_expires_at: now + Duration::seconds(ACCESS_TOKEN_LIFETIME_SECONDS),
|
||||||
@@ -110,13 +128,15 @@ fn generate_token_pair() -> Result<GeneratedTokenPair, AppError> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn token(generated: &GeneratedTokenPair) -> Token {
|
fn token(user_id: Uuid, app_id: Option<Uuid>, token_pair: &TokenPair) -> Token {
|
||||||
Token {
|
Token {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
access_token_hash: generated.access_token_hash.clone(),
|
user_id,
|
||||||
refresh_token_hash: generated.refresh_token_hash.clone(),
|
user_app_id: app_id,
|
||||||
access_expires_at: generated.access_expires_at,
|
access_token_hash: token_pair.access_token_hash.clone(),
|
||||||
refresh_expires_at: generated.refresh_expires_at,
|
refresh_token_hash: token_pair.refresh_token_hash.clone(),
|
||||||
|
access_expires_at: token_pair.access_expires_at,
|
||||||
|
refresh_expires_at: token_pair.refresh_expires_at,
|
||||||
refreshed_at: None,
|
refreshed_at: None,
|
||||||
revoked_at: None,
|
revoked_at: None,
|
||||||
created_at: Utc::now(),
|
created_at: Utc::now(),
|
||||||
|
|||||||
@@ -45,8 +45,7 @@ where
|
|||||||
.ok_or(AppError::Unauthorized)?;
|
.ok_or(AppError::Unauthorized)?;
|
||||||
|
|
||||||
Ok(Auth(Authorization {
|
Ok(Auth(Authorization {
|
||||||
user_id: token.id,
|
user_id: token.user_id,
|
||||||
app_id: None,
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -54,15 +53,10 @@ where
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Authorization {
|
pub struct Authorization {
|
||||||
user_id: Uuid,
|
user_id: Uuid,
|
||||||
app_id: Option<Uuid>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Authorization {
|
impl Authorization {
|
||||||
pub fn user_id(&self) -> Uuid {
|
pub fn user_id(&self) -> Uuid {
|
||||||
self.user_id
|
self.user_id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn app_id(&self) -> Option<Uuid> {
|
|
||||||
self.app_id
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use super::{
|
|||||||
dtos::scheduled_transaction::{
|
dtos::scheduled_transaction::{
|
||||||
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
RecurringTransactionResponse, ScheduledTransactionRequest, ScheduledTransactionResponse,
|
||||||
},
|
},
|
||||||
extractors::{json::Json, path::Path, query::Query},
|
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
domain,
|
domain,
|
||||||
@@ -38,6 +38,7 @@ pub struct QueryParams {
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn list_scheduled_transactions(
|
async fn list_scheduled_transactions(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
) -> Result<Json<Vec<ScheduledTransactionResponse>>, AppError> {
|
||||||
@@ -63,6 +64,7 @@ async fn list_scheduled_transactions(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_scheduled_transaction(
|
async fn get_scheduled_transaction(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<Json<ScheduledTransactionResponse>, AppError> {
|
) -> Result<Json<ScheduledTransactionResponse>, AppError> {
|
||||||
@@ -80,6 +82,7 @@ async fn get_scheduled_transaction(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool, transaction))]
|
#[tracing::instrument(skip(pool, transaction))]
|
||||||
async fn upsert_scheduled_transaction(
|
async fn upsert_scheduled_transaction(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(transaction): Json<ScheduledTransactionRequest>,
|
Json(transaction): Json<ScheduledTransactionRequest>,
|
||||||
@@ -108,6 +111,7 @@ async fn upsert_scheduled_transaction(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_scheduled_transaction(
|
async fn delete_scheduled_transaction(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
dtos::transaction::{TransactionRequest, TransactionResponse},
|
dtos::transaction::{TransactionRequest, TransactionResponse},
|
||||||
extractors::{json::Json, path::Path, query::Query},
|
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
domain,
|
domain,
|
||||||
@@ -35,6 +35,7 @@ pub struct QueryParams {
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn list_transactions(
|
async fn list_transactions(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
|
) -> Result<Json<Vec<TransactionResponse>>, AppError> {
|
||||||
@@ -59,6 +60,7 @@ async fn list_transactions(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_transaction(
|
async fn get_transaction(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<Json<TransactionResponse>, AppError> {
|
) -> Result<Json<TransactionResponse>, AppError> {
|
||||||
@@ -76,6 +78,7 @@ async fn get_transaction(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool, transaction))]
|
#[tracing::instrument(skip(pool, transaction))]
|
||||||
async fn upsert_transaction(
|
async fn upsert_transaction(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(transaction): Json<TransactionRequest>,
|
Json(transaction): Json<TransactionRequest>,
|
||||||
@@ -100,6 +103,7 @@ async fn upsert_transaction(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_transaction(
|
async fn delete_transaction(
|
||||||
|
Auth(auth): Auth,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use super::{
|
|||||||
dtos::user_app_secret::{
|
dtos::user_app_secret::{
|
||||||
CreatedUserAppSecretResponse, UserAppSecretRequest, UserAppSecretResponse,
|
CreatedUserAppSecretResponse, UserAppSecretRequest, UserAppSecretResponse,
|
||||||
},
|
},
|
||||||
extractors::{json::Json, path::Path, query::Query},
|
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
crypto::{generate_secret, hash_secret},
|
crypto::{generate_secret, hash_secret},
|
||||||
@@ -36,10 +36,15 @@ pub struct QueryParams {
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn list_user_app_secrets(
|
async fn list_user_app_secrets(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<UserAppSecretResponse>>, AppError> {
|
) -> Result<Json<Vec<UserAppSecretResponse>>, AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let query = repository::user_app_secrets::Query {
|
let query = repository::user_app_secrets::Query {
|
||||||
app_id,
|
app_id,
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
@@ -61,9 +66,14 @@ async fn list_user_app_secrets(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_user_app_secret(
|
async fn get_user_app_secret(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
||||||
) -> Result<Json<UserAppSecretResponse>, AppError> {
|
) -> Result<Json<UserAppSecretResponse>, AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||||
let secret = repository::user_app_secrets::get_by_id(&mut *tx, app_id, secret_id)
|
let secret = repository::user_app_secrets::get_by_id(&mut *tx, app_id, secret_id)
|
||||||
@@ -77,10 +87,15 @@ async fn get_user_app_secret(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool, request))]
|
#[tracing::instrument(skip(pool, request))]
|
||||||
async fn upsert_user_app_secret(
|
async fn upsert_user_app_secret(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
||||||
Json(request): Json<UserAppSecretRequest>,
|
Json(request): Json<UserAppSecretRequest>,
|
||||||
) -> Result<(StatusCode, Json<CreatedUserAppSecretResponse>), AppError> {
|
) -> Result<(StatusCode, Json<CreatedUserAppSecretResponse>), AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let now = chrono::Utc::now();
|
let now = chrono::Utc::now();
|
||||||
if request.expires_at <= now {
|
if request.expires_at <= now {
|
||||||
return Err(AppError::BadRequest(
|
return Err(AppError::BadRequest(
|
||||||
@@ -121,9 +136,14 @@ async fn upsert_user_app_secret(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_user_app_secret(
|
async fn delete_user_app_secret(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
Path((user_id, app_id, secret_id)): Path<(Uuid, Uuid, Uuid)>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||||
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
ensure_app_ownership(&mut tx, user_id, app_id).await?;
|
||||||
let deleted = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
let deleted = repository::user_app_secrets::delete_by_id(&mut *tx, app_id, secret_id).await?;
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
dtos::user_app::{UserAppRequest, UserAppResponse},
|
dtos::user_app::{UserAppRequest, UserAppResponse},
|
||||||
extractors::{json::Json, path::Path, query::Query},
|
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
error::AppError,
|
error::AppError,
|
||||||
@@ -32,10 +32,15 @@ pub struct QueryParams {
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn list_user_apps(
|
async fn list_user_apps(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(user_id): Path<Uuid>,
|
Path(user_id): Path<Uuid>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<UserAppResponse>>, AppError> {
|
) -> Result<Json<Vec<UserAppResponse>>, AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let query = repository::user_apps::Query {
|
let query = repository::user_apps::Query {
|
||||||
user_id,
|
user_id,
|
||||||
limit: Some(query.limit.unwrap_or(20)),
|
limit: Some(query.limit.unwrap_or(20)),
|
||||||
@@ -54,9 +59,14 @@ async fn list_user_apps(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_user_app(
|
async fn get_user_app(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
||||||
) -> Result<Json<UserAppResponse>, AppError> {
|
) -> Result<Json<UserAppResponse>, AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let app = repository::user_apps::get_by_id(&pool, app_id)
|
let app = repository::user_apps::get_by_id(&pool, app_id)
|
||||||
.await?
|
.await?
|
||||||
.filter(|app| app.user_id == user_id)
|
.filter(|app| app.user_id == user_id)
|
||||||
@@ -68,10 +78,15 @@ async fn get_user_app(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool, request))]
|
#[tracing::instrument(skip(pool, request))]
|
||||||
async fn upsert_user_app(
|
async fn upsert_user_app(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
||||||
Json(request): Json<UserAppRequest>,
|
Json(request): Json<UserAppRequest>,
|
||||||
) -> Result<(StatusCode, Json<UserAppResponse>), AppError> {
|
) -> Result<(StatusCode, Json<UserAppResponse>), AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let app = request.into_domain(user_id, app_id);
|
let app = request.into_domain(user_id, app_id);
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||||
@@ -97,9 +112,14 @@ async fn upsert_user_app(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_user_app(
|
async fn delete_user_app(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
Path((user_id, app_id)): Path<(Uuid, Uuid)>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
if auth.user_id() != user_id {
|
||||||
|
return Err(AppError::NotFound);
|
||||||
|
}
|
||||||
|
|
||||||
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
let mut tx = pool.begin().await.map_err(RepositoryError::from)?;
|
||||||
if repository::user_apps::delete_by_id(&mut *tx, app_id)
|
if repository::user_apps::delete_by_id(&mut *tx, app_id)
|
||||||
.await?
|
.await?
|
||||||
|
|||||||
@@ -5,12 +5,9 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
dtos::user::{UserRequest, UserResponse},
|
dtos::user::{UserRequest, UserResponse},
|
||||||
extractors::{json::Json, path::Path, query::Query},
|
extractors::{auth::Auth, json::Json, path::Path, query::Query},
|
||||||
};
|
|
||||||
use crate::{
|
|
||||||
error::AppError,
|
|
||||||
repository::{self},
|
|
||||||
};
|
};
|
||||||
|
use crate::{error::AppError, repository};
|
||||||
|
|
||||||
pub fn create_router() -> axum::Router<PgPool> {
|
pub fn create_router() -> axum::Router<PgPool> {
|
||||||
axum::Router::new()
|
axum::Router::new()
|
||||||
@@ -32,6 +29,7 @@ pub struct QueryParams {
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn list_users(
|
async fn list_users(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Query(query): Query<QueryParams>,
|
Query(query): Query<QueryParams>,
|
||||||
) -> Result<Json<Vec<UserResponse>>, AppError> {
|
) -> Result<Json<Vec<UserResponse>>, AppError> {
|
||||||
@@ -52,6 +50,7 @@ async fn list_users(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn get_user(
|
async fn get_user(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
) -> Result<Json<UserResponse>, AppError> {
|
) -> Result<Json<UserResponse>, AppError> {
|
||||||
@@ -65,6 +64,7 @@ async fn get_user(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool, user))]
|
#[tracing::instrument(skip(pool, user))]
|
||||||
async fn upsert_user(
|
async fn upsert_user(
|
||||||
|
Auth(auth): Auth,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
Json(user): Json<UserRequest>,
|
Json(user): Json<UserRequest>,
|
||||||
@@ -84,6 +84,7 @@ async fn upsert_user(
|
|||||||
#[axum::debug_handler]
|
#[axum::debug_handler]
|
||||||
#[tracing::instrument(skip(pool))]
|
#[tracing::instrument(skip(pool))]
|
||||||
async fn delete_user(
|
async fn delete_user(
|
||||||
|
Auth(auth): Auth,
|
||||||
Path(id): Path<Uuid>,
|
Path(id): Path<Uuid>,
|
||||||
State(pool): State<PgPool>,
|
State(pool): State<PgPool>,
|
||||||
) -> Result<StatusCode, AppError> {
|
) -> Result<StatusCode, AppError> {
|
||||||
|
|||||||
Reference in New Issue
Block a user