chore: rename GetRedirectUri->OAuthAuthReqUri and tests
This commit is contained in:
parent
3640774c6a
commit
aba028fdd8
|
@ -61,7 +61,7 @@ impl ServiceFactory {
|
|||
if let Some(forge) = ctx.adapters.forges.get_forge(&forge_name) {
|
||||
Ok(Arc::new(RequestAuthorizationService::new(
|
||||
ctx.adapters.db.save_oauth_state_adapter.clone(),
|
||||
forge.get_redirect_uri_adapter.clone(),
|
||||
forge.oauth_auth_req_uri_adapter.clone(),
|
||||
Url::parse(&format!(
|
||||
"{}://{}{}",
|
||||
"http",
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
use url::Url;
|
||||
|
||||
use super::Forgejo;
|
||||
use crate::forge::auth::application::port::out::forge::{
|
||||
errors::OutForgePortResult, get_redirect_uri::GetRedirectUri,
|
||||
};
|
||||
|
||||
impl GetRedirectUri for Forgejo {
|
||||
fn get_redirect_uri(
|
||||
&self,
|
||||
state: &str,
|
||||
process_authorization_response_uri: &Url,
|
||||
) -> OutForgePortResult<Url> {
|
||||
let mut u = self.url().to_owned();
|
||||
u.set_path("/login/oauth/authorize");
|
||||
u.set_query(Some(&format!(
|
||||
"client_id={}&redirect_uri={}&response_type=code&state={state}",
|
||||
self.client_id(),
|
||||
process_authorization_response_uri.as_str()
|
||||
)));
|
||||
|
||||
Ok(u)
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
use url::Url;
|
||||
|
||||
mod get_redirect_uri;
|
||||
mod oauth_auth_req_uri;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Forgejo {
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
use url::Url;
|
||||
|
||||
use super::Forgejo;
|
||||
use crate::forge::auth::application::port::out::forge::{
|
||||
errors::OutForgePortResult, oauth_auth_req_uri::OAuthAuthReqUri,
|
||||
};
|
||||
|
||||
impl OAuthAuthReqUri for Forgejo {
|
||||
fn oauth_auth_req_uri(
|
||||
&self,
|
||||
state: &str,
|
||||
process_authorization_response_uri: &Url,
|
||||
) -> OutForgePortResult<Url> {
|
||||
let mut u = self.url().to_owned();
|
||||
u.set_path("/login/oauth/authorize/");
|
||||
u.set_query(Some(&format!(
|
||||
"client_id={}&redirect_uri={}&response_type=code&state={state}",
|
||||
self.client_id(),
|
||||
process_authorization_response_uri.as_str()
|
||||
)));
|
||||
|
||||
Ok(u)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: change trait, file, method names to OAuth authentication req uri
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_forgejo_oauth_auth_req_uri() {
|
||||
let u = Url::parse("https://forgejo").unwrap();
|
||||
let client_id = "id";
|
||||
let client_secret = "sec";
|
||||
let state = "st";
|
||||
let process_authorization_response_uri = Url::parse("https://forgeflux").unwrap();
|
||||
|
||||
let f = Forgejo::new(u.clone(), client_id.into(), client_secret.into());
|
||||
let oauth_auth_req_uri = f
|
||||
.oauth_auth_req_uri(state.into(), &process_authorization_response_uri)
|
||||
.unwrap();
|
||||
|
||||
let path = "/login/oauth/authorize";
|
||||
let q = format!(
|
||||
"client_id={}&redirect_uri={}&response_type=code&state={state}",
|
||||
client_id,
|
||||
process_authorization_response_uri.as_str()
|
||||
);
|
||||
assert_eq!(
|
||||
oauth_auth_req_uri.to_string(),
|
||||
format!("https://forgejo{path}/?{q}")
|
||||
);
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@ use std::sync::Arc;
|
|||
use derive_more::Display;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::forge::auth::application::port::out::forge::get_redirect_uri::GetRedirectUri;
|
||||
use crate::forge::auth::application::port::out::forge::oauth_auth_req_uri::OAuthAuthReqUri;
|
||||
|
||||
use self::forgejo::Forgejo;
|
||||
|
||||
|
@ -13,7 +13,7 @@ pub mod forgejo;
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct ForgeAdapter {
|
||||
pub get_redirect_uri_adapter: Arc<dyn GetRedirectUri>,
|
||||
pub oauth_auth_req_uri_adapter: Arc<dyn OAuthAuthReqUri>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
|
@ -44,7 +44,7 @@ impl ForgeRepository {
|
|||
|
||||
pub fn new(forgejo: Forgejo) -> Self {
|
||||
let forgejo_adapter = ForgeAdapter {
|
||||
get_redirect_uri_adapter: Arc::new(forgejo),
|
||||
oauth_auth_req_uri_adapter: Arc::new(forgejo),
|
||||
};
|
||||
let mut s = Self::default();
|
||||
s.add_forge(SupportedForges::Forgejo, forgejo_adapter);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
pub mod errors;
|
||||
pub mod get_redirect_uri;
|
||||
pub mod oauth_auth_req_uri;
|
||||
pub mod refresh_access_token;
|
||||
pub mod request_access_token;
|
||||
|
||||
|
@ -10,14 +10,14 @@ pub mod tests {
|
|||
use super::*;
|
||||
|
||||
use errors::*;
|
||||
use get_redirect_uri::GetRedirectUri;
|
||||
use oauth_auth_req_uri::OAuthAuthReqUri;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct MockForge; // {
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl GetRedirectUri for MockForge {
|
||||
fn get_redirect_uri(
|
||||
impl OAuthAuthReqUri for MockForge {
|
||||
fn oauth_auth_req_uri(
|
||||
&self,
|
||||
state: &str,
|
||||
process_authorization_response_uri: &Url,
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use super::errors::*;
|
||||
use url::Url;
|
||||
|
||||
pub trait GetRedirectUri: Send + Sync {
|
||||
fn get_redirect_uri(
|
||||
pub trait OAuthAuthReqUri: Send + Sync {
|
||||
fn oauth_auth_req_uri(
|
||||
&self,
|
||||
state: &str,
|
||||
process_authorization_response_uri: &Url,
|
|
@ -4,7 +4,7 @@ use url::Url;
|
|||
|
||||
use crate::forge::auth::application::port::out::db::errors::OutDBPortError;
|
||||
use crate::forge::auth::application::port::out::db::save_oauth_state::SaveOAuthState;
|
||||
use crate::forge::auth::application::port::out::forge::get_redirect_uri::GetRedirectUri;
|
||||
use crate::forge::auth::application::port::out::forge::oauth_auth_req_uri::OAuthAuthReqUri;
|
||||
use crate::utils;
|
||||
|
||||
use super::{errors::*, RequestAuthorizationUserCase};
|
||||
|
@ -14,19 +14,19 @@ const STATE_LEN: usize = 8;
|
|||
#[derive(Clone)]
|
||||
pub struct RequestAuthorizationService {
|
||||
save_oauth_state_adapter: Arc<dyn SaveOAuthState>,
|
||||
get_redirect_uri_adapter: Arc<dyn GetRedirectUri>,
|
||||
oauth_auth_req_uri_adapter: Arc<dyn OAuthAuthReqUri>,
|
||||
process_authorization_response_redirect_uri: Url,
|
||||
}
|
||||
|
||||
impl RequestAuthorizationService {
|
||||
pub fn new(
|
||||
save_oauth_state_adapter: Arc<dyn SaveOAuthState>,
|
||||
get_redirect_uri_adapter: Arc<dyn GetRedirectUri>,
|
||||
oauth_auth_req_uri_adapter: Arc<dyn OAuthAuthReqUri>,
|
||||
process_authorization_response_redirect_uri: Url,
|
||||
) -> Self {
|
||||
Self {
|
||||
save_oauth_state_adapter,
|
||||
get_redirect_uri_adapter,
|
||||
oauth_auth_req_uri_adapter,
|
||||
process_authorization_response_redirect_uri,
|
||||
}
|
||||
}
|
||||
|
@ -58,8 +58,8 @@ impl RequestAuthorizationUserCase for RequestAuthorizationService {
|
|||
}
|
||||
}
|
||||
let redirect = self
|
||||
.get_redirect_uri_adapter
|
||||
.get_redirect_uri(&state, &self.process_authorization_response_redirect_uri)?;
|
||||
.oauth_auth_req_uri_adapter
|
||||
.oauth_auth_req_uri(&state, &self.process_authorization_response_redirect_uri)?;
|
||||
Ok(redirect)
|
||||
}
|
||||
}
|
||||
|
@ -78,13 +78,13 @@ mod tests {
|
|||
#[actix_rt::test]
|
||||
async fn test_service() {
|
||||
let save_oauth_state = MockDB::default();
|
||||
let get_redirect_uri = MockForge::default();
|
||||
let oauth_auth_req_uri = MockForge::default();
|
||||
let url = Url::parse("http://test_service_request_auth").unwrap();
|
||||
let oauth_provider = "test_service_request_auth_oauth_provider";
|
||||
|
||||
let s = RequestAuthorizationService::new(
|
||||
Arc::new(save_oauth_state.clone()),
|
||||
Arc::new(get_redirect_uri),
|
||||
Arc::new(oauth_auth_req_uri),
|
||||
url.clone(),
|
||||
);
|
||||
let cmd = RequestAuthorizationCommand::new_command(oauth_provider.to_owned()).unwrap();
|
||||
|
|
Loading…
Reference in a new issue