-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16a0b65
commit 7233987
Showing
9 changed files
with
205 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,68 @@ | ||
//! 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, | ||
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(|e| UserDetailError::Generic(e))?; | ||
|
||
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