Creating a custom development strategy

Recently struggling with this, not sure why it is not working.

The bottom line is using Devise with LDAP. I do not need to do anything other than authentication, so I do not need to use anything other than a special strategy.

I created one based on https://github.com/plataformatec/devise/wiki/How-To:-Authenticate-via-LDAP , and as far as I can tell, everything should work, unless I try to start the server (or rake route) I get a NameError

 lib/devise/models.rb:88:in `const_get': uninitialized constant Devise::Models::LdapAuthenticatable (NameError) 

I traced the error to my app/models/user.rb

 class User < ActiveRecord::Base devise :ldap_authenticatable, :rememberable, :trackable, :timeoutable end 

If I delete :ldap_authenticatable , then the failure will disappear, but I have no routes to user#session , and the login prompt may not be available.

My supporting files:

lib/ldap_authenticatable.rb

 require 'net/ldap' require 'devise/strategies/authenticatable' module Devise module Strategies class LdapAuthenticatable < Authenticatable def authenticate! if params[:user] ldap = Net::LDAP.new ldap.host = 'redacted' ldap.port = 389 ldap.auth login, password if ldap.bind user = User.where(login: login).first_or_create do |user| success!(user) else fail(:invalid_login) end end end def login params[:user][:login] end def password params[:user][:password] end end end end Warden::Strategies.add(:ldap_authenticatable, Devise::Strategies::LdapAuthenticatable) 

And finally, inside config/initializers/devise.rb

 Devise.setup do |config| # ==> LDAP Configuration require 'ldap_authenticatable' config.warden do |manager| manager.default_strategies(:scope => :user).unshift :ldap_authenticatable end end 

I have exhausted my search, maybe someone can see what I am missing.

Greetings

+7
ruby ruby-on-rails ruby-on-rails-4 devise
source share
4 answers

Is your lib/ldap_authenticatable.rb in the startup path or explicitly required? Since the Rails 3 code in the lib folder is no longer loaded automatically by default. Here is one solution

IMHO Devise is a great stone. However, in order to write your own strategy, you must be familiar not only with Devise, but also with Warden source code, and a lot of the template code needs to be written in different places, so I’m starting to explore how to make it easier for Devise to come up with and come up with this stone devise_custom_authenticatable . You can check this out and probably it will solve your problem differently. This gem is used in the production codebase for a rather busy application, so the battle is proved :)

+2
source share

File paths must match namespaces. You need to add 2 levels of directories.

 mkdir lib/devise mkdir lib/devise/strategies mv lib/ldap_authenticatable.rb lib/devise/strategies/ldap_authenticatable.rb 

Because you have names marked as

 module Devise module Strategies class LdapAuthenticatable < Authenticatable ... 
+2
source share

A few steps to take care of when creating your custom strategy:

  • you will need to take care of the strategies folder, as indicated in @csi with it, create the models folder and create ldap_authenticatable.rb inside the models. therefore, the structure will look like this.

     lib/devise/strategies/ldap_authenticatable.rb lib/devise/models/ldap_authenticatable.rb 
  • Add these lines to lib/devise/models/ldap_authenticatable.rb

     require Rails.root.join('lib/devise/strategies/ldap_authenticatable') module Devise module Models module LdapAuthenticatable extend ActiveSupport::Concern end end end 
  • In config/initializers/devise.rb add these lines to the beginning.

     Devise.add_module(:ldap_authenticatable, { strategy: true, controller: :sessions, model: 'devise/models/ldap_authenticatable', route: :session }) 

This should take care of custom auth.

0
source share
0
source share

All Articles