Skip to content

Commit

Permalink
add user extractor and handle multipart formdata uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
thebino committed Aug 25, 2023
1 parent 48c0132 commit bfdbae0
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 43 deletions.
70 changes: 49 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ oauth_authorization_server = { path = "./crates/oauth_authorization_server" }
oauth_authentication = { path = "./crates/oauth_authentication" }

activitypub_federation = "0.4.6"
async-trait = { version = "0.1.73" }
axum = { version = "0.6.20", features = ["ws", "headers"] }
axum-test = { version = "12.1.0" }
anyhow = { version = "1.0.72" }

chrono = { version = "0.4.26", features = ["serde"] }

http = { version = "0.2.9" }
hyper = { version = "0.14", features = ["full"] }
mime = { version = "0.3" }
mockall = { version = "0.11.4" }
Expand All @@ -59,6 +61,7 @@ sea-orm = { version = "0.12.2", features = [ "runtime-tokio-rustls", "debug-prin
serde = "1.0.183"
serde_json = { version = "1.0.104", features = ["raw_value"] }

time = { version = "0.3.27" }
tokio = { version = "1.30.0", features = ["full"] }
tower = { version = "0.4.13", features = ["util"] }
tower-http = { version = "0.4.3", features = ["fs", "tracing", "trace", "cors"] }
Expand Down
5 changes: 5 additions & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ path = "src/lib.rs"
doctest = false

[dependencies]
async-trait = { workspace = true }
axum = { workspace = true }
http = { workspace = true }
serde = { workspace = true, features = ["derive"] }
time = { workspace = true }
uuid = { workspace = true, features = ["serde"] }
3 changes: 2 additions & 1 deletion crates/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/

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

70 changes: 70 additions & 0 deletions crates/common/src/model/auth/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use async_trait::async_trait;
use axum::extract::FromRequestParts;
use axum::http::StatusCode;
use http::request::Parts;
use std::fmt;
use time::OffsetDateTime;
use uuid::Uuid;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct User {
uuid: Uuid,
email: String,
password: Option<String>,
lastname: Option<String>,
firstname: Option<String>,
is_locked: bool,
created_at: OffsetDateTime,
updated_at: Option<OffsetDateTime>,
last_login: Option<OffsetDateTime>,
}

impl fmt::Display for User {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{} ({}), locked:{})",
self.email, self.uuid, self.is_locked
)
}
}

impl User {
fn new(email: String) -> User {
User {
uuid: Uuid::new_v4(),
email,
password: Option::None,
lastname: Option::None,
firstname: Option::None,
is_locked: false,
created_at: OffsetDateTime::now_utc(),
updated_at: Option::None,
last_login: Option::None,
}
}
}

#[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"))
}
}
7 changes: 7 additions & 0 deletions crates/media/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ path = "src/lib.rs"
doctest = false

[dependencies]
common = { path = "../common" }

tracing = { workspace = true }
# serialization
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
Expand All @@ -29,6 +32,10 @@ mime = { workspace = true }
sea-orm = { workspace = true }
log = "0.4.19"


uuid = { workspace = true, features = ["serde"] }


# testing
mockall = { workspace = true }
rstest = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/media/src/api/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mod tests {
Request::builder()
.uri("/media?limit=100000&offset=1")
.method("GET")
.header("Authorization", "FakeAuth")
.body(Body::empty())
.unwrap(),
)
Expand All @@ -128,6 +129,7 @@ mod tests {
Request::builder()
.uri("/media")
.method("GET")
.header("Authorization", "FakeAuth")
.body(Body::empty())
.unwrap(),
)
Expand All @@ -145,6 +147,7 @@ mod tests {

// TODO: re-enable test
// #[tokio::test]
#[allow(dead_code)]
async fn post_media_success() {
// given
let app = Router::new().nest("/", MediaApi::routes());
Expand Down
Loading

0 comments on commit bfdbae0

Please sign in to comment.