pub struct OAuth2Manager { /* private fields */ }Expand description
OAuth2 protocol manager backed by SaTokenDao.
基于 SaTokenDao 的 OAuth2 协议管理器。
Implementations§
Source§impl OAuth2Manager
impl OAuth2Manager
Sourcepub fn new(storage: Arc<dyn SaStorage>) -> Self
pub fn new(storage: Arc<dyn SaStorage>) -> Self
Build from raw storage with default config / key prefix. 从原始存储构建(默认配置与键前缀)。
Sourcepub fn from_dao(dao: Arc<SaTokenDao>) -> Self
pub fn from_dao(dao: Arc<SaTokenDao>) -> Self
Build from an existing Dao (preferred when sharing Manager keys). 从已有 Dao 构建(与 Manager 共享键时推荐)。
Sourcepub fn from_manager(manager: &SaTokenManager) -> Self
pub fn from_manager(manager: &SaTokenManager) -> Self
Align Dao / key prefix with an existing manager. 与已有 manager 对齐 Dao / 键前缀。
Sourcepub fn with_ttl(
self,
code_ttl: i64,
token_ttl: i64,
refresh_token_ttl: i64,
) -> Self
pub fn with_ttl( self, code_ttl: i64, token_ttl: i64, refresh_token_ttl: i64, ) -> Self
Override code / access / refresh TTLs (seconds). 覆盖授权码 / 访问令牌 / 刷新令牌 TTL(秒)。
Sourcepub fn with_require_pkce(self, require: bool) -> Self
pub fn with_require_pkce(self, require: bool) -> Self
Require PKCE for confidential clients as well. 机密客户端也强制要求 PKCE。
Sourcepub fn with_allow_legacy_plain_secret(self, allow: bool) -> Self
pub fn with_allow_legacy_plain_secret(self, allow: bool) -> Self
Allow verifying legacy plaintext secrets stored under the hash field. 允许校验哈希字段中残留的历史明文密钥。
Sourcepub fn with_password_verifier(self, verifier: Arc<dyn PasswordVerifier>) -> Self
pub fn with_password_verifier(self, verifier: Arc<dyn PasswordVerifier>) -> Self
Inject password grant verifier (required for password grant). 注入密码模式校验器(password grant 必需)。
Sourcepub async fn register_client_with_secret(
&self,
client: OAuth2Client,
plain_secret: &str,
) -> SaTokenResult<()>
pub async fn register_client_with_secret( &self, client: OAuth2Client, plain_secret: &str, ) -> SaTokenResult<()>
Register a client after hashing plain_secret (unless public).
注册客户端:对 plain_secret 哈希后落库(公共客户端除外)。
Sourcepub async fn register_client(&self, client: &OAuth2Client) -> SaTokenResult<()>
pub async fn register_client(&self, client: &OAuth2Client) -> SaTokenResult<()>
Compatibility wrapper: hashes client.client_secret when hash is empty.
兼容包装:hash 为空时哈希 client.client_secret。
Sourcepub async fn get_client(&self, client_id: &str) -> SaTokenResult<OAuth2Client>
pub async fn get_client(&self, client_id: &str) -> SaTokenResult<OAuth2Client>
Load a registered client by id. 按 id 加载已注册客户端。
Sourcepub async fn verify_client(
&self,
client_id: &str,
client_secret: &str,
) -> SaTokenResult<bool>
pub async fn verify_client( &self, client_id: &str, client_secret: &str, ) -> SaTokenResult<bool>
Verify client credentials (public clients always succeed). 校验客户端凭据(公共客户端恒成功)。
Build an authorization code (does not persist). 构造授权码(不落库)。
Persist an authorization code with TTL. 以 TTL 持久化授权码。
Atomically consume an authorization code (take_string).
原子消费授权码(take_string)。
Sourcepub async fn exchange_code_for_token(
&self,
code: &str,
client_id: &str,
client_secret: &str,
redirect_uri: &str,
code_verifier: Option<&str>,
) -> SaTokenResult<AccessToken>
pub async fn exchange_code_for_token( &self, code: &str, client_id: &str, client_secret: &str, redirect_uri: &str, code_verifier: Option<&str>, ) -> SaTokenResult<AccessToken>
Exchange authorization code for tokens (with optional PKCE). 用授权码兑换令牌(可选 PKCE)。
Sourcepub async fn generate_access_token(
&self,
client_id: &str,
user_id: &str,
scope: Vec<String>,
) -> SaTokenResult<AccessToken>
pub async fn generate_access_token( &self, client_id: &str, user_id: &str, scope: Vec<String>, ) -> SaTokenResult<AccessToken>
Issue and persist an access + refresh token pair. 签发并持久化访问令牌 + 刷新令牌对。
Sourcepub async fn verify_access_token(
&self,
access_token: &str,
) -> SaTokenResult<OAuth2TokenInfo>
pub async fn verify_access_token( &self, access_token: &str, ) -> SaTokenResult<OAuth2TokenInfo>
Load and validate an access token. 加载并校验访问令牌。
Sourcepub async fn refresh_access_token(
&self,
refresh_token: &str,
client_id: &str,
client_secret: &str,
) -> SaTokenResult<AccessToken>
pub async fn refresh_access_token( &self, refresh_token: &str, client_id: &str, client_secret: &str, ) -> SaTokenResult<AccessToken>
Rotate refresh token atomically (take_string + rewrite on failure).
原子轮换刷新令牌(take_string;失败时回写)。
Sourcepub async fn revoke_token(&self, token: &str) -> SaTokenResult<()>
pub async fn revoke_token(&self, token: &str) -> SaTokenResult<()>
Revoke access and/or refresh token keys (errors propagate). 撤销访问/刷新令牌键(错误上抛)。
Sourcepub fn validate_redirect_uri(
&self,
client: &OAuth2Client,
redirect_uri: &str,
) -> bool
pub fn validate_redirect_uri( &self, client: &OAuth2Client, redirect_uri: &str, ) -> bool
Exact-match redirect URI validation (rejects empty / fragment). 精确匹配重定向 URI(拒绝空串 / fragment)。
Sourcepub fn validate_scope(
&self,
client: &OAuth2Client,
requested_scope: &[String],
) -> bool
pub fn validate_scope( &self, client: &OAuth2Client, requested_scope: &[String], ) -> bool
True when every requested scope is registered on the client. 请求的每个 scope 均已在客户端注册时返回 true。
Sourcepub fn supports_grant_type(client: &OAuth2Client, grant_type: &str) -> bool
pub fn supports_grant_type(client: &OAuth2Client, grant_type: &str) -> bool
True when the client lists the grant type. 客户端声明了该授权类型时返回 true。
Sourcepub async fn password_grant(
&self,
client_id: &str,
client_secret: &str,
username: &str,
password: &str,
scope: Vec<String>,
) -> SaTokenResult<AccessToken>
pub async fn password_grant( &self, client_id: &str, client_secret: &str, username: &str, password: &str, scope: Vec<String>, ) -> SaTokenResult<AccessToken>
Resource-owner password grant (requires injected verifier). 资源所有者密码模式(需注入校验器)。
Sourcepub async fn client_credentials_grant(
&self,
client_id: &str,
client_secret: &str,
scope: Vec<String>,
) -> SaTokenResult<AccessToken>
pub async fn client_credentials_grant( &self, client_id: &str, client_secret: &str, scope: Vec<String>, ) -> SaTokenResult<AccessToken>
Client-credentials grant (confidential clients only). 客户端凭证模式(仅机密客户端)。
Sourcepub async fn issue_token(
&self,
req: TokenIssueRequest,
) -> SaTokenResult<AccessToken>
pub async fn issue_token( &self, req: TokenIssueRequest, ) -> SaTokenResult<AccessToken>
Dispatch token issuance by grant type. 按授权类型分发令牌签发。
Validate client + redirect + scope + PKCE, then generate and store a code. 校验客户端 / 重定向 / scope / PKCE 后生成并存储授权码。