-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds functionality to unFTP that allows unFTP to obtain user detail over HTTP in addition to the already existing JSON file. It uses the extact same format as the JSON file functionality. The 'usr-http-url' command line arguments activate this feature. You pass it a base URL, unFTP appends a username to it and performs a GET request to the URL. The HTTP server should respond with a 200 OK and JSON body containing an array of users which should at least contain the requested user's details. Later on we can support diffent HTTP verbs and sending the username via an HTTP header instead of the URL path or Post body. For now I'm just keeping it simple.
- Loading branch information
1 parent
b76ec5e
commit 02c6c4e
Showing
9 changed files
with
207 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
//! Infra contains infrastructure specific implementations of things in the [`domain`](crate::domain) | ||
//! module. | ||
mod pubsub; | ||
mod workload_identity; | ||
|
||
pub mod userdetail_http; | ||
pub mod usrdetail_json; | ||
mod workload_identity; | ||
|
||
pub use pubsub::PubsubEventDispatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! A libunftp [`UserDetail`](libunftp::auth::user::UserDetail) provider that obtains user detail | ||
//! over HTTP. | ||
|
||
use crate::domain::user::{User, UserDetailError, UserDetailProvider}; | ||
use crate::infra::usrdetail_json::JsonUserProvider; | ||
use async_trait::async_trait; | ||
use http::{Method, Request}; | ||
use hyper::{Body, Client}; | ||
use url::form_urlencoded; | ||
|
||
/// A libunftp [`UserDetail`](libunftp::auth::user::UserDetail) provider that obtains user detail | ||
/// over HTTP. | ||
#[derive(Debug)] | ||
pub struct HTTPUserDetailProvider { | ||
url: String, | ||
#[allow(dead_code)] | ||
header_name: Option<String>, | ||
} | ||
|
||
impl HTTPUserDetailProvider { | ||
/// Creates a provider that will obtain user detail from the specified URL. | ||
pub fn new(url: impl Into<String>) -> HTTPUserDetailProvider { | ||
HTTPUserDetailProvider { | ||
url: url.into(), | ||
header_name: None, | ||
} | ||
} | ||
} | ||
|
||
impl Default for HTTPUserDetailProvider { | ||
fn default() -> Self { | ||
HTTPUserDetailProvider { | ||
url: "http://localhost:8080/users/".to_string(), | ||
header_name: None, | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl UserDetailProvider for HTTPUserDetailProvider { | ||
async fn provide_user_detail(&self, username: &str) -> Result<User, UserDetailError> { | ||
let _url_suffix: String = form_urlencoded::byte_serialize(username.as_bytes()).collect(); | ||
let req = Request::builder() | ||
.method(Method::GET) | ||
.header("Content-type", "application/json") | ||
.uri(format!("{}{}", self.url, username)) | ||
.body(Body::empty()) | ||
.map_err(|e| UserDetailError::with_source("error creating request", e))?; | ||
|
||
let client = Client::new(); | ||
|
||
let resp = client | ||
.request(req) | ||
.await | ||
.map_err(|e| UserDetailError::with_source("error doing HTTP request", e))?; | ||
|
||
let body_bytes = hyper::body::to_bytes(resp.into_body()) | ||
.await | ||
.map_err(|e| UserDetailError::with_source("error parsing body", e))?; | ||
|
||
let json_str = std::str::from_utf8(body_bytes.as_ref()) | ||
.map_err(|e| UserDetailError::with_source("body is not a valid UTF string", e))?; | ||
|
||
let json_usr_provider = | ||
JsonUserProvider::from_json(json_str).map_err(UserDetailError::Generic)?; | ||
|
||
json_usr_provider.provide_user_detail(username).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters