Grant OAuth Proxy

repository·master·Indexed 26 days ago

https://github.com/simov/grant

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

Tokens
9.4K
Snippets
24
Records
56
Agent score
87%

What's inside grant

  1. Overview of Grant OAuth Proxy

    master
    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.
  2. Configure Grant Connection Routes and Redirect URIs

    master

    Grant uses specific URL patterns for the OAuth flow.

    Login Route: Navigate to [origin][prefix]/:provider to 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]/callback

    Example: If origin is http://localhost:3000 and prefix is /connect, the redirect URI for Google would be http://localhost:3000/connect/google/callback.

  3. Define a custom OAuth provider

    master

    To 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, and scope.

    {
      "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"]
      }
    }
  4. Migrate from `path` to `prefix` in Grant v5

    master

    The path configuration key used in Grant v4 for setting a path prefix is deprecated. In Grant v5, use the prefix key instead. Note that the prefix should typically include the /connect suffix if you are following standard patterns.

    {
      "defaults": {
        "origin": "http://localhost:3000",
        "prefix": "/oauth/connect"
      }
    }
  5. Set up OAuth App redirect URIs for Grant examples

    master

    When 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
  6. Integrate Grant with HTTP Frameworks

    master

    Grant 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*/}))
  7. Handle OAuth Subdomain and Sandbox requirements

    master

    Subdomains

    Some providers (like Shopify or Mastodon) require dynamic URLs. Use the subdomain option in your provider config to inject values into the authorize_url and access_url.

    Sandbox URLs

    To use a provider's sandbox environment, override the request_url, authorize_url, and access_url in your configuration with the sandbox-specific endpoints.

    Sandbox Redirect URIs

    If a provider (like Feedly) restricts the allowed redirect_uri in 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"
    }
  8. Use Grant with ES Modules and TypeScript

    master

    To use Grant in .mjs files, import it directly. If you are importing a .json configuration file in an ES module, you may need to run Node with the --experimental-json-modules flag. 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))
  9. Configure Callback Transport

    master

    The transport setting determines how response data is delivered to your application:

    1. querystring (Default): Encodes data as a query string in the redirect URL. Best for OAuth Proxies, but can leak data in logs.
    2. 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
    3. 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(...)
    {
      "defaults": {
        "transport": "session"
      },
      "github": {
        "callback": "/hello"
      }
    }
  10. Handle `id_token` changes in Grant v5

    master

    In Grant v5, the id_token is returned as a raw string by default. In Grant v4, it was returned as a decoded object containing header, payload, and signature.

    Grant v5 default format:

    {
      id_token: 'abc.abc.abc',
      access_token: '...',
      refresh_token: '...'
    }
  11. Initiate OAuth login flows in the browser

    master

    Once 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