pam_authenticable.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # frozen_string_literal: true
  2. module User::PamAuthenticable
  3. extend ActiveSupport::Concern
  4. included do
  5. devise :pam_authenticatable if ENV['PAM_ENABLED'] == 'true'
  6. def pam_conflict(_attributes)
  7. # Block pam login tries on traditional account
  8. end
  9. def pam_conflict?
  10. if Devise.pam_authentication
  11. encrypted_password.present? && pam_managed_user?
  12. else
  13. false
  14. end
  15. end
  16. def pam_get_name
  17. if account.present?
  18. account.username
  19. else
  20. super
  21. end
  22. end
  23. def pam_setup(_attributes)
  24. account = Account.new(username: pam_get_name)
  25. account.save!(validate: false)
  26. self.email = "#{account.username}@#{find_pam_suffix}" if email.nil? && find_pam_suffix
  27. self.confirmed_at = Time.now.utc
  28. self.account = account
  29. self.external = true
  30. account.destroy! unless save
  31. end
  32. def self.pam_get_user(attributes = {})
  33. return nil unless attributes[:email]
  34. resource = if Devise.check_at_sign && !attributes[:email].index('@')
  35. joins(:account).find_by(accounts: { username: attributes[:email] })
  36. else
  37. find_by(email: attributes[:email])
  38. end
  39. if resource.nil?
  40. resource = new(email: attributes[:email], agreement: true)
  41. if Devise.check_at_sign && !resource[:email].index('@')
  42. resource[:email] = Rpam2.getenv(resource.find_pam_service, attributes[:email], attributes[:password], 'email', false)
  43. resource[:email] = "#{attributes[:email]}@#{resource.find_pam_suffix}" unless resource[:email]
  44. end
  45. end
  46. resource
  47. end
  48. def self.authenticate_with_pam(attributes = {})
  49. super if Devise.pam_authentication
  50. end
  51. end
  52. end