1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
5 class UserSessionsController < ApplicationController
6 before_action :require_auth_scope, :only => [ :destroy ]
8 skip_before_action :set_cors_headers
9 skip_before_action :find_object_by_uuid
10 skip_before_action :render_404_if_no_object
14 # omniauth callback method
16 if !Rails.configuration.Login.LoginCluster.empty? and Rails.configuration.Login.LoginCluster != Rails.configuration.ClusterID
17 raise "Local login disabled when LoginCluster is set"
20 if params[:provider] == 'controller'
21 if request.headers['Authorization'] != 'Bearer ' + Rails.configuration.SystemRootToken
22 return send_error('Invalid authorization header', status: 401)
24 # arvados-controller verified the user and is passing auth_info
26 authinfo = SafeJSON.load(params[:auth_info])
28 # omniauth middleware verified the user and is passing auth_info
30 authinfo = request.env['omniauth.auth']['info'].with_indifferent_access
34 user = User.register(authinfo)
36 Rails.logger.warn "User.register error #{e}"
37 Rails.logger.warn "authinfo was #{authinfo.inspect}"
38 return redirect_to login_failure_url
41 # For the benefit of functional and integration tests:
44 if user.uuid[0..4] != Rails.configuration.ClusterID
45 # Actually a remote user
46 # Send them to their home cluster's login
47 rh = Rails.configuration.RemoteClusters[user.uuid[0..4]]
48 remote, return_to_url = params[:return_to].split(',', 2)
49 @remotehomeurl = "#{rh.Scheme || "https"}://#{rh.Host}/login?remote=#{Rails.configuration.ClusterID}&return_to=#{return_to_url}"
54 # prevent ArvadosModel#before_create and _update from throwing
56 Thread.current[:user] = user
58 user.save or raise Exception.new(user.errors.messages)
60 # Give the authenticated user a cookie for direct API access
61 session[:user_id] = user.id
62 session[:api_client_uuid] = nil
63 session[:api_client_trusted] = true # full permission to see user's secrets
65 @redirect_to = root_path
66 if params.has_key?(:return_to)
67 # return_to param's format is 'remote,return_to_url'. This comes from login()
68 # encoding the remote=zbbbb parameter passed by a client asking for a salted
70 remote, return_to_url = params[:return_to].split(',', 2)
71 if remote !~ /^[0-9a-z]{5}$/ && remote != ""
72 return send_error 'Invalid remote cluster id', status: 400
74 remote = nil if remote == ''
75 return send_api_token_to(return_to_url, user, remote)
77 redirect_to @redirect_to
80 # Omniauth failure callback
82 flash[:notice] = params[:message]
85 # logout - Clear our rack session BUT essentially redirect to the provider
86 # to clean up the Devise session from there too !
88 session[:user_id] = nil
90 flash[:notice] = 'You have logged off'
91 return_to = params[:return_to] || root_url
92 redirect_to "#{Rails.configuration.Services.SSO.ExternalURL}users/sign_out?redirect_uri=#{CGI.escape return_to}"
95 # login - Just bounce to /auth/joshid. The only purpose of this function is
96 # to save the return_to parameter (if it exists; see the application
97 # controller). /auth/joshid bypasses the application controller.
99 if params[:remote] !~ /^[0-9a-z]{5}$/ && !params[:remote].nil?
100 return send_error 'Invalid remote cluster id', status: 400
102 if current_user and params[:return_to]
103 # Already logged in; just need to send a token to the requesting
106 # FIXME: if current_user has never authorized this app before,
107 # ask for confirmation here!
109 return send_api_token_to(params[:return_to], current_user, params[:remote])
112 p << "auth_provider=#{CGI.escape(params[:auth_provider])}" if params[:auth_provider]
114 if !Rails.configuration.Login.LoginCluster.empty? and Rails.configuration.Login.LoginCluster != Rails.configuration.ClusterID
115 host = ApiClientAuthorization.remote_host(uuid_prefix: Rails.configuration.Login.LoginCluster)
117 raise "LoginCluster #{Rails.configuration.Login.LoginCluster} missing from RemoteClusters"
120 cluster = Rails.configuration.RemoteClusters[Rails.configuration.Login.LoginCluster]
121 if cluster and cluster['Scheme'] and !cluster['Scheme'].empty?
122 scheme = cluster['Scheme']
124 login_cluster = "#{scheme}://#{host}"
125 p << "remote=#{CGI.escape(params[:remote])}" if params[:remote]
126 p << "return_to=#{CGI.escape(params[:return_to])}" if params[:return_to]
127 redirect_to "#{login_cluster}/login?#{p.join('&')}"
129 if params[:return_to]
130 # Encode remote param inside callback's return_to, so that we'll get it on
131 # create() after login.
132 remote_param = params[:remote].nil? ? '' : params[:remote]
133 p << "return_to=#{CGI.escape(remote_param + ',' + params[:return_to])}"
135 redirect_to "/auth/joshid?#{p.join('&')}"
139 def send_api_token_to(callback_url, user, remote=nil)
140 # Give the API client a token for making API calls on behalf of
141 # the authenticated user
143 # Stub: automatically register all new API clients
144 api_client_url_prefix = callback_url.match(%r{^.*?://[^/]+})[0] + '/'
145 act_as_system_user do
146 @api_client = ApiClient.
147 find_or_create_by(url_prefix: api_client_url_prefix)
150 token_expiration = nil
151 if Rails.configuration.Login.TokenLifetime > 0
152 token_expiration = Time.now + Rails.configuration.Login.TokenLifetime
154 @api_client_auth = ApiClientAuthorization.
156 api_client: @api_client,
157 created_by_ip_address: remote_ip,
158 expires_at: token_expiration,
160 @api_client_auth.save!
162 if callback_url.index('?')
168 token = @api_client_auth.token
170 token = @api_client_auth.salted_token(remote: remote)
172 callback_url += 'api_token=' + token
173 redirect_to callback_url
176 def cross_origin_forbidden
177 send_error 'Forbidden', status: 403