Skip to main content

OAuth2Config

Struct OAuth2Config 

Source
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: bool

PKCE 是否启用(默认 false)

§device_auth_url: Option<String>

device_code 流程的设备授权端点(可选)

Implementations§

Source§

impl OAuth2Config

Source

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: 客户端 ID
  • client_secret: 客户端密钥
  • redirect_url: 回调 URL
  • auth_url: 授权服务器 authorize 端点
  • token_url: 授权服务器 token 端点
Source

pub fn with_user_url(self, user_url: impl Into<String>) -> OAuth2Config

设置用户信息端点

Source

pub fn with_scopes(self, scopes: Vec<String>) -> OAuth2Config

设置 scopes 列表(覆盖原有)

Source

pub fn with_scope(self, scope: impl Into<String>) -> OAuth2Config

追加单个 scope

Source

pub fn with_extra_params(self, params: Vec<(String, String)>) -> OAuth2Config

设置额外参数列表(覆盖原有)

Source

pub fn with_extra_param( self, key: impl Into<String>, value: impl Into<String>, ) -> OAuth2Config

追加单个额外参数

Source

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 交换时提交。

Source

pub fn with_device_auth_url(self, url: impl Into<String>) -> OAuth2Config

设置 device_code 流程的设备授权端点

Source

pub fn generate_state() -> String

自动生成 state 参数 — 使用 rand::rngs::OsRng 生成 16 字节随机数 → 32 字符 hex

满足 spec 4.3.3 ≥32 字符密码学安全要求。

Source

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
Source

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

Source§

fn clone(&self) -> OAuth2Config

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for OAuth2Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more