Skip to content

Instantly share code, notes, and snippets.

@elarex
Last active December 18, 2015 11:29
Show Gist options
  • Save elarex/5776467 to your computer and use it in GitHub Desktop.
Save elarex/5776467 to your computer and use it in GitHub Desktop.
Devise custom remote auth strategy
Devise.setup do |config|
config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
require 'devise/orm/active_record'
config.case_insensitive_keys = [ :email ]
config.strip_whitespace_keys = [ :email ]
config.skip_session_storage = [:http_auth]
config.stretches = Rails.env.test? ? 1 : 10
config.reconfirmable = true
config.password_length = 8..128
config.reset_password_within = 6.hours
config.sign_out_via = :delete
config.warden do |manager|
manager.intercept_401 = false
manager.default_strategies(:scope => :user).unshift :remote
end
end
Devise.add_module :remote_authenticatable, :controller => :sessions, :route => { :session => :routes }
require 'devise/strategies/remote_authenticatable'
#from here: https://gist.github.com/madtrick/3916999
module Devise
module Models
module RemoteAuthenticatable
extend ActiveSupport::Concern
#
# Here you do the request to the external webservice
#
# If the authentication is successful you should return
# a resource instance
#
# If the authentication fails you should return false
#
def remote_authentication(authentication_hash)
binding.pry
#construct a user instance from authentication_hash (Her::Resource)
#sign_in = SignIn.new(authentication_hash)
#sign_in.save() #trigger post
end
module ClassMethods
####################################
# Overriden methods from Devise::Models::Authenticatable
####################################
#
# This method is called from:
# Warden::SessionSerializer in devise
#
# It takes as many params as elements had the array
# returned in serialize_into_session
#
# Recreates a resource from session data
#
def serialize_from_session(id, email)
resource = self.new
resource.email = email
resource.id = id
resource
end
#
# Here you have to return an array with the data of your resource
# that you want to serialize into the session
#
# You might want to include some authentication data
#
def serialize_into_session(record)
[record.id, record.email]
end
end
end
end
end
#from here: https://gist.github.com/madtrick/3917079
require 'devise/strategies/authenticatable'
module Devise
module Strategies
class RemoteAuthenticatable < Authenticatable
def valid?
binding.pry
#params[:user] && params[:user][:email] && params[:user][:password]
true
end
#
# For an example check : https://github.com/plataformatec/devise/blob/master/lib/devise/strategies/database_authenticatable.rb
#
# Method called by warden to authenticate a resource.
#
def authenticate!
binding.pry #NEVER GETS HERE ?!
#
# authentication_hash doesn't include the password
#
auth_params = authentication_hash
auth_params[:password] = password
#
# mapping.to is a wrapper over the resource model
#
resource = mapping.to.new
return fail! unless resource
# remote_authentication method is defined in Devise::Models::RemoteAuthenticatable
#
# validate is a method defined in Devise::Strategies::Authenticatable. It takes
#a block which must return a boolean value.
#
# If the block returns true the resource will be loged in
# If the block returns false the authentication will fail!
#
if validate(resource){ resource.remote_authentication(auth_params) }
success!(resource)
end
end
end
end
end
Warden::Strategies.add(:remote_authenticatable, Devise::Strategies::RemoteAuthenticatable)
@elarex
Copy link
Author

elarex commented Jun 13, 2013

Gist limits the file names, the live in config/initializers/devise.rb, lib/devise/strategies/remote_authenticatable.rb and lib/devise/models/remote_authenticatable.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment