Class: Google::Auth::ClientId

Inherits:
Object
  • Object
show all
Defined in:
lib/googleauth/client_id.rb

Overview

Representation of an application’s identity for user authorization flows.

Constant Summary collapse

INSTALLED_APP =
'installed'.freeze
WEB_APP =
'web'.freeze
CLIENT_ID =
'client_id'.freeze
CLIENT_SECRET =
'client_secret'.freeze
MISSING_TOP_LEVEL_ELEMENT_ERROR =
"Expected top level property 'installed' or 'web' to be present.".freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, secret) ⇒ ClientId

Note:

Direction instantion is discouraged to avoid embedding IDs & secrets in source. See #from_file to load from ‘client_secrets.json` files.

Initialize the Client ID

Parameters:

  • id (String)

    Text identifier of the client ID

  • secret (String)

    Secret associated with the client ID



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

def initialize(id, secret)
  CredentialsLoader.warn_if_cloud_sdk_credentials id
  raise 'Client id can not be nil' if id.nil?
  raise 'Client secret can not be nil' if secret.nil?
  @id = id
  @secret = secret
end

Class Attribute Details

.defaultObject

Returns the value of attribute default.



54
55
56
# File 'lib/googleauth/client_id.rb', line 54

def default
  @default
end

Instance Attribute Details

#idString (readonly)

Text identifier of the client ID

Returns:

  • (String)


47
48
49
# File 'lib/googleauth/client_id.rb', line 47

def id
  @id
end

#secretString (readonly)

Secret associated with the client ID

Returns:

  • (String)


51
52
53
# File 'lib/googleauth/client_id.rb', line 51

def secret
  @secret
end

Class Method Details

.from_file(file) ⇒ Google::Auth::ClientID

Constructs a Client ID from a JSON file downloaded from the Google Developers Console.

Parameters:

  • file (String, File)

    Path of file to read from

Returns:

  • (Google::Auth::ClientID)


80
81
82
83
84
85
86
87
# File 'lib/googleauth/client_id.rb', line 80

def self.from_file(file)
  raise 'File can not be nil.' if file.nil?
  File.open(file.to_s) do |f|
    json = f.read
    config = MultiJson.load json
    from_hash(config)
  end
end

.from_hash(config) ⇒ Google::Auth::ClientID

Constructs a Client ID from a previously loaded JSON file. The hash structure should match the expected JSON format.

Parameters:

  • config (hash)

    Parsed contents of the JSON file

Returns:

  • (Google::Auth::ClientID)

Raises:



96
97
98
99
100
101
# File 'lib/googleauth/client_id.rb', line 96

def self.from_hash(config)
  raise 'Hash can not be nil.' if config.nil?
  raw_detail = config[INSTALLED_APP] || config[WEB_APP]
  raise MISSING_TOP_LEVEL_ELEMENT_ERROR if raw_detail.nil?
  ClientId.new(raw_detail[CLIENT_ID], raw_detail[CLIENT_SECRET])
end