Grant OAuth Proxy
repository·master·Indexed 26 days ago
https://github.com/simov/grantAn OAuth proxy supporting over 200 providers, designed to simplify OAuth flow integration. Grant provides standardized handlers for Node.js frameworks including Express, Koa, Hapi, and Fastify, as well as serverless environments such as AWS Lambda, Azure Functions, Google Cloud Functions, and Vercel. Version 5.4.24.
What's inside grant
- Grant is an OAuth proxy that supports over 200 providers (including Google, GitHub, Facebook, Slack, and many others). It simplifies the OAuth flow by providing standardized handlers for various HTTP frameworks and serverless environments, acting as a middleware layer to manage authentication.
Configure OAuth credentials in config.json
masterTo use the examples, add your OAuth App credentials (such as Client IDs and Client Secrets) to theconfig.jsonfile in the project root.Configure Grant Connection Routes and Redirect URIs
masterGrant uses specific URL patterns for the OAuth flow.
Login Route: Navigate to
[origin][prefix]/:providerto initiate login. If using static overrides, use[origin][prefix]/:provider/:override?.Callback Route: Grant expects a callback at
[origin][prefix]/:provider/callback.Setting the Redirect URI in your OAuth App: Your OAuth provider must be configured with a Redirect URI following this format:
[origin][prefix]/[provider]/callbackExample: If
originishttp://localhost:3000andprefixis/connect, the redirect URI for Google would behttp://localhost:3000/connect/google/callback.Define a custom OAuth provider
masterTo add a provider not natively supported by Grant, add a new key to your configuration object. You must specify all required configuration keys, including
authorize_url,access_url,oauth(the protocol version),key,secret, andscope.{ "defaults": { "origin": "http://localhost:3000" }, "awesome": { "authorize_url": "https://awesome.com/authorize", "access_url": "https://awesome.com/token", "oauth": 2, "key": "...", "secret": "...", "scope": ["read", "write"] } }Migrate from `path` to `prefix` in Grant v5
masterThe
pathconfiguration key used in Grant v4 for setting a path prefix is deprecated. In Grant v5, use theprefixkey instead. Note that the prefix should typically include the/connectsuffix if you are following standard patterns.{ "defaults": { "origin": "http://localhost:3000", "prefix": "/oauth/connect" } }Set up OAuth App redirect URIs for Grant examples
masterWhen using the Grant examples to test OAuth 2.0 (e.g., Google) or OAuth 1.0a (e.g., Twitter) flows, you must configure your OAuth provider's dashboard with the following redirect URIs:
- For Google:
http://localhost:3000/connect/google/callback - For Twitter:
http://localhost:3000/connect/twitter/callback
- For Google:
Integrate Grant with HTTP Frameworks
masterGrant provides specialized handlers for popular Node.js HTTP frameworks. When using these handlers, you must ensure a session store is configured as Grant relies on sessions for state management.
// Express var express = require('express') var session = require('express-session') var grant = require('grant').express() var app = express() app.use(session({secret: 'grant'})) app.use(grant({/*configuration*/})) // Koa var Koa = require('koa') var session = require('koa-session') var grant = require('grant').koa() var app = new Koa() app.keys = ['grant'] app.use(session(app)) app.use(grant({/*configuration*/})) // Hapi var Hapi = require('hapi') var yar = require('yar') var grant = require('grant').hapi() var server = new Hapi.Server() server.register([ {plugin: yar, options: {cookieOptions: {password: 'grant', isSecure: false}}}, {plugin: grant({/*configuration*/})} ]) // Fastify var fastify = require('fastify') var cookie = require('@fastify/cookie') var session = require('@fastify/session') var grant = require('grant').fastify() fastify() .register(cookie) .register(session, {secret: 'grant', cookie: {secure: false}}) .register(grant({/*configuration*/}))Handle OAuth Subdomain and Sandbox requirements
masterSubdomains
Some providers (like Shopify or Mastodon) require dynamic URLs. Use the
subdomainoption in your provider config to inject values into theauthorize_urlandaccess_url.Sandbox URLs
To use a provider's sandbox environment, override the
request_url,authorize_url, andaccess_urlin your configuration with the sandbox-specific endpoints.Sandbox Redirect URIs
If a provider (like Feedly) restricts the allowed
redirect_uriin sandbox mode, you may need to manually redirect the user to Grant's internal callback route after the provider redirects them back to your origin.// Subdomain example "shopify": { "subdomain": "mycompany" } // Sandbox example "paypal": { "authorize_url": "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize", "access_url": "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice" }Use Grant with ES Modules and TypeScript
masterTo use Grant in
.mjsfiles, import it directly. If you are importing a.jsonconfiguration file in an ES module, you may need to run Node with the--experimental-json-modulesflag. Grant includes built-in TypeScript definitions.import express from 'express' import session from 'express-session' import grant from 'grant' import config from './config.json' express() .use(session({})) .use(grant.express(config))Configure Callback Transport
masterThe
transportsetting determines how response data is delivered to your application:querystring(Default): Encodes data as a query string in the redirect URL. Best for OAuth Proxies, but can leak data in logs.session: Recommended for local routes. Stores data in the session object.- Express:
req.session.grant.response - Koa:
ctx.session.grant.response - Fastify:
req.session.grant.response
- Express:
state: Uses the request/response lifecycle state. No callback route is needed.- Express:
res.locals.grant.response - Koa:
ctx.state.grant.response - Fastify:
res.grant.response - Serverless:
var {response} = await grant(...)
- Express:
{ "defaults": { "transport": "session" }, "github": { "callback": "/hello" } }Handle `id_token` changes in Grant v5
masterIn Grant v5, the
id_tokenis returned as a raw string by default. In Grant v4, it was returned as a decoded object containingheader,payload, andsignature.Grant v5 default format:
{ id_token: 'abc.abc.abc', access_token: '...', refresh_token: '...' }Initiate OAuth login flows in the browser
masterOnce the server is running, you can trigger the OAuth flows by navigating to these specific endpoints in your browser:
- Google flow:
http://localhost:3000/connect/google - Twitter flow:
http://localhost:3000/connect/twitter
- Google flow: