jeudi 30 mai 2019

Custom strategyfor Omniauth

I need to implement a custom strategy for an app in rails, The version of the software: Rails 5.0.7.2 ruby 2.3.2p217 (2016-11-15 revision 56796) [x86_64-linux] Bundler version 1.16.6

These are the files I added:

# config/initializers/omniauth.rb
module OmniAuth
  module Strategies
    # tell OmniAuth to load our strategy
    autoload :Platform, 'lib/strategies/platform.rb'
  end
end

Rails.application.config.middleware.use OmniAuth::Builder do
  # pass the 2 parameters to the constructor
  provider :platform, "Client Id", "Client Secret",
      client_options: {
        site: "https://my.auth.provider",
        user_info_url: "/connect/userinfo"
      }

end

Now, in my lib/strategies/ directory I created my strategy file:

# lib/strategies/platform.rb
require 'omniauth-oauth2'

module OmniAuth
  module Strategies
    class Platform < OmniAuth::Strategies::OAuth2
      option :name, :platform

      option :client_options, {
        :site => "https://my.auth.provider",
        :authorize_url => "/connect/authorize",
        :user_info_url => "/connect/userinfo"
      }

      uid { raw_info["id"] }

      info do
        {
          :email => raw_info["email"]
          # and anything else you want to return to your API consumers
        }
      end

      def raw_info
        @raw_info ||= access_token.get('/connect/userinfo/me.json').parsed
      end

      def callback_url
        full_host + script_name + callback_path
      end
    end
  end
end

And I registered the callback URL in the config/routes.rb:

#OAuth2 Generic
get "/auth/:provider/callback" => "sessions#create"

Now, this app I am trying to configure, has already devise and omniauth enabled for Google, Twitter and Facebook, but for some reason, my URL does never get registered. What am I missing? what should I do? I want to have

user_platform_omniauth_authorize_path and
user_platform_omniauth_callback_path

Registered routes for my app

Aucun commentaire:

Enregistrer un commentaire