Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Last active May 14, 2021 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhunt/61a193d9580e4f8d9036d5aedc2e5a83 to your computer and use it in GitHub Desktop.
Save tylerhunt/61a193d9580e4f8d9036d5aedc2e5a83 to your computer and use it in GitHub Desktop.
OAuth Redirect Server for Local Development

OAuth Redirect Server for Local Development

Some OAuth providers (like Google), limit what hosts can be used for redirect URIs. This can be problematic when using a hostname-based local development server (like Pow or Invoker). This very simple redirection server can be used to work around this restriction.

Usage

  1. Assuming your local hostname is app.test and your callback path is /auth/google/callback, add the following URL to the credentials section of the Google Cloud Platform console under “Authorized Redirect URIs”:
    http://localhost:4000/app.test/auth/google/callback
    
  2. Update your app to use this as the redirect URI in your development environment.
  3. Start the redirect server:
    PORT=4000 ruby oauth_redirector.rb
    You may need to use BUNDLE_IGNORE_CONFIG=true depending on how Bundler is configured.

Notes

  • The redirector assumes you’re using SSL for your local development server. If you aren’t, modifying the server is left as an exercise for the reader.
  • The port 4000 is used in these examples, but this can be changed as long as the same port is used in the credentials configuration, the app configuration, and when running the redirect server.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'puma', '~> 5.3'
gem 'rack', '~> 2.2'
end
Rack::Server.start(
app: ->(env) {
[302, {'Location' => "https://#{env['REQUEST_URI'].sub(%r(^/), '')}"}, []]
},
Port: ENV['PORT'],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment