add auth scopes
[arvados.git] / services / api / app / controllers / user_sessions_controller.rb
1 class UserSessionsController < ApplicationController
2   before_filter :require_auth_scope_all, :only => [ :destroy ]
3
4   skip_before_filter :uncamelcase_params_hash_keys
5   skip_before_filter :find_object_by_uuid
6
7   respond_to :html
8
9   # omniauth callback method
10   def create
11     omniauth = env['omniauth.auth']
12     #logger.debug "+++ #{omniauth}"
13
14     identity_url_ok = (omniauth['info']['identity_url'].length > 0) rescue false
15     unless identity_url_ok
16       # Whoa. This should never happen.
17
18       @title = "UserSessionsController.create: omniauth object missing/invalid"
19       @body = "omniauth.pretty_inspect():\n\n#{omniauth.pretty_inspect()}"
20
21       view_context.fatal_error(@title,@body)
22       return redirect_to openid_login_error_url
23     end
24
25     user = User.find_by_identity_url(omniauth['info']['identity_url'])
26     if not user
27       # New user registration
28       user = User.new(: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'],
32                       :is_active => Rails.configuration.new_users_are_active)
33     else
34       user.email = omniauth['info']['email']
35       user.first_name = omniauth['info']['first_name']
36       user.last_name = omniauth['info']['last_name']
37     end
38
39     # prevent ArvadosModel#before_create and _update from throwing
40     # "unauthorized":
41     Thread.current[:user] = user
42
43     user.save!
44
45     omniauth.delete('extra')
46
47     # Give the authenticated user a cookie for direct API access
48     session[:user_id] = user.id
49     session[:api_client_uuid] = nil
50     session[:api_client_trusted] = true # full permission to see user's secrets
51
52     @redirect_to = root_path
53     if params.has_key?(:return_to)
54       return send_api_token_to(params[:return_to], user)
55     end
56     redirect_to @redirect_to
57   end
58
59   # Omniauth failure callback
60   def failure
61     flash[:notice] = params[:message]
62   end
63
64   # logout - Clear our rack session BUT essentially redirect to the provider
65   # to clean up the Devise session from there too !
66   def logout
67     session[:user_id] = nil
68
69     flash[:notice] = 'You have logged off'
70     return_to = params[:return_to] || root_url
71     redirect_to "#{CUSTOM_PROVIDER_URL}/users/sign_out?redirect_uri=#{CGI.escape return_to}"
72   end
73
74   # login - Just bounce to /auth/joshid. The only purpose of this function is
75   # to save the return_to parameter (if it exists; see the application
76   # controller). /auth/joshid bypasses the application controller.
77   def login
78     if current_user and params[:return_to]
79       # Already logged in; just need to send a token to the requesting
80       # API client.
81       #
82       # FIXME: if current_user has never authorized this app before,
83       # ask for confirmation here!
84
85       send_api_token_to(params[:return_to], current_user)
86     elsif params[:return_to]
87       redirect_to "/auth/joshid?return_to=#{CGI.escape(params[:return_to])}"
88     else
89       redirect_to "/auth/joshid"
90     end
91   end
92
93   def send_api_token_to(callback_url, user)
94     # Give the API client a token for making API calls on behalf of
95     # the authenticated user
96
97     # Stub: automatically register all new API clients
98     api_client_url_prefix = callback_url.match(%r{^.*?://[^/]+})[0] + '/'
99     api_client = ApiClient.find_or_create_by_url_prefix(api_client_url_prefix)
100
101     api_client_auth = ApiClientAuthorization.
102       new(user: user,
103           api_client: api_client,
104           created_by_ip_address: remote_ip)
105     api_client_auth.save!
106
107     if callback_url.index('?')
108       callback_url << '&'
109     else
110       callback_url << '?'
111     end
112     callback_url << 'api_token=' << api_client_auth.api_token
113     redirect_to callback_url
114   end
115 end