Guest User

Untitled

a guest
Apr 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. require 'active_support'
  2. require 'base64'
  3. require 'net/ldap'
  4.  
  5. module LdapAuth #:nodoc:
  6. require 'ldap_constants'
  7. ############################################################################
  8. # Auth against LDAP
  9. ############################################################################
  10. def initialize_ldap_con username, password
  11. return Net::LDAP.new({:host=>LDAP_IP, :port=>LDAP_PORT, :encryption=>:simple_tls,
  12. :auth=>{:method=>:simple, :username=>username, :password=>password}})
  13. end
  14.  
  15. def auth_against_ldap user, password
  16. ldap_con = initialize_ldap_con(LDAP_USER, LDAP_PWD)
  17. filter = Net::LDAP::Filter.eq("sAMAccountName", user)
  18. dn = String.new
  19. ldap_con.search(:base=>LDAP_BASE_DN, :filter=>filter, :attributes=>'dn'){|entry| dn = entry.dn}
  20. login_succeeded = false
  21. unless dn.empty?
  22. ldap_con = initialize_ldap_con(dn, password)
  23. request.env['REMOTE_USER'] = user
  24. login_succeeded = true if ldap_con.bind
  25. end
  26. return login_succeeded
  27. end
  28. end
  29.  
  30. module SimpleHTTPAuthentication #:nodoc:
  31. include LdapAuth
  32. def self.append_features(base) #:nodoc:
  33. super
  34. base.extend(ClassMethods)
  35. end
  36.  
  37. # Authentifizierung, siehe RFC 2617.
  38. module ClassMethods #:doc:
  39. def requires_authentication(options = {})
  40. around_filter(SimpleHTTPAuthentication::ActionFilter.new(options))
  41. end
  42. end
  43.  
  44. class ActionFilter #:nodoc:
  45. attr_accessor :only_actions, :except_actions, :event_handler,
  46. :auth_location, :realm, :error_msg, :logout_action
  47.  
  48. def initialize(options)
  49. @only_actions = options[:only] || []
  50. @except_actions = options[:except] || []
  51. @event_handler = options[:using] || :auth_against_ldap #lambda{ |username, password| true }
  52. @auth_locations = options[:at] || ['REDIRECT_REDIRECT_X_HTTP_AUTHORIZATION',
  53. 'REDIRECT_X_HTTP_AUTHORIZATION',
  54. 'X-HTTP_AUTHORIZATION', 'HTTP_AUTHORIZATION']
  55. @realm = options[:realm] || 'Login Required'
  56. @logout_action = options[:logout_on]
  57. @error_msg = options[:error_msg] || "401 Unauthorized: You are not authorized to view this page."
  58. end
  59.  
  60. def before(controller)
  61. if controller.action_name.intern == @logout_action
  62. controller.response.headers["Status"] = "Unauthorized"
  63. controller.response.headers["WWW-Authenticate"] = "Basic realm=\"#{@realm}\""
  64. controller.render :action => @logout_action.to_s, :status => 401
  65. return false
  66. elsif (@only_actions.include?(controller.action_name.intern) || @only_actions.empty?) && !@except_actions.include?(controller.action_name.intern)
  67. username, password = get_auth_data(controller)
  68. authenticated = false
  69. if @event_handler
  70. if @event_handler.is_a?(Proc)
  71. authenticated = controller.instance_exec(username, password, &@event_handler)
  72. elsif @event_handler.is_a?(Symbol) || @event_handler.is_a?(String)
  73. simple_http_auth_handler = @event_handler
  74. controller.instance_eval do
  75. authenticated = self.send(simple_http_auth_handler, username, password)
  76. end
  77. else
  78. authenticated = true
  79. end
  80. else
  81. authenticated = true
  82. end
  83.  
  84. unless authenticated
  85. controller.response.headers["Status"] = "Unauthorized"
  86. controller.response.headers["WWW-Authenticate"] = "Basic realm=\"#{@realm}\""
  87. controller.render :text => @error_msg, :status => 401
  88. end
  89. return authenticated
  90. end
  91. end
  92.  
  93. def after(controller)
  94. # wird benötigt, da rails sonst rumheult
  95. end
  96.  
  97. private
  98. def get_auth_data(controller)
  99. authdata = nil
  100. for location in @auth_locations
  101. if controller.request.env.has_key?(location)
  102. # split basiert auf whitespace, aber splittet nur in 2 teile
  103. authdata = controller.request.env[location].to_s.split(nil, 2)
  104. end
  105. end
  106. if authdata and authdata[0] == 'Basic'
  107. user, pass = Base64.decode64(authdata[1]).split(':')[0..1]
  108. else
  109. user, pass = ['', '']
  110. end
  111. return user, pass
  112. end
  113.  
  114. end
  115. end
  116.  
  117. module ActionController #:nodoc:
  118. require 'ldap_constants'
  119. class Base #:nodoc:
  120. include SimpleHTTPAuthentication
  121. end
  122. end
Add Comment
Please, Sign In to add comment