Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. # The site you want to log in to
  2. SITE_URL = nil
  3.  
  4. # The username/password you want to log in with
  5. USERNAME = nil
  6. PASSWORD = nil
  7.  
  8. # All of these details are visible when you do a "login" and
  9. # inspect the parameters POSTed when you click "login" on
  10. # the Lock Widget. You'll want to do this in your favorite
  11. # web browser while having the developer console open and
  12. # inspecting the network traffic.
  13. AUTH0_CLIENT_ID = nil
  14. AUTH0_ACCOUNT_URL = nil
  15. AUTH0_CONNECTION = nil
  16. AUTH0_TENANT = nil
  17. AUTH0_APP_LOGIN_REDIRECT_URL = nil
  18.  
  19. # Make an initial call to the page that has the
  20. # state value that is used when POSTing to login
  21. response = RestClient.get(SITE_URL)
  22. state = /state: '([^']+)'/.match(response.body)[1]
  23.  
  24. # POST the username/password and attempt to log in
  25. response = RestClient.post(
  26. "#{AUTH0_ACCOUNT_URL}/usernamepassword/login",
  27. payload={
  28. client_id: AUTH0_CLIENT_ID,
  29. connection: AUTH0_CONNECTION,
  30. redirect_uri: AUTH0_APP_LOGIN_REDIRECT_URL,
  31. response_type: "code",
  32. scope: "openid email crud:all",
  33. sso: true,
  34. state: state,
  35. tenant: AUTH0_TENANT,
  36. username: USERNAME,
  37. password: PASSWORD
  38. },
  39. headers={
  40. cookies: response.cookies
  41. }
  42. )
  43.  
  44. # Extract the hidden form elements that we need to
  45. # POST to the callback
  46. form = Nokogiri::HTML(response.body)
  47. payload = {}
  48. form.css('input').each do |input|
  49. if input.attributes['type'].value == 'hidden'
  50. payload[input.attributes['name'].value] = input.attributes['value'].value
  51. end
  52. end
  53.  
  54. # Call the callback, and then redirect to where the
  55. # callback says to go.
  56. begin
  57. RestClient.post(
  58. "#{AUTH0_ACCOUNT_URL}/login/callback",
  59. payload=payload,
  60. headers={
  61. cookies: response.cookies
  62. }
  63. )
  64. rescue RestClient::MovedPermanently,
  65. RestClient::Found,
  66. RestClient::TemporaryRedirect => err
  67. logged_in_response = RestClient.get(err.response.headers[:location], headers={cookies: err.response.cookies})
  68. end
  69.  
  70. # The user is now logged in and logged_in_response now has all the
  71. # appropriate cookies that can be then used in subsequent call the
  72. # the site
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement