RUser

Struct RUser 

Source
pub struct RUser {
    pub id: String,
    pub token: String,
}
Expand description

Authenticated User Information | 已认证用户信息

RUser is the core concept of r-token. It implements actix-web’s FromRequest trait, enabling “parameter-as-authentication” by using it directly as a handler parameter.

RUser 是 r-token 最核心的概念,它实现了 actix-webFromRequest trait, 可以作为 Handler 的参数直接使用,实现“参数即鉴权“的效果。

§How It Works | 工作原理

When you declare RUser as a handler parameter, actix-web automatically:

当你在 Handler 参数中声明 RUser 时,actix-web 会自动:

  1. Extracts the Token from the Authorization header | 从请求的 Authorization header 中提取 Token
  2. Validates the Token through RTokenManager | 通过 RTokenManager 验证 Token 的有效性
  3. If valid, creates an RUser instance and passes it to your handler | 如果验证通过,创建 RUser 实例并传递给你的 Handler
  4. If invalid, returns 401 Unauthorized without calling the handler | 如果验证失败,直接返回 401 Unauthorized,Handler 不会被调用

§Zero-Intrusion Design | 零侵入式设计

You don’t need any if/else checks in your business code to verify if a user is logged in. If a parameter has RUser, the user is guaranteed to be authenticated!

你不需要在业务代码中写任何 if/else 来检查用户是否登录, 只要参数里有 RUser,就保证用户一定是已登录的!

§Example | 示例

use actix_web::{get, HttpResponse};
use r_token::RUser;

#[get("/protected")]
async fn protected_route(user: RUser) -> impl actix_web::Responder {
    // If we get here, user is guaranteed to be valid! | 能进到这里,user 一定是合法的!
    HttpResponse::Ok().body(format!("Welcome, user {}", user.id))
}

Fields§

§id: String

User ID | 用户 ID

Corresponds to the user identifier passed during login | 对应登录时传入的用户标识符

§token: String

User’s Token | 用户的 Token

The Token string extracted from the Authorization header | 从 Authorization header 中提取的 Token 字符串

Trait Implementations§

Source§

impl Debug for RUser

Source§

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

Formats the value using the given formatter. Read more
Source§

impl FromRequest for RUser

FromRequest Trait Implementation | FromRequest trait 实现

This is the key to r-token’s “parameter-as-authentication” feature.

这是 r-token 实现“参数即鉴权“的关键。

§Execution Flow | 执行流程

When actix-web receives a request and finds a handler needs an RUser parameter, it automatically executes this logic:

actix-web 收到请求并发现 Handler 需要 RUser 参数时,会自动执行这里的逻辑:

  1. Get Token Manager | 获取 Token 管理器: Extract RTokenManager from app_data | 从 app_data 中提取 RTokenManager
  2. Extract Token | 提取 Token: Get Token from Authorization header (supports Bearer prefix) | 从 Authorization header 中获取 Token(支持 Bearer 前缀)
  3. Validate Token | 验证 Token: Check if Token exists in manager’s storage | 检查 Token 是否存在于管理器的存储中
  4. Return Result | 返回结果:
    • Success → Create RUser instance, handler executes normally | 成功 → 创建 RUser 实例,Handler 正常执行
    • Failure → Return 401 Unauthorized, handler is not called | 失败 → 返回 401 Unauthorized,Handler 不会被调用
§Error Handling | 错误处理
  • 500 Internal Server Error: Token manager not injected into app_data | Token 管理器未注入到 app_data
  • 401 Unauthorized: Token missing or invalid | Token 缺失或无效
Source§

type Error = Error

The associated error which can be returned.
Source§

type Future = Ready<Result<RUser, <RUser as FromRequest>::Error>>

Future that resolves to a Self. Read more
Source§

fn from_request(req: &HttpRequest, _payload: &mut Payload) -> Self::Future

Create a Self from request parts asynchronously.
Source§

fn extract(req: &HttpRequest) -> Self::Future

Create a Self from request head asynchronously. Read more

Auto Trait Implementations§

§

impl Freeze for RUser

§

impl RefUnwindSafe for RUser

§

impl Send for RUser

§

impl Sync for RUser

§

impl Unpin for RUser

§

impl UnwindSafe for RUser

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

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> 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

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