pub struct OAuth2Config {
pub client_id: String,
pub client_secret: String,
pub redirect_url: String,
pub auth_url: String,
pub token_url: String,
pub user_url: Option<String>,
pub scopes: Vec<String>,
pub extra_params: Vec<(String, String)>,
pub pkce_enabled: bool,
pub device_auth_url: Option<String>,
}Expand description
OAuth2 配置 — 对齐 Laravel Socialite config/services.php
通过 builder 模式构建,必填字段在 OAuth2Config::new 中提供,
可选字段通过 with_* 链式方法追加。
§PHP 对齐
// config/services.php
'qq' => [
'client_id' => env('QQ_CLIENT_ID'),
'client_secret' => env('QQ_CLIENT_SECRET'),
'redirect' => env('QQ_REDIRECT_URL'),
],§Rust 用法
use sz_rust_auth_facade::oauth::OAuth2Config;
let config = OAuth2Config::new(
"100123456",
"secretabc",
"https://example.com/oauth/qq/callback",
"https://graph.qq.com/oauth2.0/authorize",
"https://graph.qq.com/oauth2.0/token",
)
.with_user_url("https://graph.qq.com/user/get_user_info")
.with_scope("get_user_info");Fields§
§client_id: String客户端 ID(必填)
client_secret: String客户端密钥(必填,Debug 脱敏输出 “***”)
redirect_url: String回调 URL(必填)
auth_url: String授权服务器 authorize 端点(必填,如 https://graph.qq.com/oauth2.0/authorize)
token_url: String授权服务器 token 端点(必填,如 https://graph.qq.com/oauth2.0/token)
user_url: Option<String>资源服务器用户信息端点(可选,如 https://graph.qq.com/user/get_user_info)
scopes: Vec<String>默认 scopes(可选)
extra_params: Vec<(String, String)>额外参数(可选)
pkce_enabled: boolPKCE 是否启用(默认 false)
device_auth_url: Option<String>device_code 流程的设备授权端点(可选)
Implementations§
Source§impl OAuth2Config
impl OAuth2Config
Sourcepub fn new(
client_id: impl Into<String>,
client_secret: impl Into<String>,
redirect_url: impl Into<String>,
auth_url: impl Into<String>,
token_url: impl Into<String>,
) -> OAuth2Config
pub fn new( client_id: impl Into<String>, client_secret: impl Into<String>, redirect_url: impl Into<String>, auth_url: impl Into<String>, token_url: impl Into<String>, ) -> OAuth2Config
创建 OAuth2 配置
§参数
client_id: 客户端 IDclient_secret: 客户端密钥redirect_url: 回调 URLauth_url: 授权服务器 authorize 端点token_url: 授权服务器 token 端点
Sourcepub fn with_user_url(self, user_url: impl Into<String>) -> OAuth2Config
pub fn with_user_url(self, user_url: impl Into<String>) -> OAuth2Config
设置用户信息端点
Sourcepub fn with_scopes(self, scopes: Vec<String>) -> OAuth2Config
pub fn with_scopes(self, scopes: Vec<String>) -> OAuth2Config
设置 scopes 列表(覆盖原有)
Sourcepub fn with_scope(self, scope: impl Into<String>) -> OAuth2Config
pub fn with_scope(self, scope: impl Into<String>) -> OAuth2Config
追加单个 scope
Sourcepub fn with_extra_params(self, params: Vec<(String, String)>) -> OAuth2Config
pub fn with_extra_params(self, params: Vec<(String, String)>) -> OAuth2Config
设置额外参数列表(覆盖原有)
Sourcepub fn with_extra_param(
self,
key: impl Into<String>,
value: impl Into<String>,
) -> OAuth2Config
pub fn with_extra_param( self, key: impl Into<String>, value: impl Into<String>, ) -> OAuth2Config
追加单个额外参数
Sourcepub fn with_pkce(self, enabled: bool) -> OAuth2Config
pub fn with_pkce(self, enabled: bool) -> OAuth2Config
启用/禁用 PKCE(Proof Key for Code Exchange, RFC 7636)
启用后,调用方需通过 OAuth2Config::generate_pkce_pair 生成 PKCE 参数对,
将 code_challenge 追加到授权 URL,将 code_verifier 在 token 交换时提交。
Sourcepub fn with_device_auth_url(self, url: impl Into<String>) -> OAuth2Config
pub fn with_device_auth_url(self, url: impl Into<String>) -> OAuth2Config
设置 device_code 流程的设备授权端点
Sourcepub fn generate_state() -> String
pub fn generate_state() -> String
自动生成 state 参数 — 使用 rand::rngs::OsRng 生成 16 字节随机数 → 32 字符 hex
满足 spec 4.3.3 ≥32 字符密码学安全要求。
Sourcepub fn generate_pkce_pair() -> PkceParams
pub fn generate_pkce_pair() -> PkceParams
自动生成 PKCE 参数对 — code_verifier + code_challenge
- code_verifier:OsRng 生成 32 字节随机 → 64 字符 hex(满足 43-128 字符要求)
- code_challenge:
BASE64URL-NO-PAD(SHA256(code_verifier)) - method:固定
S256
Sourcepub fn validate(&self) -> Result<(), OAuth2Error>
pub fn validate(&self) -> Result<(), OAuth2Error>
校验必填字段
必填字段:client_id / client_secret / redirect_url / auth_url / token_url。
任一为空字符串则返回 OAuth2Error::MissingField。
Trait Implementations§
Source§impl Clone for OAuth2Config
impl Clone for OAuth2Config
Source§fn clone(&self) -> OAuth2Config
fn clone(&self) -> OAuth2Config
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for OAuth2Config
impl RefUnwindSafe for OAuth2Config
impl Send for OAuth2Config
impl Sync for OAuth2Config
impl Unpin for OAuth2Config
impl UnsafeUnpin for OAuth2Config
impl UnwindSafe for OAuth2Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.