Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert unintentional change for auth key generation API endpoint #109

Merged
merged 3 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/config.toml
/data.db
/.vscode/launch.json

101 changes: 99 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ uuid = { version = "1", features = ["v4"] }

[dev-dependencies]
mockall = "0.11"
reqwest = { version = "0.11.13", features = ["json"] }
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod resources;
pub mod server;
89 changes: 89 additions & 0 deletions src/api/resources/auth_key_resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::convert::From;

use serde::{Deserialize, Serialize};

use crate::key::AuthKey;
use crate::protocol::clock::DurationSinceUnixEpoch;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct AuthKeyResource {
pub key: String,
pub valid_until: Option<u64>,
}

impl From<AuthKeyResource> for AuthKey {
fn from(auth_key_resource: AuthKeyResource) -> Self {
AuthKey {
key: auth_key_resource.key,
valid_until: auth_key_resource
.valid_until
.map(|valid_until| DurationSinceUnixEpoch::new(valid_until, 0)),
}
}
}

impl From<AuthKey> for AuthKeyResource {
fn from(auth_key: AuthKey) -> Self {
AuthKeyResource {
key: auth_key.key,
valid_until: auth_key.valid_until.map(|valid_until| valid_until.as_secs()),
}
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;

use super::AuthKeyResource;
use crate::key::AuthKey;
use crate::protocol::clock::{DefaultClock, TimeNow};

#[test]
fn it_should_be_convertible_into_an_auth_key() {
let duration_in_secs = 60;

let auth_key_resource = AuthKeyResource {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(duration_in_secs),
};

assert_eq!(
AuthKey::from(auth_key_resource),
AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(DefaultClock::add(&Duration::new(duration_in_secs, 0)).unwrap())
}
)
}

#[test]
fn it_should_be_convertible_from_an_auth_key() {
let duration_in_secs = 60;

let auth_key = AuthKey {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(DefaultClock::add(&Duration::new(duration_in_secs, 0)).unwrap()),
};

assert_eq!(
AuthKeyResource::from(auth_key),
AuthKeyResource {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(duration_in_secs)
}
)
}

#[test]
fn it_should_be_convertible_into_json() {
assert_eq!(
serde_json::to_string(&AuthKeyResource {
key: "IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM".to_string(), // cspell:disable-line
valid_until: Some(60)
})
.unwrap(),
"{\"key\":\"IaWDneuFNZi8IB4MPA3qW1CD0M30EZSM\",\"valid_until\":60}" // cspell:disable-line
);
}
}
9 changes: 9 additions & 0 deletions src/api/resources/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! These are the Rest API resources.
//!
//! WIP. Not all endpoints have their resource structs.
//!
//! - [x] AuthKeys
//! - [ ] ...
//! - [ ] ...
//! - [ ] ...
pub mod auth_key_resource;
3 changes: 2 additions & 1 deletion src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::time::Duration;
use serde::{Deserialize, Serialize};
use warp::{filters, reply, serve, Filter};

use super::resources::auth_key_resource::AuthKeyResource;
use crate::peer::TorrentPeer;
use crate::protocol::common::*;
use crate::tracker::TorrentTracker;
Expand Down Expand Up @@ -267,7 +268,7 @@ pub fn start(socket_addr: SocketAddr, tracker: Arc<TorrentTracker>) -> impl warp
})
.and_then(|(seconds_valid, tracker): (u64, Arc<TorrentTracker>)| async move {
match tracker.generate_auth_key(Duration::from_secs(seconds_valid)).await {
Ok(auth_key) => Ok(warp::reply::json(&auth_key)),
Ok(auth_key) => Ok(warp::reply::json(&AuthKeyResource::from(auth_key))),
Err(..) => Err(warp::reject::custom(ActionStatus::Err {
reason: "failed to generate key".into(),
})),
Expand Down
Loading