pub struct GenericOAuth2Provider { /* private fields */ }Expand description
通用 OAuth2 提供商 — 对齐 Laravel Socialite GenericProvider
接收 OAuth2Config 和 OAuth2HttpTransport,支持任意标准 OAuth2 提供商。
§工作流程
GenericOAuth2Provider::redirect_url:构造授权 URL,引导用户跳转到授权服务器- 授权服务器回调,携带
code和state GenericOAuth2Provider::user_from_token:- POST
token_url换取access_token(grant_type=authorization_code) - 如果配置了
user_url,POST 用户信息端点获取用户资料 - 返回
SocialiteUser
- POST
§用法
ⓘ
use std::sync::Arc;
use sz_rust_auth_facade::oauth::{
GenericOAuth2Provider, MemoryOAuth2HttpTransport, OAuth2Config, OAuth2Provider,
};
let config = OAuth2Config::new(
"client_id",
"client_secret",
"https://example.com/callback",
"https://provider.com/oauth2.0/authorize",
"https://provider.com/oauth2.0/token",
)
.with_user_url("https://provider.com/user/info");
let transport = Arc::new(MemoryOAuth2HttpTransport::new());
let provider = GenericOAuth2Provider::new(config, transport);
let url = provider.redirect_url("random_state");
let user = provider.user_from_token("auth_code").unwrap();Implementations§
Source§impl GenericOAuth2Provider
impl GenericOAuth2Provider
Sourcepub fn new(
config: OAuth2Config,
transport: Arc<dyn OAuth2HttpTransport>,
) -> GenericOAuth2Provider
pub fn new( config: OAuth2Config, transport: Arc<dyn OAuth2HttpTransport>, ) -> GenericOAuth2Provider
Sourcepub fn with_audit_logger(
self,
logger: Arc<dyn OAuth2AuditLogger>,
) -> GenericOAuth2Provider
pub fn with_audit_logger( self, logger: Arc<dyn OAuth2AuditLogger>, ) -> GenericOAuth2Provider
注入审计日志
Sourcepub fn refresh_token(
&self,
refresh_token: &str,
) -> Result<TokenResponse, OAuth2Error>
pub fn refresh_token( &self, refresh_token: &str, ) -> Result<TokenResponse, OAuth2Error>
用 refresh_token 换取新的 access_token
POST token_url,body 为 JSON:
{
"grant_type": "refresh_token",
"refresh_token": "<refresh_token>",
"client_id": "<client_id>",
"client_secret": "<client_secret>"
}Source§impl GenericOAuth2Provider
impl GenericOAuth2Provider
Sourcepub fn redirect_url_with_pkce(&self, state: &str, pkce: &PkceParams) -> String
pub fn redirect_url_with_pkce(&self, state: &str, pkce: &PkceParams) -> String
构造授权 URL(带 PKCE 参数)
当配置了 PKCE 参数时,授权 URL 追加 code_challenge 和 code_challenge_method=S256。
Sourcepub fn user_from_token_with_pkce(
&self,
code: &str,
code_verifier: Option<&str>,
) -> Result<SocialiteUser, OAuth2Error>
pub fn user_from_token_with_pkce( &self, code: &str, code_verifier: Option<&str>, ) -> Result<SocialiteUser, OAuth2Error>
用授权码换取用户信息(可选 PKCE code_verifier)
当 code_verifier 为 Some 时,token 交换 POST body 追加 code_verifier 字段。
Trait Implementations§
Source§impl OAuth2Provider for GenericOAuth2Provider
impl OAuth2Provider for GenericOAuth2Provider
Source§fn redirect_url(&self, state: &str) -> String
fn redirect_url(&self, state: &str) -> String
生成授权 URL(对齐
Socialite::driver('qq')->redirect()) Read moreSource§fn user_from_token(&self, code: &str) -> Result<SocialiteUser, OAuth2Error>
fn user_from_token(&self, code: &str) -> Result<SocialiteUser, OAuth2Error>
用授权码换取访问令牌并获取用户信息(对齐
Socialite::driver('qq')->user()) Read moreAuto Trait Implementations§
impl !RefUnwindSafe for GenericOAuth2Provider
impl !UnwindSafe for GenericOAuth2Provider
impl Freeze for GenericOAuth2Provider
impl Send for GenericOAuth2Provider
impl Sync for GenericOAuth2Provider
impl Unpin for GenericOAuth2Provider
impl UnsafeUnpin for GenericOAuth2Provider
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
Converts
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> ⓘ
Converts
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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.