Update oauth2 dependency.

This commit is contained in:
Orne Brocaar 2024-07-11 09:33:09 +01:00
parent d39fbea7af
commit 2737284d2d
3 changed files with 47 additions and 19 deletions

24
Cargo.lock generated
View File

@ -838,7 +838,7 @@ dependencies = [
"lazy_static",
"lrwn",
"mime_guess",
"oauth2",
"oauth2 5.0.0-alpha.4",
"openidconnect",
"p256",
"pbjson-types",
@ -2835,6 +2835,26 @@ dependencies = [
"url",
]
[[package]]
name = "oauth2"
version = "5.0.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "098af5a5110b4deacf3200682963713b143ae9d28762b739bdb7b98429dfaf68"
dependencies = [
"base64 0.22.0",
"chrono",
"getrandom",
"http 1.1.0",
"rand",
"reqwest 0.12.3",
"serde",
"serde_json",
"serde_path_to_error",
"sha2",
"thiserror",
"url",
]
[[package]]
name = "object"
version = "0.32.2"
@ -2873,7 +2893,7 @@ dependencies = [
"http 0.2.12",
"itertools 0.10.5",
"log",
"oauth2",
"oauth2 4.4.2",
"p256",
"p384",
"rand",

View File

@ -127,7 +127,7 @@
p256 = "0.13"
rcgen = { version = "0.13.1", features = ["x509-parser"] }
openidconnect = { version = "3.5", features = ["accept-rfc3339-timestamps"] }
oauth2 = "4.4"
oauth2 = "5.0.0-alpha.4"
# MQTT
rumqttc = { version = "0.24", features = ["url"] }

View File

@ -3,10 +3,10 @@ use std::str::FromStr;
use anyhow::{Context, Result};
use chrono::Duration;
use oauth2::basic::BasicClient;
use oauth2::reqwest::async_http_client;
use oauth2::reqwest;
use oauth2::{
AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, PkceCodeChallenge,
RedirectUrl, Scope, TokenResponse, TokenUrl,
AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EndpointNotSet,
EndpointSet, PkceCodeChallenge, RedirectUrl, Scope, TokenResponse, TokenUrl,
};
use reqwest::header::AUTHORIZATION;
use serde::{Deserialize, Serialize};
@ -17,6 +17,8 @@ use crate::config;
use crate::helpers::errors::PrintFullError;
use crate::storage::{get_async_redis_conn, redis_key};
type Client = BasicClient<EndpointSet, EndpointNotSet, EndpointNotSet, EndpointNotSet, EndpointSet>;
#[derive(Deserialize)]
struct ClerkUserinfo {
pub email: String,
@ -81,29 +83,31 @@ pub async fn callback_handler(args: CallbackArgs) -> Result<impl Reply, Rejectio
))
}
fn get_client() -> Result<BasicClient> {
fn get_client() -> Result<Client> {
let conf = config::get();
if conf.user_authentication.enabled != "oauth2" {
return Err(anyhow!("OAuth2 is not enabled"));
}
let client = BasicClient::new(
ClientId::new(conf.user_authentication.oauth2.client_id.clone()),
Some(ClientSecret::new(
conf.user_authentication.oauth2.client_secret.clone(),
)),
AuthUrl::new(conf.user_authentication.oauth2.auth_url.clone())?,
Some(TokenUrl::new(
conf.user_authentication.oauth2.token_url.clone(),
)?),
)
let client = BasicClient::new(ClientId::new(
conf.user_authentication.oauth2.client_id.clone(),
))
.set_client_secret(ClientSecret::new(
conf.user_authentication.oauth2.client_secret.clone(),
))
.set_auth_uri(AuthUrl::new(
conf.user_authentication.oauth2.auth_url.clone(),
)?)
.set_token_uri(TokenUrl::new(
conf.user_authentication.oauth2.token_url.clone(),
)?)
.set_redirect_uri(RedirectUrl::new(
conf.user_authentication.oauth2.redirect_url.clone(),
)?)
.set_auth_type(match conf.user_authentication.oauth2.provider.as_ref() {
"clerk" => AuthType::RequestBody, // clerk does not support BasicAuth
_ => AuthType::BasicAuth, // default oauth2 crate value
_ => AuthType::BasicAuth, // default oauth2 crate value
});
Ok(client)
@ -114,10 +118,14 @@ pub async fn get_user(code: &str, state: &str) -> Result<User> {
let verifier = get_verifier(&state).await?;
let client = get_client()?;
let http_client = reqwest::ClientBuilder::new()
.redirect(reqwest::redirect::Policy::none())
.build()?;
let token = match client
.exchange_code(AuthorizationCode::new(code.to_string()))
.set_pkce_verifier(verifier)
.request_async(async_http_client)
.request_async(&http_client)
.await
{
Ok(v) => v,