Class: Google::Auth::UserRefreshCredentials

Inherits:
Signet::OAuth2::Client show all
Extended by:
CredentialsLoader
Defined in:
lib/googleauth/user_refresh.rb

Overview

Authenticates requests using User Refresh credentials.

This class allows authorizing requests from user refresh tokens.

This the end of the result of a 3LO flow. E.g, the end result of ‘gcloud auth login’ saves a file with these contents in well known location

cf [Application Default Credentials](goo.gl/mkAHpZ)

Constant Summary collapse

TOKEN_CRED_URI =
'https://www.googleapis.com/oauth2/v3/token'

Constants included from CredentialsLoader

CredentialsLoader::ENV_VAR, CredentialsLoader::NOT_FOUND_ERROR, CredentialsLoader::WELL_KNOWN_ERROR, CredentialsLoader::WELL_KNOWN_PATH

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CredentialsLoader

from_env, from_well_known_path, make_creds, windows?

Methods inherited from Signet::OAuth2::Client

#apply, #apply!, #updater_proc

Constructor Details

#initialize(json_key_io, scope = nil) ⇒ UserRefreshCredentials

Initializes a UserRefreshCredentials.

Parameters:

  • json_key_io (IO)

    an IO from which the JSON key can be read

  • scope (string|array|nil) (defaults to: nil)

    the scope(s) to access



66
67
68
69
70
71
72
73
# File 'lib/googleauth/user_refresh.rb', line 66

def initialize(json_key_io, scope = nil)
  user_creds = self.class.read_json_key(json_key_io)
  super(token_credential_uri: TOKEN_CRED_URI,
        client_id: user_creds['client_id'],
        client_secret: user_creds['client_secret'],
        refresh_token: user_creds['refresh_token'],
        scope: scope)
end

Class Method Details

.read_json_key(json_key_io) ⇒ Object

Reads the client_id, client_secret and refresh_token fields from the JSON key.



53
54
55
56
57
58
59
60
# File 'lib/googleauth/user_refresh.rb', line 53

def self.read_json_key(json_key_io)
  json_key = MultiJson.load(json_key_io.read)
  wanted = %w(client_id client_secret refresh_token)
  wanted.each do |key|
    fail "the json is missing the #{key} field" unless json_key.key?(key)
  end
  json_key
end