2017-09-25 03:10:25 -04:00
|
|
|
module GoogleApi
|
2017-09-30 11:54:22 -04:00
|
|
|
class Auth
|
2017-09-25 03:10:25 -04:00
|
|
|
attr_reader :access_token, :redirect_uri, :state
|
|
|
|
|
2017-09-27 08:01:08 -04:00
|
|
|
ConfigMissingError = Class.new(StandardError)
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
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,
|
|
|
|
state: state # This is used for arbitary redirection
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_token(code)
|
2017-10-02 04:13:46 -04:00
|
|
|
ret = client.auth_code.get_token(code, redirect_uri: redirect_uri)
|
|
|
|
return ret.token, ret.expires_at
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def scope
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def config
|
2018-03-12 17:01:43 -04:00
|
|
|
Gitlab::Auth::OAuth::Provider.config_for('google_oauth2')
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
2017-09-27 08:01:08 -04:00
|
|
|
|
|
|
|
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',
|
2017-10-04 03:04:45 -04:00
|
|
|
token_url: '/o/oauth2/token',
|
2017-09-27 08:01:08 -04:00
|
|
|
authorize_url: '/o/oauth2/auth'
|
|
|
|
)
|
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
end
|