Skip to main content

OAuth2Server

Struct OAuth2Server 

Source
pub struct OAuth2Server { /* private fields */ }
Expand description

OAuth2 授权服务器:管理授权码的签发、验证与交换。

内部使用 Mutex<HashMap> 存储授权码,支持:

  • 创建授权码(create_authorization_code
  • 用授权码交换令牌(exchange_code
  • 验证客户端凭据
  • 授权码一次性消费

Implementations§

Source§

impl OAuth2Server

Source

pub fn new(clients: HashMap<String, String>) -> Self

创建授权服务器,注册一组客户端

Source

pub fn empty() -> Self

创建空授权服务器,后续通过 register_client 注册

Source

pub fn register_client( &mut self, client_id: impl Into<String>, client_secret: impl Into<String>, )

注册客户端

Source

pub fn validate_client(&self, client_id: &str, client_secret: &str) -> bool

验证客户端凭据

Source

pub fn has_client(&self, client_id: &str) -> bool

是否已注册客户端

Source

pub fn create_authorization_code( &self, req: &AuthorizationRequest, user_id: i64, ) -> Result<AuthorizationCode, AuthError>

签发授权码

验证授权请求中的 client_id 已注册后,创建一次性授权码。

Source

pub fn exchange_code( &self, req: &TokenRequest, ) -> Result<AuthorizationCode, AuthError>

用授权码交换访问令牌

验证流程:

  1. 授权码存在
  2. 授权码未过期
  3. 授权码未使用(一次性消费)
  4. redirect_uri 与签发时一致
  5. client_id 与签发时一致
Source

pub fn code_count(&self) -> usize

返回当前存储的授权码数量

Source

pub fn cleanup(&self) -> usize

清理已过期或已使用的授权码

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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