feat: delete_oauth_access_token, if one exists before saving new access_token

This commit is contained in:
Aravinth Manivannan 2024-05-12 21:20:59 +05:30
parent 9437d50831
commit 96946b7007
Signed by: realaravinth
GPG key ID: F8F50389936984FF
2 changed files with 19 additions and 8 deletions

View file

@ -7,5 +7,5 @@ pub trait ProcessAuthorizationResponseUseCase {
async fn process_authorization_response(
&self,
cmd: command::ProcessAuthorizationResponseCommand,
) -> errors::ProcessAuthorizationServiceResult<()>;
) -> errors::ProcessAuthorizationServiceResult<String>;
}

View file

@ -3,8 +3,8 @@ use std::sync::Arc;
use url::Url;
use crate::auth::application::port::out::db::{
delete_oauth_state::DeleteOAuthState, oauth_state_exists::OAuthStateExists,
save_oauth_access_token::SaveOAuthAccessToken,
delete_oauth_access_token::DeleteOAuthAccessToken, delete_oauth_state::DeleteOAuthState,
oauth_state_exists::OAuthStateExists, save_oauth_access_token::SaveOAuthAccessToken,
};
use crate::auth::application::port::out::forge::{
get_username::GetUsername, request_access_token::RequestAccessToken,
@ -16,6 +16,7 @@ pub struct ProcessAuthorizationResponseService {
oauth_state_exists_adapter: Arc<dyn OAuthStateExists>,
delete_oauth_state_adapter: Arc<dyn DeleteOAuthState>,
save_oauth_access_token_adapter: Arc<dyn SaveOAuthAccessToken>,
delete_oauth_access_token_adapter: Arc<dyn DeleteOAuthAccessToken>,
request_access_token_adapter: Arc<dyn RequestAccessToken>,
get_username_adapter: Arc<dyn GetUsername>,
process_authorization_response_redirect_uri: Url,
@ -26,6 +27,7 @@ impl ProcessAuthorizationResponseService {
oauth_state_exists_adapter: Arc<dyn OAuthStateExists>,
delete_oauth_state_adapter: Arc<dyn DeleteOAuthState>,
save_oauth_access_token_adapter: Arc<dyn SaveOAuthAccessToken>,
delete_oauth_access_token_adapter: Arc<dyn DeleteOAuthAccessToken>,
request_access_token_adapter: Arc<dyn RequestAccessToken>,
get_username_adapter: Arc<dyn GetUsername>,
process_authorization_response_redirect_uri: Url,
@ -34,6 +36,7 @@ impl ProcessAuthorizationResponseService {
oauth_state_exists_adapter,
delete_oauth_state_adapter,
save_oauth_access_token_adapter,
delete_oauth_access_token_adapter,
request_access_token_adapter,
get_username_adapter,
process_authorization_response_redirect_uri,
@ -46,7 +49,7 @@ impl ProcessAuthorizationResponseUseCase for ProcessAuthorizationResponseService
async fn process_authorization_response(
&self,
cmd: command::ProcessAuthorizationResponseCommand,
) -> ProcessAuthorizationServiceResult<()> {
) -> ProcessAuthorizationServiceResult<String> {
if let Some(u) = cmd.redirect_uri() {
if u.host() != self.process_authorization_response_redirect_uri.host()
&& u.path() != self.process_authorization_response_redirect_uri.path()
@ -83,11 +86,15 @@ impl ProcessAuthorizationResponseUseCase for ProcessAuthorizationResponseService
.get_username(&access_token)
.await?;
self.delete_oauth_access_token_adapter
.delete_oauth_access_token(&username, &cmd.oauth_provider())
.await?;
self.save_oauth_access_token_adapter
.save_oauth_access_token(&username, cmd.oauth_provider(), &access_token)
.await?;
Ok(())
Ok(username)
}
}
@ -96,8 +103,8 @@ mod tests {
use super::*;
use crate::auth::application::port::out::db::{
delete_oauth_state::tests::*, oauth_state_exists::tests::*,
save_oauth_access_token::tests::*,
delete_oauth_access_token::tests::*, delete_oauth_state::tests::*,
oauth_state_exists::tests::*, save_oauth_access_token::tests::*,
};
use crate::auth::application::port::out::forge::{
@ -128,11 +135,15 @@ mod tests {
mock_oauth_state_exists(IS_CALLED_ONLY_ONCE, RETURNS_TRUE),
mock_delete_oauth_state(IS_CALLED_ONLY_ONCE),
mock_save_oauth_access_token(IS_CALLED_ONLY_ONCE),
mock_delete_oauth_access_token(IS_CALLED_ONLY_ONCE),
mock_request_access_token(IS_CALLED_ONLY_ONCE),
mock_get_username(username.into(), IS_CALLED_ONLY_ONCE),
redirect_uri,
);
s.process_authorization_response(cmd).await.unwrap();
assert_eq!(
s.process_authorization_response(cmd).await.unwrap(),
username
);
}
}