feat: add refresh cookie reader
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
use platform_auth::{JwtConfig, JwtError};
|
||||
use std::{error::Error, fmt};
|
||||
|
||||
use platform_auth::{
|
||||
JwtConfig, JwtError, RefreshCookieConfig, RefreshCookieError, RefreshCookieSameSite,
|
||||
};
|
||||
|
||||
use crate::config::AppConfig;
|
||||
|
||||
@@ -9,23 +13,69 @@ pub struct AppState {
|
||||
#[allow(dead_code)]
|
||||
pub config: AppConfig,
|
||||
auth_jwt_config: JwtConfig,
|
||||
refresh_cookie_config: RefreshCookieConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AppStateInitError {
|
||||
Jwt(JwtError),
|
||||
RefreshCookie(RefreshCookieError),
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(config: AppConfig) -> Result<Self, JwtError> {
|
||||
pub fn new(config: AppConfig) -> Result<Self, AppStateInitError> {
|
||||
let auth_jwt_config = JwtConfig::new(
|
||||
config.jwt_issuer.clone(),
|
||||
config.jwt_secret.clone(),
|
||||
config.jwt_access_token_ttl_seconds,
|
||||
)?;
|
||||
let refresh_cookie_same_site =
|
||||
RefreshCookieSameSite::parse(&config.refresh_cookie_same_site).ok_or(
|
||||
RefreshCookieError::InvalidConfig("refresh cookie SameSite 取值非法"),
|
||||
)?;
|
||||
let refresh_cookie_config = RefreshCookieConfig::new(
|
||||
config.refresh_cookie_name.clone(),
|
||||
config.refresh_cookie_path.clone(),
|
||||
config.refresh_cookie_secure,
|
||||
refresh_cookie_same_site,
|
||||
config.refresh_session_ttl_days,
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
config,
|
||||
auth_jwt_config,
|
||||
refresh_cookie_config,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn auth_jwt_config(&self) -> &JwtConfig {
|
||||
&self.auth_jwt_config
|
||||
}
|
||||
|
||||
pub fn refresh_cookie_config(&self) -> &RefreshCookieConfig {
|
||||
&self.refresh_cookie_config
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for AppStateInitError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::Jwt(error) => write!(f, "{error}"),
|
||||
Self::RefreshCookie(error) => write!(f, "{error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for AppStateInitError {}
|
||||
|
||||
impl From<JwtError> for AppStateInitError {
|
||||
fn from(value: JwtError) -> Self {
|
||||
Self::Jwt(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RefreshCookieError> for AppStateInitError {
|
||||
fn from(value: RefreshCookieError) -> Self {
|
||||
Self::RefreshCookie(value)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user