diff options
author | Shinya Maeda <shinya@gitlab.com> | 2017-09-30 18:54:22 +0300 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2017-09-30 18:54:22 +0300 |
commit | e499c1c39dbea505858874ee47436641df3d93d4 (patch) | |
tree | bce2333e86abfbdba7e55d15bb1fe8e432657832 /lib/google_api/auth.rb | |
parent | c30546f4aa073f44e97b49f47c57a9a89062c3c6 (diff) |
Replace reactive_cache by multipel sidekiq workers
Diffstat (limited to 'lib/google_api/auth.rb')
-rw-r--r-- | lib/google_api/auth.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/google_api/auth.rb b/lib/google_api/auth.rb new file mode 100644 index 00000000000..92787b87ac6 --- /dev/null +++ b/lib/google_api/auth.rb @@ -0,0 +1,53 @@ +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, + state: state # This is used for arbitary redirection + ) + end + + def get_token(code) + client.auth_code.get_token(code, redirect_uri: redirect_uri).token + end + + protected + + def scope + raise NotImplementedError + end + + private + + def config + Gitlab.config.omniauth.providers.find { |provider| provider.name == "google_oauth2" } + end + + 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', + authorize_url: '/o/oauth2/auth' + ) + end + end +end |