Guest User

Untitled

a guest
Feb 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. class OpenIdController < ApplicationController
  2. open_id_consumer :required => [:email, :nickname]
  3.  
  4. def begin
  5. # If the URL was unusable (either because of network conditions,
  6. # a server error, or that the response returned was not an OpenID
  7. # identity page), the library will return HTTP_FAILURE or PARSE_ERROR.
  8. # Let the user know that the URL is unusable.
  9. case open_id_response.status
  10. when OpenID::SUCCESS
  11. # The URL was a valid identity URL. Now we just need to send a redirect
  12. # to the server using the redirect_url the library created for us.
  13.  
  14. # redirect to the server
  15. redirect_to open_id_response.redirect_url((request.protocol + request.host_with_port + "/"), url_for(:action => 'complete'))
  16. else
  17. flash[:error] = "Unable to find openid server for <q>#{params[:openid_url]}</q>"
  18. render :template => 'account/index'
  19. end
  20. end
  21.  
  22. def complete
  23. case open_id_response.status
  24. when OpenID::FAILURE
  25. # In the case of failure, if info is non-nil, it is the
  26. # URL that we were verifying. We include it in the error
  27. # message to help the user figure out what happened.
  28. if open_id_response.identity_url
  29. flash[:message] = "Verification of #{open_id_response.identity_url} failed. "
  30. else
  31. flash[:message] = "Verification failed. "
  32. end
  33. flash[:message] += open_id_response.msg.to_s
  34.  
  35. when OpenID::SUCCESS
  36. # Success means that the transaction completed without
  37. # error. If info is nil, it means that the user cancelled
  38. # the verification.
  39. flash[:message] = "You have successfully verified #{open_id_response.identity_url} as your identity."
  40. if open_id_fields.any?
  41. @user = User.find_by_identity_url(open_id_response.identity_url)
  42. @user ||= User.new(:identity_url => open_id_response.identity_url)
  43. @user.login = open_id_fields['openid.sreg.nickname'] if open_id_fields['openid.sreg.nickname']
  44. @user.email = open_id_fields['openid.sreg.email'] if open_id_fields['openid.sreg.email']
  45. if @user.save
  46. self.current_user = @user
  47. wipe_session_cache
  48. flash[:notice] = "Logged in successfully"
  49. redirect_back_or_default(:controller => 'articles', :action => 'index')
  50. else
  51. render :template => 'account/signup'
  52. end
  53. end
  54.  
  55. when OpenID::CANCEL
  56. flash[:message] = "Verification cancelled."
  57.  
  58. else
  59. flash[:message] = "Unknown response status: #{open_id_response.status}"
  60. end
  61. redirect_to :controller => 'account', :action => 'signup' unless performed?
  62. end
  63.  
  64. end
Add Comment
Please, Sign In to add comment