2018-09-23 19:44:14 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-25 07:10:25 +00:00
|
|
|
module GoogleApi
|
|
|
|
class AuthorizationsController < ApplicationController
|
2019-02-13 00:11:28 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
before_action :validate_session_key!
|
|
|
|
|
2017-09-25 07:10:25 +00:00
|
|
|
def callback
|
2017-10-02 08:13:46 +00:00
|
|
|
token, expires_at = GoogleApi::CloudPlatform::Client
|
2017-10-05 15:41:36 +00:00
|
|
|
.new(nil, callback_google_api_auth_url)
|
2017-10-02 08:13:46 +00: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 07:10:25 +00:00
|
|
|
|
2020-05-18 12:08:08 +00:00
|
|
|
rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
|
|
|
|
flash[:alert] = _('Timeout connecting to the Google API. Please try again.')
|
|
|
|
ensure
|
2019-02-13 00:11:28 +00:00
|
|
|
redirect_to redirect_uri_from_session
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
2017-10-06 14:14:14 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-13 00:11:28 +00: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 14:14:14 +00:00
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
end
|