2018-09-23 15:44:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
module GoogleApi
|
|
|
|
class AuthorizationsController < ApplicationController
|
2019-02-12 19:11:28 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
before_action :validate_session_key!
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
def callback
|
2017-10-02 04:13:46 -04:00
|
|
|
token, expires_at = GoogleApi::CloudPlatform::Client
|
2017-10-05 11:41:36 -04:00
|
|
|
.new(nil, callback_google_api_auth_url)
|
2017-10-02 04:13:46 -04:00
|
|
|
.get_token(params[:code])
|
|
|
|
|
|
|
|
session[GoogleApi::CloudPlatform::Client.session_key_for_token] = token
|
|
|
|
session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at] =
|
|
|
|
expires_at.to_s
|
2017-09-25 03:10:25 -04:00
|
|
|
|
2020-05-18 08:08:08 -04:00
|
|
|
rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
|
|
|
|
flash[:alert] = _('Timeout connecting to the Google API. Please try again.')
|
|
|
|
ensure
|
2019-02-12 19:11:28 -05:00
|
|
|
redirect_to redirect_uri_from_session
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
2017-10-06 10:14:14 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-12 19:11:28 -05:00
|
|
|
def validate_session_key!
|
|
|
|
access_denied! unless redirect_uri_from_session.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_uri_from_session
|
|
|
|
strong_memoize(:redirect_uri_from_session) do
|
|
|
|
if params[:state].present?
|
|
|
|
session[session_key_for_redirect_uri(params[:state])]
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def session_key_for_redirect_uri(state)
|
|
|
|
GoogleApi::CloudPlatform::Client.session_key_for_redirect_uri(state)
|
2017-10-06 10:14:14 -04:00
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
end
|