1 class UserSessionsController < ApplicationController
2 before_filter :require_auth_scope_all, :only => [ :destroy ]
4 skip_before_filter :find_object_by_uuid
8 # omniauth callback method
10 omniauth = env['omniauth.auth']
11 #logger.debug "+++ #{omniauth}"
13 identity_url_ok = (omniauth['info']['identity_url'].length > 0) rescue false
14 unless identity_url_ok
15 # Whoa. This should never happen.
17 @title = "UserSessionsController.create: omniauth object missing/invalid"
18 @body = "omniauth.pretty_inspect():\n\n#{omniauth.pretty_inspect()}"
20 view_context.fatal_error(@title,@body)
21 return redirect_to openid_login_error_url
24 user = User.find_by_identity_url(omniauth['info']['identity_url'])
26 # New user registration
27 user = User.new(:email => omniauth['info']['email'],
28 :first_name => omniauth['info']['first_name'],
29 :last_name => omniauth['info']['last_name'],
30 :identity_url => omniauth['info']['identity_url'],
31 :is_active => Rails.configuration.new_users_are_active)
33 user.email = omniauth['info']['email']
34 user.first_name = omniauth['info']['first_name']
35 user.last_name = omniauth['info']['last_name']
38 # prevent ArvadosModel#before_create and _update from throwing
40 Thread.current[:user] = user
44 omniauth.delete('extra')
46 # Give the authenticated user a cookie for direct API access
47 session[:user_id] = user.id
48 session[:api_client_uuid] = nil
49 session[:api_client_trusted] = true # full permission to see user's secrets
51 @redirect_to = root_path
52 if params.has_key?(:return_to)
53 return send_api_token_to(params[:return_to], user)
55 redirect_to @redirect_to
58 # Omniauth failure callback
60 flash[:notice] = params[:message]
63 # logout - Clear our rack session BUT essentially redirect to the provider
64 # to clean up the Devise session from there too !
66 session[:user_id] = nil
68 flash[:notice] = 'You have logged off'
69 return_to = params[:return_to] || root_url
70 redirect_to "#{CUSTOM_PROVIDER_URL}/users/sign_out?redirect_uri=#{CGI.escape return_to}"
73 # login - Just bounce to /auth/joshid. The only purpose of this function is
74 # to save the return_to parameter (if it exists; see the application
75 # controller). /auth/joshid bypasses the application controller.
77 if current_user and params[:return_to]
78 # Already logged in; just need to send a token to the requesting
81 # FIXME: if current_user has never authorized this app before,
82 # ask for confirmation here!
84 send_api_token_to(params[:return_to], current_user)
85 elsif params[:return_to]
86 redirect_to "/auth/joshid?return_to=#{CGI.escape(params[:return_to])}"
88 redirect_to "/auth/joshid"
92 def send_api_token_to(callback_url, user)
93 # Give the API client a token for making API calls on behalf of
94 # the authenticated user
96 # Stub: automatically register all new API clients
97 api_client_url_prefix = callback_url.match(%r{^.*?://[^/]+})[0] + '/'
98 api_client = ApiClient.find_or_create_by_url_prefix(api_client_url_prefix)
100 api_client_auth = ApiClientAuthorization.
102 api_client: api_client,
103 created_by_ip_address: remote_ip)
104 api_client_auth.save!
106 if callback_url.index('?')
111 callback_url << 'api_token=' << api_client_auth.api_token
112 redirect_to callback_url