1 class UserSessionsController < ApplicationController
2 before_filter :login_required, :only => [ :destroy ]
4 skip_before_filter :uncamelcase_params_hash_keys
5 skip_before_filter :find_object_by_uuid
9 # omniauth callback method
11 omniauth = env['omniauth.auth']
12 #logger.debug "+++ #{omniauth}"
14 identity_url_ok = (omniauth['info']['identity_url'].length > 0) rescue false
15 unless identity_url_ok
16 # Whoa. This should never happen.
18 @title = "UserSessionsController.create: omniauth object missing/invalid"
19 @body = "omniauth.pretty_inspect():\n\n#{omniauth.pretty_inspect()}"
21 view_context.fatal_error(@title,@body)
22 return redirect_to openid_login_error_url
25 user = User.find_by_identity_url(omniauth['info']['identity_url'])
27 # New user registration
28 user = User.create!(:email => omniauth['info']['email'],
29 :first_name => omniauth['info']['first_name'],
30 :last_name => omniauth['info']['last_name'],
31 :identity_url => omniauth['info']['identity_url'])
33 user.email = omniauth['info']['email']
34 user.first_name = omniauth['info']['first_name']
35 user.last_name = omniauth['info']['last_name']
39 omniauth.delete('extra')
41 # Give the authenticated user a cookie for direct API access
42 session[:user_id] = user.id
43 session[:api_client_uuid] = nil
44 session[:api_client_trusted] = true # full permission to see user's secrets
46 @redirect_to = root_path
47 if params.has_key?(:return_to)
48 return send_api_token_to(params[:return_to], user)
50 redirect_to @redirect_to
53 # Omniauth failure callback
55 flash[:notice] = params[:message]
58 # logout - Clear our rack session BUT essentially redirect to the provider
59 # to clean up the Devise session from there too !
61 session[:user_id] = nil
63 flash[:notice] = 'You have logged off'
64 redirect_to "#{CUSTOM_PROVIDER_URL}/users/sign_out?redirect_uri=#{root_url}"
67 # login - Just bounce to /auth/joshid. The only purpose of this function is
68 # to save the return_to parameter (if it exists; see the application
69 # controller). /auth/joshid bypasses the application controller.
71 if current_user and params[:return_to]
72 # Already logged in; just need to send a token to the requesting
75 # FIXME: if current_user has never authorized this app before,
76 # ask for confirmation here!
78 send_api_token_to(params[:return_to], current_user)
79 elsif params[:return_to]
80 redirect_to "/auth/joshid?return_to=#{CGI.escape(params[:return_to])}"
82 redirect_to "/auth/joshid"
86 def send_api_token_to(callback_url, user)
87 # Give the API client a token for making API calls on behalf of
88 # the authenticated user
90 # Stub: automatically register all new API clients
91 api_client_url_prefix = callback_url.match(%r{^.*?://[^/]+})[0] + '/'
92 api_client = ApiClient.find_or_create_by_url_prefix(api_client_url_prefix)
94 api_client_auth = ApiClientAuthorization.
96 api_client: api_client,
97 created_by_ip_address: remote_ip)
100 if callback_url.index('?')
105 callback_url << 'api_token=' << api_client_auth.api_token
106 redirect_to callback_url