2015-10-14 20:53:37 +00:00
|
|
|
# Copyright 2014, Google Inc.
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
# met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above
|
|
|
|
# copyright notice, this list of conditions and the following disclaimer
|
|
|
|
# in the documentation and/or other materials provided with the
|
|
|
|
# distribution.
|
|
|
|
# * Neither the name of Google Inc. nor the names of its
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
# this software without specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2019-02-27 16:06:41 +00:00
|
|
|
require "multi_json"
|
|
|
|
require "googleauth/credentials_loader"
|
2015-10-14 20:53:37 +00:00
|
|
|
|
|
|
|
module Google
|
|
|
|
module Auth
|
|
|
|
# Representation of an application's identity for user authorization
|
|
|
|
# flows.
|
|
|
|
class ClientId
|
2019-02-27 16:06:41 +00:00
|
|
|
INSTALLED_APP = "installed".freeze
|
|
|
|
WEB_APP = "web".freeze
|
|
|
|
CLIENT_ID = "client_id".freeze
|
|
|
|
CLIENT_SECRET = "client_secret".freeze
|
2015-10-14 20:53:37 +00:00
|
|
|
MISSING_TOP_LEVEL_ELEMENT_ERROR =
|
2016-07-13 22:44:16 +00:00
|
|
|
"Expected top level property 'installed' or 'web' to be present.".freeze
|
2015-10-14 20:53:37 +00:00
|
|
|
|
|
|
|
# Text identifier of the client ID
|
|
|
|
# @return [String]
|
|
|
|
attr_reader :id
|
|
|
|
|
|
|
|
# Secret associated with the client ID
|
|
|
|
# @return [String]
|
|
|
|
attr_reader :secret
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :default
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initialize the Client ID
|
|
|
|
#
|
|
|
|
# @param [String] id
|
|
|
|
# Text identifier of the client ID
|
|
|
|
# @param [String] secret
|
|
|
|
# Secret associated with the client ID
|
|
|
|
# @note Direction instantion is discouraged to avoid embedding IDs
|
|
|
|
# & secrets in source. See {#from_file} to load from
|
|
|
|
# `client_secrets.json` files.
|
2019-03-15 19:34:54 +00:00
|
|
|
def initialize id, secret
|
2018-07-18 20:54:03 +00:00
|
|
|
CredentialsLoader.warn_if_cloud_sdk_credentials id
|
2019-02-27 16:06:41 +00:00
|
|
|
raise "Client id can not be nil" if id.nil?
|
|
|
|
raise "Client secret can not be nil" if secret.nil?
|
2015-10-14 20:53:37 +00:00
|
|
|
@id = id
|
|
|
|
@secret = secret
|
|
|
|
end
|
|
|
|
|
2018-07-18 20:54:03 +00:00
|
|
|
# Constructs a Client ID from a JSON file downloaded from the
|
2015-10-14 20:53:37 +00:00
|
|
|
# Google Developers Console.
|
|
|
|
#
|
|
|
|
# @param [String, File] file
|
|
|
|
# Path of file to read from
|
|
|
|
# @return [Google::Auth::ClientID]
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.from_file file
|
2019-02-27 16:06:41 +00:00
|
|
|
raise "File can not be nil." if file.nil?
|
2019-03-15 19:34:54 +00:00
|
|
|
File.open file.to_s do |f|
|
2015-10-14 20:53:37 +00:00
|
|
|
json = f.read
|
2018-07-18 20:54:03 +00:00
|
|
|
config = MultiJson.load json
|
2019-03-15 19:34:54 +00:00
|
|
|
from_hash config
|
2015-10-14 20:53:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Constructs a Client ID from a previously loaded JSON file. The hash
|
|
|
|
# structure should
|
|
|
|
# match the expected JSON format.
|
|
|
|
#
|
|
|
|
# @param [hash] config
|
|
|
|
# Parsed contents of the JSON file
|
|
|
|
# @return [Google::Auth::ClientID]
|
2019-03-15 19:34:54 +00:00
|
|
|
def self.from_hash config
|
2019-02-27 16:06:41 +00:00
|
|
|
raise "Hash can not be nil." if config.nil?
|
2015-10-14 20:53:37 +00:00
|
|
|
raw_detail = config[INSTALLED_APP] || config[WEB_APP]
|
2016-07-13 22:44:16 +00:00
|
|
|
raise MISSING_TOP_LEVEL_ELEMENT_ERROR if raw_detail.nil?
|
2019-03-15 19:34:54 +00:00
|
|
|
ClientId.new raw_detail[CLIENT_ID], raw_detail[CLIENT_SECRET]
|
2015-10-14 20:53:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|