Skip to content

Commit

Permalink
update openidconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 22, 2024
1 parent dcb721d commit a713716
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
19 changes: 9 additions & 10 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/kitsune-oidc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ license.workspace = true
[dependencies]
enum_dispatch = "0.3.13"
http = "1.1.0"
http-compat = { path = "../../lib/http-compat" }
kitsune-config = { path = "../kitsune-config" }
kitsune-error = { path = "../kitsune-error" }
kitsune-http-client = { path = "../kitsune-http-client" }
moka = { version = "0.12.7", features = ["future"] }
multiplex-pool = { path = "../../lib/multiplex-pool" }
oauth2 = { version = "5.0.0-alpha.4", default-features = false }
once_cell = "1.19.0"
openidconnect = { version = "3.5.0", default-features = false, features = [
openidconnect = { version = "4.0.0-alpha.1", default-features = false, features = [
# Accept these two, per specification invalid, cases to increase compatibility
"accept-rfc3339-timestamps",
"accept-string-booleans",
Expand Down
20 changes: 7 additions & 13 deletions crates/kitsune-oidc/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
use http::Request;
use http_compat::Compat;
use kitsune_http_client::Client as HttpClient;
use once_cell::sync::Lazy;
use openidconnect::{HttpRequest, HttpResponse};

static HTTP_CLIENT: Lazy<HttpClient> = Lazy::new(HttpClient::default);

pub async fn async_client(req: HttpRequest) -> Result<HttpResponse, kitsune_http_client::Error> {
let mut request = Request::builder()
.method(req.method.compat())
.uri(req.url.as_str());
*request.headers_mut().unwrap() = req.headers.compat();
let request = request.body(req.body.into()).unwrap();
let response = HTTP_CLIENT.execute(request).await?;
let response = HTTP_CLIENT.execute(req.map(Into::into)).await?;

Check warning on line 8 in crates/kitsune-oidc/src/http.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/http.rs#L8

Added line #L8 was not covered by tests

Ok(HttpResponse {
status_code: response.status().compat(),
headers: response.headers().clone().compat(),
body: response.bytes().await?.to_vec(),
})
let mut builder = http::Response::builder()
.status(response.status())
.version(response.version());
*builder.headers_mut().unwrap() = response.headers().clone();

Ok(builder.body(response.bytes().await?.to_vec()).unwrap())

Check warning on line 15 in crates/kitsune-oidc/src/http.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/http.rs#L10-L15

Added lines #L10 - L15 were not covered by tests
}
44 changes: 39 additions & 5 deletions crates/kitsune-oidc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,38 @@ use openidconnect::{
use speedy_uuid::Uuid;
use url::Url;

type OidcClient = openidconnect::Client<
openidconnect::EmptyAdditionalClaims,
openidconnect::core::CoreAuthDisplay,
openidconnect::core::CoreGenderClaim,
openidconnect::core::CoreJweContentEncryptionAlgorithm,
openidconnect::core::CoreJsonWebKey,
openidconnect::core::CoreAuthPrompt,
openidconnect::StandardErrorResponse<oauth2::basic::BasicErrorResponseType>,
openidconnect::StandardTokenResponse<
openidconnect::IdTokenFields<
openidconnect::EmptyAdditionalClaims,
openidconnect::EmptyExtraTokenFields,
openidconnect::core::CoreGenderClaim,
openidconnect::core::CoreJweContentEncryptionAlgorithm,
openidconnect::core::CoreJwsSigningAlgorithm,
>,
oauth2::basic::BasicTokenType,
>,
openidconnect::StandardTokenIntrospectionResponse<
openidconnect::EmptyExtraTokenFields,
oauth2::basic::BasicTokenType,
>,
oauth2::StandardRevocableToken,
openidconnect::StandardErrorResponse<openidconnect::RevocationErrorResponseType>,
openidconnect::EndpointSet,
openidconnect::EndpointNotSet,
openidconnect::EndpointNotSet,
openidconnect::EndpointNotSet,
openidconnect::EndpointMaybeSet,
openidconnect::EndpointMaybeSet,
>;

mod state;

pub mod http;
Expand All @@ -36,7 +68,7 @@ pub struct UserInfo {

#[derive(Clone)]
pub struct OidcService {
client: CoreClient,
client: OidcClient,
login_state_store: self::state::AnyStore,
}

Expand All @@ -45,7 +77,7 @@ impl OidcService {
pub async fn initialise(config: &Configuration, redirect_uri: String) -> Result<Self> {
let provider_metadata = CoreProviderMetadata::discover_async(
IssuerUrl::new(config.server_url.to_string())?,
self::http::async_client,
&self::http::async_client,

Check warning on line 80 in crates/kitsune-oidc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/lib.rs#L80

Added line #L80 was not covered by tests
)
.await?;

Expand Down Expand Up @@ -125,20 +157,22 @@ impl OidcService {

let token_response = self
.client
.exchange_code(AuthorizationCode::new(authorization_code))
.exchange_code(AuthorizationCode::new(authorization_code))?

Check warning on line 160 in crates/kitsune-oidc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/lib.rs#L160

Added line #L160 was not covered by tests
.set_pkce_verifier(pkce_verifier)
.request_async(self::http::async_client)
.request_async(&self::http::async_client)

Check warning on line 162 in crates/kitsune-oidc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/lib.rs#L162

Added line #L162 was not covered by tests
.await?;

let id_token = token_response
.id_token()
.ok_or_else(|| kitsune_error!("missing id token"))?;
let id_token_verifier = self.client.id_token_verifier();

Check warning on line 168 in crates/kitsune-oidc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/lib.rs#L168

Added line #L168 was not covered by tests
let claims = id_token.claims(&self.client.id_token_verifier(), &nonce)?;

if let Some(expected_hash) = claims.access_token_hash() {
let actual_hash = AccessTokenHash::from_token(
token_response.access_token(),
&id_token.signing_alg()?,
id_token.signing_alg()?,
id_token.signing_key(&id_token_verifier)?,

Check warning on line 175 in crates/kitsune-oidc/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-oidc/src/lib.rs#L174-L175

Added lines #L174 - L175 were not covered by tests
)?;

if actual_hash != *expected_hash {
Expand Down

0 comments on commit a713716

Please sign in to comment.