/lib/devise_shibboleth_authenticatable/model.rb

https://github.com/jgeorge300/devise_shibboleth_authenticatable · Ruby · 66 lines · 53 code · 12 blank · 1 comment · 6 complexity · dffe9df42e91eaba47044a4132d96e4b MD5 · raw file

  1. require 'devise_shibboleth_authenticatable/strategy'
  2. module Devise
  3. module Models
  4. module ShibbolethAuthenticatable
  5. extend ActiveSupport::Concern
  6. # Need to determine why these need to be included
  7. included do
  8. attr_reader :password, :current_password
  9. attr_accessor :password_confirmation
  10. end
  11. def update_with_password(params={})
  12. params.delete(:current_password)
  13. self.update_without_password(params)
  14. end
  15. def update_without_password(params={})
  16. params.delete(:password)
  17. params.delete(:password_confirmation)
  18. result = update_attributes(params)
  19. result
  20. end
  21. module ClassMethods
  22. def authenticate_with_shibboleth(env)
  23. auth_key = self.authentication_keys.first
  24. auth_key_value = (self.case_insensitive_keys || []).include?(auth_key) ? env['eppn'].downcase : env['eppn']
  25. resource = where(auth_key => auth_key_value).first
  26. if (resource.nil? && !Devise.shibboleth_create_user)
  27. logger.info("User(#{auth_key_value}) not found. Not configured to create the user.")
  28. return nil
  29. end
  30. if (resource.nil? && Devise.shibboleth_create_user)
  31. logger.info("Creating user(#{auth_key_value}).")
  32. resource = new
  33. save_user_shibboleth_headers(resource, env)
  34. resource.save
  35. end
  36. resource
  37. end
  38. def find_for_shibb_authentication(conditions)
  39. find_for_authentication(conditions)
  40. end
  41. private
  42. def save_user_shibboleth_headers(user, env)
  43. shib_config = YAML.load(ERB.new(File.read(::Devise.shibboleth_config || "#{Rails.root}/config/shibboleth.yml")).result)[Rails.env]
  44. shib_config['user-mapping'].each do |model, header|
  45. logger.info("Saving #{env[header]} to #{model}")
  46. field = "#{model}="
  47. value = env[header]
  48. user.send(field, value.to_s)
  49. end
  50. end
  51. end
  52. end
  53. end
  54. end