Skip to content

About

An OAuth 2.1 authorization server for Laravel Rust: MCP authorization, consent and access tokens, on oauth-provider

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

oauth-provider-laravel

An OAuth 2.1 authorization server for Laravel Rust applications, so Claude, ChatGPT, Cursor, VS Code, Codex and other MCP clients can connect to your application's MCP server, or any API it serves, with a sign-in and a consent page.

It's the Laravel package for oauth-provider, which implements the MCP authorization spec. Think of it as Laravel Passport plus laravel/mcp's Mcp::oauthRoutes(), for Laravel Rust. Your application keeps its own users, sign-in and database.

DocSpring maintains it. It's a community package, not an official Laravel one.

What you get

  • The endpoints MCP clients look for: authorization server metadata (RFC 8414), protected resource metadata (RFC 9728), dynamic client registration (RFC 7591), client ID metadata documents, token, revocation (RFC 7009) and introspection (RFC 7662).
  • The authorization code flow with PKCE S256 only, iss in every authorization response (RFC 9207), and tokens bound to the resource they were issued for (RFC 8707).
  • Refresh tokens that rotate on every use. A refresh token or code used twice revokes the whole connection.
  • A consent page on your application's own sign-in, session and CSRF protection, as a view you can override. It says whether the app is a known one (Claude, ChatGPT, VS Code and others), unreviewed, or borrowing a known app's name.
  • RequireToken, middleware that answers a request without a live token with the 401 challenge MCP clients discover the server from, and a 403 insufficient_scope challenge for step-up authorization.
  • OAuthTokens, a request guard, so Auth<User> and Authenticated<User> work in handlers and MCP tools.
  • Storage on your application's database, through Laravel's own connection: PostgreSQL, MySQL, MariaDB or SQLite. Tokens, codes and client secrets are stored as SHA-256 digests, never in plain text.
  • oauth:prune, scheduled daily, and OAuth::connections / disconnect for a "connected apps" settings page.

Installing

cargo add oauth-provider-laravel --features postgres   # or mysql, or sqlite

Turn on the feature for your application's database: postgres, mysql (MySQL and MariaDB) or sqlite. It needs one.

Register the service provider in src/app.rs, with the user provider your session guard uses:

use laravel::model;
use oauth_provider_laravel::OAuthServiceProvider;

use crate::models::User;

Application::configure()
    .with_providers(|providers| {
        providers.add(OAuthServiceProvider::<model::Users<User>>::new());
    })
    .with_auth(|auth, _| {
        auth.session::<model::Users<User>>();
    })

Then create the tables:

laravel migrate

That's all the configuration a new application needs: the issuer is APP_URL, and the protected resource is the MCP server at <APP_URL>/mcp.

examples/mcp-server is a whole application set up this way, with a sign-in page and an MCP server that Claude Code can connect to.

Protecting your MCP server

Put RequireToken and the OAuthTokens guard on the route, RequireToken first:

use laravel::auth::Authenticate;
use laravel::model;
use oauth_provider_laravel::{OAuthTokens, RequireToken};

type Tokens = OAuthTokens<model::Users<User>>;

route
    .middleware(RequireToken::new())
    .middleware(Authenticate::via::<Tokens>())
    .group(|route| {
        route.post("/mcp", mcp::handle);
    });

A handler gets the user as on any other route, and the token's grant with Grant:

use oauth_provider_laravel::Grant;

pub async fn handle(Authenticated(user): Authenticated<User>, grant: Grant) -> laravel::Result<Json<Value>> {
    grant.require(&["mcp:write"])?; // a 403 whose challenge asks the client for the scope
    // ...
}

RequireToken::new().scopes(&["admin"]) refuses tokens without a scope for a whole group.

The consent page

GET /oauth/authorize and POST /oauth/authorize are in the web group, behind Authenticate. A person who isn't signed in goes to your login page (#[laravel::auth::page(login)]) and comes back to the consent page afterwards, as with any other protected page. Someone without an account can sign up on the way, as long as your sign-up sends them back with Redirect::intended.

The page is the view oauth::authorize. To restyle it, copy resources/views/authorize.html to resources/views/vendor/oauth/authorize.html in your application. It gets client_name, client_id, about (who the app is and where approving sends the person), warning (set when the app is unreviewed or borrows a known app's name), scopes, user, service_name and hidden, the fields the form must post back.

Configuration

Variable Default What it sets
OAUTH_ISSUER APP_URL The authorization server's URL
OAUTH_RESOURCES <issuer>/mcp The protected resources, comma-separated
OAUTH_SCOPES mcp The scopes clients may ask for; the first is the default
OAUTH_SERVICE_NAME APP_NAME Your service's name on the consent page
OAUTH_TOKEN_PREFIX acme_ gives acme_at_…, acme_rt_… and acme_cs_…, for secret scanners
OAUTH_DYNAMIC_REGISTRATION true Whether clients may register themselves
OAUTH_METADATA_DOCUMENTS true Whether clients may identify themselves by a metadata document
OAUTH_DB_CONNECTION the default The connection that holds the OAuth tables
OAUTH_ALLOW_HTTP false Allow http:// URLs besides localhost, for a development server

For anything else, edit oauth-provider's Config in Rust:

OAuthServiceProvider::<model::Users<User>>::new().configure(|config| {
    config.with_lifetimes(oauth_provider::Lifetimes {
        access: chrono::Duration::minutes(15),
        ..Default::default()
    })
})

Connected apps

Inject OAuth to show a person the apps they've connected and let them disconnect one:

use oauth_provider_laravel::{OAuth, subject};

pub async fn index(oauth: Inject<OAuth>, Authenticated(user): Authenticated<User>) -> laravel::Result<Page> {
    let connections = oauth.connections(&subject(&user)).await?;
    // ...
}

Call oauth.disconnect_all(&subject(&user)) when a person changes their password or closes their account.

Rate limits

Add your own. Anyone can start an authorization with any client metadata document URL, so limit /oauth/authorize by address, and count refused token requests by address.

Testing your application

oauth-provider-laravel's own tests run the whole flow through a test application: discovery, registration, consent, the code exchange, a protected route, refresh-token rotation and disconnection (tests/flow.rs). They're a good starting point for yours. The storage passes oauth-provider's conformance suite on PostgreSQL, MySQL and SQLite, including the tests where requests race to spend one code or refresh token.

Status and versioning

This is 0.1, for Laravel Rust 0.1. Both are pre-1.0: a minor version (0.2) may break the API, a patch version won't. Every change is in the changelog. The minimum supported Rust version is 1.99, as for Laravel Rust.

Security

Please report vulnerabilities privately: see SECURITY.md.

Licence

Apache-2.0. See LICENSE.

About

An OAuth 2.1 authorization server for Laravel Rust: MCP authorization, consent and access tokens, on oauth-provider

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages