Skip to content

Commit

Permalink
add optional user extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Aug 27, 2023
1 parent bfdbae0 commit a154ea8
Show file tree
Hide file tree
Showing 26 changed files with 212 additions and 96 deletions.
2 changes: 1 addition & 1 deletion crates/accounts/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub(crate) mod routes;
pub mod router;
pub(crate) mod routes;
6 changes: 1 addition & 5 deletions crates/accounts/src/api/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,25 @@ impl AccountsApi {
// 401 Unauthorized - Requesting user is unauthenticated
// 404 Not Found - The requested resource does not exist.
.route("/users/:user_id/profile", get(get_user_id_profile))

// Update a single account when `admin.users:write` scope is present
// 200 - OK
// 400 Bad Request - The request body was malformed or a field violated its constraints.
// 400 Bad Request - The request body was malformed or a field violated its constraints.
// 401 Unauthorized - You are unauthenticated
// 403 Forbidden - You are authenticated but have no permission to manage the target user.
// 404 Not Found - The requested resource does not exist.
.route("/users/:user_id/profile", patch(get_user_id_profile))

// Disable a single account by ID when `admin.users:write` scope is present
// 204 No Content - Account was disabled successful
// 401 Unauthorized - You are unauthenticated
// 403 Forbidden - You are authenticated but have no permission to manage the target user.
// 404 Not Found - The requested resource does not exist.
.route("/users/:user_id/disable", patch(get_user_id_profile))

// Enable a single account by ID when `admin.users:write` scope is present
// 204 No Content - Account was enabled successful
// 401 Unauthorized - You are unauthenticated
// 403 Forbidden - You are authenticated but have no permission to manage the target user.
// 404 Not Found - The requested resource does not exist.
.route("/users/:user_id/enabled", patch(get_user_id_profile))

.layer(tower_http::trace::TraceLayer::new_for_http())
}
}
2 changes: 1 addition & 1 deletion crates/accounts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
*/

//! This crate handles all account related tasks in [Photos.network](https://photos.network) core application.
//!
//!
pub mod api;
2 changes: 1 addition & 1 deletion crates/activity_pub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//! This crate provides the [ActivityPub](https://www.w3.org/TR/activitypub/) implementation for [Photos.network](https://photos.network).
//!
//!
18 changes: 18 additions & 0 deletions crates/common/src/http/extractors/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod optuser;
pub mod user;
47 changes: 47 additions & 0 deletions crates/common/src/http/extractors/optuser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

//! This extractor checks if an `Authorization` is header and contains a valid JWT token.
//! Otherwise it will respond `Some(None)` to indicate an unauthorized user or a visiter without an account at all.
//!
use crate::model::auth::user::User;
use async_trait::async_trait;
use axum::extract::FromRequestParts;
use http::request::Parts;

pub struct OptionalUser(pub Option<User>);

#[async_trait]
impl<S> FromRequestParts<S> for OptionalUser
where
S: Send + Sync,
{
type Rejection = String;

async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
parts

Check failure on line 36 in crates/common/src/http/extractors/optuser.rs

View workflow job for this annotation

GitHub Actions / Lints

using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`
.headers
.get("Authorization")
.and_then(|header| {
let _auth_token = header.to_str().ok();

// TODO: verify auth token
Some(Self(Some(User::new("[email protected]".to_string()))))
})
.ok_or("".to_string())
}
}
50 changes: 50 additions & 0 deletions crates/common/src/http/extractors/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

//! This extractor requires an `Authorization` header present with a valid JWT token.
//! Otherwise it will respond with `StatusCode::UNAUTHORIZED`
//!
use async_trait::async_trait;
use axum::extract::FromRequestParts;
use axum::http::StatusCode;
use http::request::Parts;

use crate::model::auth::user::User;

#[async_trait]
impl<S> FromRequestParts<S> for User
where
S: Send + Sync,
{
type Rejection = (StatusCode, &'static str);

async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let _auth_token = parts
.headers
.get("Authorization")
.and_then(|header| header.to_str().ok())
.ok_or((StatusCode::UNAUTHORIZED, "Unauthorized"))?;

// TODO: get user for Authtoken
Ok(User::new("[email protected]".to_string()))
// TODO: verify Token
// verify_auth_token(auth_header)
// .await
// .map_err(|_| (StatusCode::UNAUTHORIZED, "Unauthorized"))
//Err((StatusCode::UNAUTHORIZED, "Unauthorized"))
}
}
17 changes: 17 additions & 0 deletions crates/common/src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod extractors;
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

//! This crate offers shared data models for [Photos.network](https://photos.network) core application.
//!
pub mod http;
pub mod model {
pub mod auth;
pub mod sensitive;
Expand Down
1 change: 0 additions & 1 deletion crates/common/src/model/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod user;

47 changes: 18 additions & 29 deletions crates/common/src/model/auth/user.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use async_trait::async_trait;
use axum::extract::FromRequestParts;
use axum::http::StatusCode;
use http::request::Parts;
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

use std::fmt;
use time::OffsetDateTime;
use uuid::Uuid;
Expand Down Expand Up @@ -30,7 +43,7 @@ impl fmt::Display for User {
}

impl User {
fn new(email: String) -> User {
pub(crate) fn new(email: String) -> User {
User {
uuid: Uuid::new_v4(),
email,
Expand All @@ -44,27 +57,3 @@ impl User {
}
}
}

#[async_trait]
impl<S> FromRequestParts<S> for User
where
S: Send + Sync,
{
type Rejection = (StatusCode, &'static str);

async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let _auth_token = parts
.headers
.get("Authorization")
.and_then(|header| header.to_str().ok())
.ok_or((StatusCode::UNAUTHORIZED, "Unauthorized"))?;

// TODO: get user for Authtoken
Ok(User::new("[email protected]".to_string()))
// TODO: verify Token
// verify_auth_token(auth_header)
// .await
// .map_err(|_| (StatusCode::UNAUTHORIZED, "Unauthorized"))
//Err((StatusCode::UNAUTHORIZED, "Unauthorized"))
}
}
2 changes: 1 addition & 1 deletion crates/media/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub(crate) mod routes;
pub mod router;
pub(crate) mod routes;
2 changes: 1 addition & 1 deletion crates/media/src/api/routes/delete_media_id.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Deletes the given item owned by the user
//!
//!
use axum::http::StatusCode;

pub(crate) async fn delete_media_id() -> std::result::Result<String, StatusCode> {
Expand Down
9 changes: 5 additions & 4 deletions crates/media/src/api/routes/get_media.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Returns a list of owned media items for current user
//!
use axum::{extract::Query, http::StatusCode, Json};
use common::http::extractors::{self, optuser::OptionalUser};

Check warning on line 4 in crates/media/src/api/routes/get_media.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `self`

Check warning on line 4 in crates/media/src/api/routes/get_media.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `self`
use common::model::auth::user::User;

Check warning on line 5 in crates/media/src/api/routes/get_media.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `common::model::auth::user::User`

Check warning on line 5 in crates/media/src/api/routes/get_media.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `common::model::auth::user::User`
use serde::{Deserialize, Serialize};
use std::result::Result;
Expand All @@ -12,10 +13,10 @@ pub(crate) struct MediaListQuery {
}

pub(crate) async fn get_media(
user: User,
user: OptionalUser,

Check warning on line 16 in crates/media/src/api/routes/get_media.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `user`

Check warning on line 16 in crates/media/src/api/routes/get_media.rs

View workflow job for this annotation

GitHub Actions / Check

unused variable: `user`
Query(query): Query<MediaListQuery>,
) -> Result<Json<String>, StatusCode> {
tracing::error!("GET /media user={}", user);
//tracing::error!("GET /media user={}", user);
// TODO: check auth header
// TODO: read list from persistency
// TODO: return list
Expand All @@ -40,7 +41,7 @@ mod tests {
use super::*;

#[tokio::test]
async fn get_media_unauthorized_should_fail() {
async fn get_media_unauthorized_should_not_fail() {
// given
let app = Router::new().nest("/", MediaApi::routes());

Expand All @@ -57,6 +58,6 @@ mod tests {
.unwrap();

// then
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
assert_eq!(response.status(), StatusCode::OK);
}
}
12 changes: 6 additions & 6 deletions crates/media/src/api/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
pub(crate) mod get_media;
pub(crate) mod post_media;
pub(crate) mod get_media_id;
pub(crate) mod post_media_id;
pub(crate) mod patch_media_id;
pub(crate) mod delete_media_id;
pub(crate) mod get_albums;
pub(crate) mod post_albums;
pub(crate) mod get_albums_id;
pub(crate) mod get_media;
pub(crate) mod get_media_id;
pub(crate) mod patch_albums_id;
pub(crate) mod patch_albums_id_share;
pub(crate) mod patch_albums_id_unshare;
pub(crate) mod patch_media_id;
pub(crate) mod post_albums;
pub(crate) mod post_media;
pub(crate) mod post_media_id;
6 changes: 3 additions & 3 deletions crates/media/src/data/exif_info.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Expand Down
6 changes: 3 additions & 3 deletions crates/media/src/data/file.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Expand Down
8 changes: 4 additions & 4 deletions crates/media/src/data/location.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* Photos.network · A privacy first photo storage and sharing service for fediverse.
* Copyright (C) 2020 Photos network developers
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

pub struct Location {
pub latitude: f64,
pub longitude: f64
pub longitude: f64,
}
Loading

0 comments on commit a154ea8

Please sign in to comment.