/
codemaster
/
a-pass-oauth
Обзор
Документация
Войти
/
codemaster
/
a-pass-oauth
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/models.rs
250 строк
8 KB
mich-master
2512
25 дек 2025, 16:06
25 дек 2025, 16:06
d7d10d9
Код
Авторство
О чём код?
use serde::{Deserialize, Serialize}; use chrono::{DateTime, Utc}; use sha2::{Sha256, Digest}; pub trait ToUriString { fn to_uri_string(&self) -> String { String::default() } } // OAuth 2.1 Models #[derive(Debug)] #[derive(Serialize, Deserialize)] // pub struct AuthorizationParams<T> // where T: Serialize + Deserialize<'static> + ToUriString pub struct AuthorizationParams { pub response_type: String, // Must be "code" pub client_id: String, pub redirect_uri: String, pub code_challenge: String, // REQUIRED in OAuth 2.1 pub code_challenge_method: String, // "S256" or "plain" pub state: String, // REQUIRED in OAuth 2.1 pub scope: Option<String>, pub nonce: Option<String>, // For OpenID Connect // pub prompt: Option<String>, // pub extra: T, } impl From<ConsentParams> for AuthorizationParams { fn from(params: ConsentParams) -> Self { Self { response_type: params.response_type, client_id: params.client_id, redirect_uri: params.redirect_uri, code_challenge: params.code_challenge, code_challenge_method: params.code_challenge_method, state: params.state, scope: params.scope, nonce: params.nonce, } } } impl ToUriString for AuthorizationParams { fn to_uri_string(&self) -> String { let mut params = vec![ format!( "response_type={}&client_id={}&redirect_uri={}&code_challenge={}&code_challenge_method={}&state={}", self.response_type, self.client_id, urlencoding::encode(self.redirect_uri.as_str()), self.code_challenge, self.code_challenge_method, self.state, ), ]; if let Some(scope) = &self.scope { params.push(format!("scope={}", urlencoding::encode(scope))); } format!("{}", params.join("&")) } } impl AuthorizationParams { pub fn auth_hash(&self) -> String { let mut hasher = Sha256::new(); hasher.update(self.client_id.as_str()); hasher.update(self.code_challenge.as_str()); hasher.update(self.state.as_str()); format!("{:x}",hasher.finalize()) } } #[derive(Debug)] #[derive(Serialize, Deserialize)] pub struct ConsentParams { pub response_type: String, // Must be "code" pub client_id: String, pub redirect_uri: String, pub code_challenge: String, // REQUIRED in OAuth 2.1 pub code_challenge_method: String, // "S256" or "plain" pub state: String, // REQUIRED in OAuth 2.1 pub scope: Option<String>, pub nonce: Option<String>, // For OpenID Connect pub action: String, } // #[derive(Debug, Deserialize)] // pub struct ConsentDenyRequest { // pub client_id: String, // pub redirect_uri: String, // pub state: String, // // pub session_id: String, // } #[derive(Debug)] #[derive(Clone)] #[derive(Serialize, Deserialize)] pub struct AuthorizationCode { pub code: String, pub client_id: String, pub redirect_uri: String, pub user_id: String, pub scope: Option<String>, pub code_challenge: String, pub code_challenge_method: String, pub nonce: Option<String>, pub expires_at: DateTime<Utc>, pub created_at: DateTime<Utc>, } #[derive(Debug, Serialize, Deserialize)] pub struct TokenRequest { pub grant_type: String, // "authorization_code" or "refresh_token" pub code: Option<String>, pub refresh_token: Option<String>, pub redirect_uri: String, pub client_id: String, pub client_secret: Option<String>, // pub client_secret: Option<Secret<String>>, pub code_verifier: Option<String>, // REQUIRED for authorization_code } #[derive(Debug, Serialize, Deserialize)] pub struct TokenResponse { pub access_token: String, pub token_type: String, // Always "Bearer" pub expires_in: i64, pub refresh_token: Option<String>, // Single-use in OAuth 2.1 pub scope: Option<String>, pub id_token: Option<String>, // OpenID Connect } #[derive(Debug)] #[derive(Clone)] #[derive(Serialize, Deserialize)] pub struct RefreshToken { pub token: String, pub client_id: String, pub user_id: String, pub scope: Option<String>, pub expires_at: DateTime<Utc>, pub used: bool, // Single-use in OAuth 2.1 pub created_at: DateTime<Utc>, } // OpenID Connect Models #[derive(Debug, Serialize, Deserialize)] pub struct IdTokenClaims { // Required claims pub iss: String, // Issuer pub sub: String, // Subject (user ID) pub aud: String, // Audience (client ID) pub exp: i64, // Expiration time pub iat: i64, // Issued at pub auth_time: Option<i64>, // Authentication time // Optional claims pub nonce: Option<String>, pub acr: Option<String>, // Authentication Context Class Reference pub amr: Option<Vec<String>>, // Authentication Methods References pub azp: Option<String>, // Authorized party // Profile claims pub name: Option<String>, pub given_name: Option<String>, pub family_name: Option<String>, pub middle_name: Option<String>, pub nickname: Option<String>, pub preferred_username: Option<String>, pub profile: Option<String>, pub picture: Option<String>, pub website: Option<String>, pub email: Option<String>, pub email_verified: Option<bool>, pub gender: Option<String>, pub birthdate: Option<String>, pub zoneinfo: Option<String>, pub locale: Option<String>, pub phone_number: Option<String>, pub phone_number_verified: Option<bool>, pub address: Option<serde_json::Value>, pub updated_at: Option<i64>, } #[derive(Debug, Serialize, Deserialize)] pub struct UserInfo { pub sub: String, // pub name: Option<String>, // pub given_name: Option<String>, // pub family_name: Option<String>, // pub middle_name: Option<String>, // pub nickname: Option<String>, // pub preferred_username: Option<String>, // pub profile: Option<String>, // pub picture: Option<String>, // pub website: Option<String>, // pub email: Option<String>, // pub email_verified: Option<bool>, // pub gender: Option<String>, // pub birthdate: Option<String>, // pub zoneinfo: Option<String>, // pub locale: Option<String>, // pub phone_number: Option<String>, // pub phone_number_verified: Option<bool>, // pub address: Option<serde_json::Value>, pub provider: String, pub created_at: Option<DateTime<Utc>>, pub updated_at: Option<DateTime<Utc>>, } // User Models // #[derive(Debug)] // #[derive(Clone)] // #[derive(Serialize, Deserialize)] // pub struct UserFull { // // pub id: Uuid, // pub id: String, // pub username: String, // pub email: String, // pub email_verified: bool, // pub password_hash: String, // // pub password_hash: Secret<String>, // pub given_name: Option<String>, // pub family_name: Option<String>, // pub name: Option<String>, // pub nickname: Option<String>, // pub picture: Option<String>, // pub locale: Option<String>, // pub zoneinfo: Option<String>, // pub created_at: DateTime<Utc>, // pub updated_at: DateTime<Utc>, // pub last_login: Option<DateTime<Utc>>, // } #[derive(Debug)] #[derive(Deserialize)] pub struct AuthLoginParams { pub auth: Option<String>, } #[derive(Debug)] #[derive(Clone)] pub struct PkceState { // pub state: String, pub code_verifier: String, }