Guest User

Untitled

a guest
Feb 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. require 'rubygems'
  2. require 'httpauth'
  3. require 'addressable/uri'
  4.  
  5. class SSBEAuthenticator
  6.  
  7. attr_reader :username, :password, :realm, :domain, :challenge
  8.  
  9. def initialize(username, password)
  10. @username, @password = username, password
  11. @realm = 'SystemShepherd'
  12. @domain = nil
  13. end
  14.  
  15. def update_credentials(challenge_response)
  16. @domain = Addressable::URI.parse(challenge_response.uri).host
  17. @challenge = HTTPAuth::Digest::Challenge.from_header(challenge_response.header['WWW-Authenticate'].first)
  18. end
  19.  
  20. def valid_for?(challenge_response)
  21. return false unless challenge_header = challenge_response.header['WWW-Authenticate']
  22. begin
  23. challenge = HTTPAuth::Digest::Challenge.from_header(challenge_header.first)
  24. rescue HTTPAuth::UnwellformedHeader
  25. return false
  26. end
  27. challenge.realm == @realm
  28. end
  29.  
  30. def can_handle?(request)
  31. Addressable::URI.parse(request.uri).host == @domain
  32. end
  33.  
  34. def add_credentials_to(request)
  35. request.header['Authorization'] = credentials_for(request)
  36. end
  37.  
  38. def credentials_for(request)
  39. HTTPAuth::Digest::Credentials.from_challenge(@challenge,
  40. :username => @username,
  41. :password => @password,
  42. :method => request.method.to_s.upcase,
  43. :uri => Addressable::URI.parse(request.uri).path).to_header
  44. end
  45.  
  46. end
Add Comment
Please, Sign In to add comment