Sha256: 79a758aaf57fd4d68354d2ccf0b4c75d2cdfb2dcf44b896a9a10ca4929f7544d

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

require 'roo_on_rails/config'

module RooOnRails
  module Railties
    class GoogleOAuth < Rails::Railtie
      initializer 'roo_on_rails.google_auth.middleware' do |app|
        $stderr.puts 'initializer roo_on_rails.google_auth'
        _if_enabled do
          _add_middleware(app)
          _add_routes(app)
        end
      end

      private

      def _if_enabled
        return unless Config.google_auth_enabled?
        yield
      end

      def _add_middleware(app)
        require 'omniauth'
        require 'omniauth-google-oauth2'
        require 'active_support/core_ext/object/blank'

        options = {
          path_prefix: Config.google_auth_path_prefix,
          prompt: 'consent',
          # https://stackoverflow.com/questions/45271730/jwtinvalidissuererror-invalid-issuer-expected-accounts-google-com-received
          # https://github.com/zquestz/omniauth-google-oauth2/issues/197
          skip_jwt: true,
        }

        domain_list = ENV.fetch('GOOGLE_AUTH_ALLOWED_DOMAINS', '').split(',').reject(&:blank?)
        options[:hd] = domain_list if domain_list.any?

        app.config.middleware.use ::OmniAuth::Builder do
          provider :google_oauth2,
            ENV.fetch('GOOGLE_AUTH_CLIENT_ID'),
            ENV.fetch('GOOGLE_AUTH_CLIENT_SECRET'),
            options
        end
      end

      def _add_routes(app)
        prefix = Config.google_auth_path_prefix
        ctrl   = Config.google_auth_controller

        app.routes.prepend do
          get  "#{prefix}/google_oauth2",           controller: ctrl, action: 'failure'
          get  "#{prefix}/google_oauth2/callback",  controller: ctrl, action: 'create'
          post "#{prefix}/google_oauth2/callback",  controller: ctrl, action: 'create'
          get  "#{prefix}/failure",                 controller: ctrl, action: 'failure'
          get  "#{prefix}/logout",                  controller: ctrl, action: 'destroy'
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roo_on_rails-1.10.0 lib/roo_on_rails/railties/google_oauth.rb
roo_on_rails-1.9.0 lib/roo_on_rails/railties/google_oauth.rb
roo_on_rails-1.8.1 lib/roo_on_rails/railties/google_oauth.rb