Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Refactor the upstream link provider template logic
Browse files Browse the repository at this point in the history
Also adds tests for new account registration through an upstream oauth2
provider
  • Loading branch information
sandhose committed Nov 13, 2023
1 parent 9c94e11 commit 89420a2
Show file tree
Hide file tree
Showing 9 changed files with 569 additions and 162 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

21 changes: 17 additions & 4 deletions crates/axum-utils/src/cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ impl CookieManager {
let key = Key::derive_from(key);
Self::new(base_url, key)
}

#[must_use]
pub fn cookie_jar(&self) -> CookieJar {
let inner = PrivateCookieJar::new(self.key.clone());
let options = self.options.clone();

CookieJar { inner, options }
}

#[must_use]
pub fn cookie_jar_from_headers(&self, headers: &http::HeaderMap) -> CookieJar {
let inner = PrivateCookieJar::from_headers(headers, self.key.clone());
let options = self.options.clone();

CookieJar { inner, options }
}
}

#[async_trait]
Expand All @@ -67,10 +83,7 @@ where

async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let cookie_manager = CookieManager::from_ref(state);
let inner = PrivateCookieJar::from_headers(&parts.headers, cookie_manager.key.clone());
let options = cookie_manager.options.clone();

Ok(CookieJar { inner, options })
Ok(cookie_manager.cookie_jar_from_headers(&parts.headers))
}
}

Expand Down
9 changes: 9 additions & 0 deletions crates/data-model/src/upstream_oauth2/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,13 @@ impl ImportAction {
pub fn is_required(&self) -> bool {
matches!(self, Self::Require)
}

#[must_use]
pub fn should_import(&self, user_preference: bool) -> bool {
match self {
Self::Ignore => false,
Self::Suggest => user_preference,
Self::Force | Self::Require => true,
}
}
}
1 change: 1 addition & 0 deletions crates/handlers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pbkdf2 = { version = "0.12.2", features = ["password-hash", "std", "simple", "pa
zeroize = "1.6.0"

# Various data types and utilities
base64ct = "1.6.0"
camino.workspace = true
chrono.workspace = true
psl = "2.1.4"
Expand Down
15 changes: 14 additions & 1 deletion crates/handlers/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use axum::{
async_trait,
body::{Bytes, HttpBody},
extract::{FromRef, FromRequestParts},
response::{IntoResponse, IntoResponseParts},
};
use cookie_store::{CookieStore, RawCookie};
use futures_util::future::BoxFuture;
Expand All @@ -31,7 +32,9 @@ use hyper::{
Request, Response, StatusCode,
};
use mas_axum_utils::{
cookies::CookieManager, http_client_factory::HttpClientFactory, ErrorWrapper,
cookies::{CookieJar, CookieManager},
http_client_factory::HttpClientFactory,
ErrorWrapper,
};
use mas_i18n::Translator;
use mas_keystore::{Encrypter, JsonWebKey, JsonWebKeySet, Keystore, PrivateKey};
Expand Down Expand Up @@ -264,6 +267,11 @@ impl TestState {
_ => panic!("Unexpected status code: {}", response.status()),
}
}

/// Get an empty cookie jar
pub fn cookie_jar(&self) -> CookieJar {
self.cookie_manager.cookie_jar()
}
}

struct TestGraphQLState {
Expand Down Expand Up @@ -631,6 +639,11 @@ impl CookieHelper {
&url,
);
}

pub fn import(&self, res: impl IntoResponseParts) {
let response = (res, "").into_response();
self.save_cookies(&response);
}
}

impl<S> Layer<S> for CookieHelper {
Expand Down
Loading

0 comments on commit 89420a2

Please sign in to comment.