简体   繁体   中英

Write a custom OmniAuth strategy

I've got a Rail 4 app with Devise. I'm trying to configure OmniAuth to use our corporate Ping OpenID Connect IdP. It appears that I have to write an OmniAuth strategy in Rack Middleware.

I took the 'omniauth-digitalocean' gem (which has their strategy) and carefully replaced all references of 'digitalocean' with another name. I was careful to respect all case to conform to convention.

The problem I'm having now is that I appear to have a private gem. I added it to my Gemfile with:

gem 'omniauth-private', :path => "/var/lib/gems/2.0.0/gems/omniauth-private-0.1.0"

I get no errors when I run 'bundle install'.

I was getting this error with 'rake db:migrate':

fatal: Not a git repository (or any of the parent directories): .git

I believe this was caused by a .gitignore file in my custom gem. I deleted the .gitignore file and now I'm getting:

Devise::OmniAuth::StrategyNotFound: Could not find a strategy with name `Private'. Please ensure it is required or explicitly set it using the :strategy_class option.

This is the same error message I was getting before I figure out I needed to write n Omniuth strategy, so I think it means my gem is not being recognized.

So I'm not sure exactly what's going on. I think I'm struggling with this private gem. But it could be an OmniAuth problem too.

Anyone ever gotten a private OpenID Connect IdP working with OmniAuth?

I had the same "Could not find a strategy with the name..." with my custom Omniauth OAuth2 strategy.

I created a custom strategy as per these instructions https://github.com/intridea/omniauth-oauth2 , and saved my file in config/initializers - this then loads the module on ruby boot.

I feel that I should be able to store this in the lib/ folder, but can't work out what the filename or folder structure should be!

The "fatal" error about "Not a git repo" comes from the fact that gems use 'git ls'. Just running "git init" should fix it. I did that and then committed to github.

"Could not find a strategy with the name..." error is fixed by loading the custom gem properly. I did that by adding this line to my Gemfile:

gem 'omniauth-private', :path => "/var/lib/gems/2.0.0/gems/omniauth-private-0.1.0

You need to add:

require 'strategies/private'

to the top of config/devise.rb. This points to your strategy file at /lib/strategies/private.rb

In your devise/initializers file check and make sure you have the correct name of the auth you want to configure example:

 config.omniauth :facebook, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' 
 config.omniauth :private, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' 
 config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM