-
-
Save farmisen/d961fcc3a0b4771aa4f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Devise.setup do |config| | |
config.mailer_sender = "[email protected]" | |
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment