2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module GoogleApi
|
|
|
|
class Auth
|
|
|
|
attr_reader :access_token, :redirect_uri, :state
|
|
|
|
|
|
|
|
ConfigMissingError = Class.new(StandardError)
|
|
|
|
|
|
|
|
def initialize(access_token, redirect_uri, state: nil)
|
|
|
|
@access_token = access_token
|
|
|
|
@redirect_uri = redirect_uri
|
|
|
|
@state = state
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorize_url
|
|
|
|
client.auth_code.authorize_url(
|
|
|
|
redirect_uri: redirect_uri,
|
|
|
|
scope: scope,
|
2018-12-13 13:39:08 +05:30
|
|
|
state: state # This is used for arbitrary redirection
|
2018-03-17 18:26:18 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(code)
|
|
|
|
ret = client.auth_code.get_token(code, redirect_uri: redirect_uri)
|
|
|
|
return ret.token, ret.expires_at
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def scope
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def config
|
2018-05-09 12:01:36 +05:30
|
|
|
Gitlab::Auth::OAuth::Provider.config_for('google_oauth2')
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def client_options
|
|
|
|
config.args.client_options.deep_symbolize_keys
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def client
|
|
|
|
return @client if defined?(@client)
|
|
|
|
|
|
|
|
unless config
|
|
|
|
raise ConfigMissingError
|
|
|
|
end
|
|
|
|
|
|
|
|
@client = ::OAuth2::Client.new(
|
|
|
|
config.app_id,
|
|
|
|
config.app_secret,
|
|
|
|
site: 'https://accounts.google.com',
|
|
|
|
token_url: '/o/oauth2/token',
|
2020-05-24 23:13:21 +05:30
|
|
|
authorize_url: '/o/oauth2/auth',
|
|
|
|
**client_options
|
2018-03-17 18:26:18 +05:30
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|