diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 21b54fa..5aee443 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,10 @@ class ApplicationController < ActionController::Base before_filter :require_login def require_login - redirect_to root_url unless logged_in? + unless logged_in? + session[:initial_url] = request.url + redirect_to root_url + end end def logged_in? diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index c4e7bc7..8ba8c6c 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,11 +1,15 @@ -require 'net/http' +require 'persona' class HomeController < ApplicationController + include Persona + skip_filter :require_login, only: [:index, :verify] def index if logged_in? - redirect_to :home + redirect_url = session[:initial_url] || :home + session[:initial_url] = nil + redirect_to redirect_url else render layout: 'landing' end @@ -21,22 +25,8 @@ class HomeController < ApplicationController end def verify - bid_resp = {} - if params['assertion'] - http = Net::HTTP.new 'browserid.org', 443 - http.use_ssl = true - response = http.post '/verify', - "audience=#{request.host_with_port}&assertion=#{params['assertion']}", - { 'Content-Type' => 'application/x-www-form-urlencoded' } - begin - bid_resp = JSON.parse response.body - if bid_resp['status'] == 'okay' - session[:email] = bid_resp['email'] - end - rescue JSON::ParserError - logger.info 'BrowserId returning bad JSON?' - end - end - render json: bid_resp + response = {} + verify_persona { |email, response| session[:email] = email } + render json: response end end \ No newline at end of file diff --git a/lib/persona.rb b/lib/persona.rb new file mode 100644 index 0000000..62b3466 --- /dev/null +++ b/lib/persona.rb @@ -0,0 +1,19 @@ +require 'net/http' + +module Persona + def verify_persona + if params['assertion'] + http = Net::HTTP.new 'persona.org', 443 + http.use_ssl = true + response = http.post '/verify', + "audience=#{request.host_with_port}&assertion=#{params['assertion']}", + { 'Content-Type' => 'application/x-www-form-urlencoded' } + begin + response = JSON.parse response.body + yield response['email'], response if response['status'] == 'okay' + rescue JSON::ParserError + logger.info 'PersonaId returned invalid JSON' + end + end + end +end \ No newline at end of file