Browse Source

Fix: is_some_and and formatting issues

pull/6388/head
hnolde 1 month ago
parent
commit
bfbedb05d6
  1. 19
      src/api/admin.rs
  2. 5
      src/config.rs
  3. 21
      src/mail.rs

19
src/api/admin.rs

@ -360,10 +360,7 @@ fn oauth2_authorize(_token: AdminToken) -> Result<Redirect, Error> {
let state = crate::crypto::encode_random_bytes::<32>(BASE64URL_NOPAD); let state = crate::crypto::encode_random_bytes::<32>(BASE64URL_NOPAD);
// Store state with expiration (10 minutes from now) // Store state with expiration (10 minutes from now)
let expiration = SystemTime::now() let expiration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + 600;
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() + 600;
OAUTH2_STATES.write().unwrap().insert(state.clone(), expiration); OAUTH2_STATES.write().unwrap().insert(state.clone(), expiration);
@ -411,7 +408,7 @@ async fn oauth2_callback(params: OAuth2CallbackParams) -> Result<Html<String>, E
let valid_state = { let valid_state = {
let states = OAUTH2_STATES.read().unwrap(); let states = OAUTH2_STATES.read().unwrap();
let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(); let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs();
states.get(&state).map_or(false, |&exp| exp > now) states.get(&state).is_some_and(|&exp| exp > now)
}; };
if !valid_state { if !valid_state {
@ -449,16 +446,12 @@ async fn oauth2_callback(params: OAuth2CallbackParams) -> Result<Html<String>, E
return Err(Error::new("OAuth2 Token Exchange Failed", format!("HTTP {}: {}", status, body))); return Err(Error::new("OAuth2 Token Exchange Failed", format!("HTTP {}: {}", status, body)));
} }
let token_response: Value = response let token_response: Value =
.json() response.json().await.map_err(|e| Error::new("OAuth2 Token Parse Error", e.to_string()))?;
.await
.map_err(|e| Error::new("OAuth2 Token Parse Error", e.to_string()))?;
// Extract refresh_token from response // Extract refresh_token from response
let refresh_token = token_response let refresh_token =
.get("refresh_token") token_response.get("refresh_token").and_then(|v| v.as_str()).ok_or("No refresh_token in response")?;
.and_then(|v| v.as_str())
.ok_or("No refresh_token in response")?;
// Save refresh_token to configuration // Save refresh_token to configuration
let config_builder: ConfigBuilder = serde_json::from_value(json!({ let config_builder: ConfigBuilder = serde_json::from_value(json!({

5
src/config.rs

@ -1146,10 +1146,7 @@ fn validate_config(cfg: &ConfigItems) -> Result<(), Error> {
} }
// OAuth2 validation - triggered when SMTP Auth mechanism includes xoauth2 // OAuth2 validation - triggered when SMTP Auth mechanism includes xoauth2
let uses_xoauth2 = cfg.smtp_auth_mechanism let uses_xoauth2 = cfg.smtp_auth_mechanism.as_ref().map(|m| m.to_lowercase().contains("xoauth2")).unwrap_or(false);
.as_ref()
.map(|m| m.to_lowercase().contains("xoauth2"))
.unwrap_or(false);
if uses_xoauth2 { if uses_xoauth2 {
if cfg.smtp_oauth2_client_id.is_none() { if cfg.smtp_oauth2_client_id.is_none() {

21
src/mail.rs

@ -1,9 +1,9 @@
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use percent_encoding::{percent_encode, NON_ALPHANUMERIC}; use percent_encoding::{percent_encode, NON_ALPHANUMERIC};
use std::{env::consts::EXE_SUFFIX, str::FromStr}; use serde::{Deserialize, Serialize};
use std::sync::{LazyLock, RwLock}; use std::sync::{LazyLock, RwLock};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize}; use std::{env::consts::EXE_SUFFIX, str::FromStr};
use lettre::{ use lettre::{
message::{Attachment, Body, Mailbox, Message, MultiPart, SinglePart}, message::{Attachment, Body, Mailbox, Message, MultiPart, SinglePart},
@ -68,17 +68,12 @@ pub async fn refresh_oauth2_token() -> Result<OAuth2Token, Error> {
return Err(Error::new("OAuth2 Token Refresh Failed", format!("HTTP {status}: {body}"))); return Err(Error::new("OAuth2 Token Refresh Failed", format!("HTTP {status}: {body}")));
} }
let token_response: TokenRefreshResponse = response let token_response: TokenRefreshResponse =
.json() response.json().await.map_err(|e| Error::new("OAuth2 Token Parse Error", e.to_string()))?;
.await
.map_err(|e| Error::new("OAuth2 Token Parse Error", e.to_string()))?; let expires_at = token_response
.expires_in
let expires_at = token_response.expires_in.map(|expires_in| { .map(|expires_in| SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() + expires_in);
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() + expires_in
});
Ok(OAuth2Token { Ok(OAuth2Token {
access_token: token_response.access_token, access_token: token_response.access_token,

Loading…
Cancel
Save