oxide-auth

repository·master·Indexed 21 days ago

https://github.com/197g/oxide-auth

A modular OAuth2 server library for Rust (v0.6.1) featuring a trait-based interface for managing tokens. It is designed to be server-agnostic with configurable and pluggable backends, providing official integration crates for web frameworks including actix-web, axum, rocket, poem, iron, and rouille, as well as async/await and redis support.

Tokens
33.4K
Snippets
104
Records
134
Agent score
72%

What's inside oxide-auth

  1. Overview of oxide-auth

    master
    oxide-auth is an OAuth2 server library designed to manage OAuth2 tokens on a server. It is built to be highly extensible and agnostic of the specific web server being used. The core library provides a trait-based interface that allows you to plug in different front-end web servers and back-end storage systems easily.
  2. Integrate oxide-auth with web frameworks and databases

    master

    While the core oxide-auth crate is server-agnostic, several extension crates provide idiomatic wrappers for popular Rust web frameworks and database backends. These extensions implement the necessary oxide-auth traits for request types, errors, and responses specific to those frameworks.

    Available Integrations

    TargetCrateNotes
    actixoxide-auth-actix
    async wrappersoxide-auth-async
    redisoxide-auth-db
    rocketoxide-auth-rocketRequires nightly
    rouilleoxide-auth-rouille
    ironoxide-auth-iron
    poemoxide-auth-poem
    axumoxide-auth-axum(Available in repository)
  3. How OAuth2 scopes and access control work

    master

    In oxide-auth, a Scope is a set of space-separated tokens. Scopes follow a partial ordering logic based on conjunction: a scope is considered a subset of another if all its tokens are present in the other scope.

    Access Control Logic

    • Resource Requirement: A resource requires a specific Scope (the resource_scope).
    • Grant Privileges: A user holds a Scope (the grant_scope).
    • Rule: Access is granted if the resource_scope is a subset of the grant_scope (i.e., resource_scope <= grant_scope).

    Comparison Summary

    OperationMeaningLogic
    resource_scope.allow_access(&grant_scope)Can the resource allow this grant?resource_scope <= grant_scope
    grant_scope.priviledged_to(&resource_scope)Does the grant have enough privilege for the resource?resource_scope <= grant_scope

    Note that if scopes contain incomparable tokens (e.g., A B vs A C), they are not considered subsets of each other.

    ```rust
    use oxide_auth::primitives::scope::Scope;
    
    let grant_scope    = "some_scope other_scope".parse::<Scope>().unwrap();
    let resource_scope = "some_scope".parse::<Scope>().unwrap();
    let uncomparable   = "some_scope third_scope".parse::<Scope>().unwrap();
    
    // Access granted because resource_scope is a subset of grant_scope
    assert!(resource_scope.allow_access(&grant_scope));
    assert!(grant_scope.priviledged_to(&resource_scope));
    
    // Access denied because tokens are incomparable
    assert!(!uncomparable.allow_access(&grant_scope));
    ```埋
  4. Untitled record

    master

    DBRegistrar is a database-backed implementation of the Registrar trait from oxide-auth. It manages the storage, retrieval, and validation of OAuth2 clients using a DataSource (repository).

    Key features:

    • Client Registration: Persists Client objects by encoding them with a PasswordPolicy (defaults to Argon2).
    • Password Policy Management: Allows customizing how client secrets are encoded using set_password_policy.
    • OAuth2 Compliance: Implements bound_redirect for URI validation, negotiate for pre-grant generation, and check for client authentication.

    To use DBRegistrar, you must provide a connection URL, a maximum pool size, and a client prefix for your database keys.

    use oxide_auth::primitives::registrar::{Client, Registrar};
    // Note: DBRegistrar requires a DataSource implementation (e.g., Redis via the oxide-auth-db crate)
    let mut registrar = DBRegistrar::new(
        "redis://localhost/0".to_string(), 
        32, 
        "client:".to_string()
    ).unwrap();
  5. Configure RegisteredUrl matching strategies

    master

    When registering redirect URIs, you can choose between different matching behaviors using the RegisteredUrl enum:

    1. Exact(ExactUrl): Requires a literal, character-for-character match of the string. Useful for preventing injection of unexpected query parameters.
    2. Semantic(Url): Uses standard URL semantic matching (e.g., normalization).
    3. IgnorePortOnLocalhost(IgnoreLocalPortUrl): Matches the URL semantically but ignores the port number if the host is localhost. This follows IETF recommendations for local development.
    pub enum RegisteredUrl {
        Exact(ExactUrl),
        Semantic(Url),
        IgnorePortOnLocalhost(IgnoreLocalPortUrl),
    }