commit
0b1dbb67fe
|
@ -490,7 +490,7 @@ module Google
|
||||||
else
|
else
|
||||||
check_cached_certs = lambda do
|
check_cached_certs = lambda do
|
||||||
valid = false
|
valid = false
|
||||||
for key, cert in @certificates
|
for _key, cert in @certificates
|
||||||
begin
|
begin
|
||||||
self.authorization.decoded_id_token(cert.public_key)
|
self.authorization.decoded_id_token(cert.public_key)
|
||||||
valid = true
|
valid = true
|
||||||
|
|
|
@ -25,44 +25,44 @@ module Google
|
||||||
#
|
#
|
||||||
# @param [String] keyfile
|
# @param [String] keyfile
|
||||||
# Path of the PKCS12 file to load. If not a path to an actual file,
|
# Path of the PKCS12 file to load. If not a path to an actual file,
|
||||||
# assumes the string is the content of the file itself.
|
# assumes the string is the content of the file itself.
|
||||||
# @param [String] passphrase
|
# @param [String] passphrase
|
||||||
# Passphrase for unlocking the private key
|
# Passphrase for unlocking the private key
|
||||||
#
|
#
|
||||||
# @return [OpenSSL::PKey] The private key for signing assertions.
|
# @return [OpenSSL::PKey] The private key for signing assertions.
|
||||||
def self.load_from_pkcs12(keyfile, passphrase)
|
def self.load_from_pkcs12(keyfile, passphrase)
|
||||||
load_key(keyfile, passphrase) do |content, passphrase|
|
load_key(keyfile, passphrase) do |content, pass_phrase|
|
||||||
OpenSSL::PKCS12.new(content, passphrase).key
|
OpenSSL::PKCS12.new(content, pass_phrase).key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Loads a key from a PEM file.
|
# Loads a key from a PEM file.
|
||||||
#
|
#
|
||||||
# @param [String] keyfile
|
# @param [String] keyfile
|
||||||
# Path of the PEM file to load. If not a path to an actual file,
|
# Path of the PEM file to load. If not a path to an actual file,
|
||||||
# assumes the string is the content of the file itself.
|
# assumes the string is the content of the file itself.
|
||||||
# @param [String] passphrase
|
# @param [String] passphrase
|
||||||
# Passphrase for unlocking the private key
|
# Passphrase for unlocking the private key
|
||||||
#
|
#
|
||||||
# @return [OpenSSL::PKey] The private key for signing assertions.
|
# @return [OpenSSL::PKey] The private key for signing assertions.
|
||||||
#
|
#
|
||||||
def self.load_from_pem(keyfile, passphrase)
|
def self.load_from_pem(keyfile, passphrase)
|
||||||
load_key(keyfile, passphrase) do | content, passphrase|
|
load_key(keyfile, passphrase) do | content, pass_phrase|
|
||||||
OpenSSL::PKey::RSA.new(content, passphrase)
|
OpenSSL::PKey::RSA.new(content, pass_phrase)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
##
|
##
|
||||||
# Helper for loading keys from file or memory. Accepts a block
|
# Helper for loading keys from file or memory. Accepts a block
|
||||||
# to handle the specific file format.
|
# to handle the specific file format.
|
||||||
#
|
#
|
||||||
# @param [String] keyfile
|
# @param [String] keyfile
|
||||||
# Path of thefile to load. If not a path to an actual file,
|
# Path of thefile to load. If not a path to an actual file,
|
||||||
# assumes the string is the content of the file itself.
|
# assumes the string is the content of the file itself.
|
||||||
# @param [String] passphrase
|
# @param [String] passphrase
|
||||||
# Passphrase for unlocking the private key
|
# Passphrase for unlocking the private key
|
||||||
#
|
#
|
||||||
|
@ -86,8 +86,8 @@ module Google
|
||||||
block.call(content, passphrase)
|
block.call(content, passphrase)
|
||||||
rescue OpenSSL::OpenSSLError
|
rescue OpenSSL::OpenSSLError
|
||||||
raise ArgumentError.new("Invalid keyfile or passphrase")
|
raise ArgumentError.new("Invalid keyfile or passphrase")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -38,6 +38,7 @@ module Google
|
||||||
# @params [Object] Storage object
|
# @params [Object] Storage object
|
||||||
def initialize(store)
|
def initialize(store)
|
||||||
@store= store
|
@store= store
|
||||||
|
@authorization = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -35,7 +35,7 @@ module Google
|
||||||
|
|
||||||
##
|
##
|
||||||
# Initialize the call response
|
# Initialize the call response
|
||||||
#
|
#
|
||||||
# @param [String] call_id
|
# @param [String] call_id
|
||||||
# UUID of the original call
|
# UUID of the original call
|
||||||
# @param [Fixnum] status
|
# @param [Fixnum] status
|
||||||
|
@ -48,7 +48,7 @@ module Google
|
||||||
@call_id, @status, @headers, @body = call_id, status, headers, body
|
@call_id, @status, @headers, @body = call_id, status, headers, body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Wraps multiple API calls into a single over-the-wire HTTP request.
|
# Wraps multiple API calls into a single over-the-wire HTTP request.
|
||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
|
@ -58,7 +58,7 @@ module Google
|
||||||
# batch = Google::APIClient::BatchRequest.new do |result|
|
# batch = Google::APIClient::BatchRequest.new do |result|
|
||||||
# puts result.data
|
# puts result.data
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/foo' })
|
# batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/foo' })
|
||||||
# batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' })
|
# batch.add(:api_method => urlshortener.url.insert, :body_object => { 'longUrl' => 'http://example.com/bar' })
|
||||||
#
|
#
|
||||||
|
@ -80,16 +80,17 @@ module Google
|
||||||
# Callback for every call's response. Won't be called if a call defined
|
# Callback for every call's response. Won't be called if a call defined
|
||||||
# a callback of its own.
|
# a callback of its own.
|
||||||
#
|
#
|
||||||
# @return [Google::APIClient::BatchRequest]
|
# @return [Google::APIClient::BatchRequest]
|
||||||
# The constructed object.
|
# The constructed object.
|
||||||
#
|
#
|
||||||
# @yield [Google::APIClient::Result]
|
# @yield [Google::APIClient::Result]
|
||||||
# block to be called when result ready
|
# block to be called when result ready
|
||||||
def initialize(options = {}, &block)
|
def initialize(options = {}, &block)
|
||||||
@calls = []
|
@calls = []
|
||||||
|
@global_callback = nil
|
||||||
@global_callback = block if block_given?
|
@global_callback = block if block_given?
|
||||||
@last_auto_id = 0
|
@last_auto_id = 0
|
||||||
|
|
||||||
@base_id = SecureRandom.uuid
|
@base_id = SecureRandom.uuid
|
||||||
|
|
||||||
options[:uri] ||= 'https://www.googleapis.com/batch'
|
options[:uri] ||= 'https://www.googleapis.com/batch'
|
||||||
|
@ -104,7 +105,7 @@ module Google
|
||||||
# automatically be generated, avoiding collisions. If duplicate call IDs
|
# automatically be generated, avoiding collisions. If duplicate call IDs
|
||||||
# are provided, an error will be thrown.
|
# are provided, an error will be thrown.
|
||||||
#
|
#
|
||||||
# @param [Hash, Google::APIClient::Request] call
|
# @param [Hash, Google::APIClient::Request] call
|
||||||
# the call to be added.
|
# the call to be added.
|
||||||
# @param [String] call_id
|
# @param [String] call_id
|
||||||
# the ID to be used for this call. Must be unique
|
# the ID to be used for this call. Must be unique
|
||||||
|
@ -126,7 +127,7 @@ module Google
|
||||||
'A call with this ID already exists: %s' % call_id
|
'A call with this ID already exists: %s' % call_id
|
||||||
end
|
end
|
||||||
callback = block_given? ? block : @global_callback
|
callback = block_given? ? block : @global_callback
|
||||||
@calls << [call_id, call, callback]
|
@calls << [call_id, call, callback]
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -165,12 +166,12 @@ module Google
|
||||||
if @calls.nil? || @calls.empty?
|
if @calls.nil? || @calls.empty?
|
||||||
raise BatchError, 'Cannot make an empty batch request'
|
raise BatchError, 'Cannot make an empty batch request'
|
||||||
end
|
end
|
||||||
parts = @calls.map {|(call_id, call, callback)| serialize_call(call_id, call)}
|
parts = @calls.map {|(call_id, call, _callback)| serialize_call(call_id, call)}
|
||||||
build_multipart(parts, 'multipart/mixed', BATCH_BOUNDARY)
|
build_multipart(parts, 'multipart/mixed', BATCH_BOUNDARY)
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -183,7 +184,7 @@ module Google
|
||||||
# @param [Hash] headers
|
# @param [Hash] headers
|
||||||
# the hash of headers and their values.
|
# the hash of headers and their values.
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
# the value of the desired header.
|
# the value of the desired header.
|
||||||
def find_header(name, headers)
|
def find_header(name, headers)
|
||||||
_, header = headers.detect do |h, v|
|
_, header = headers.detect do |h, v|
|
||||||
|
@ -197,7 +198,7 @@ module Google
|
||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
# the new, unique ID.
|
# the new, unique ID.
|
||||||
def new_id
|
def new_id
|
||||||
@last_auto_id += 1
|
@last_auto_id += 1
|
||||||
|
@ -216,7 +217,7 @@ module Google
|
||||||
# @param [String] header
|
# @param [String] header
|
||||||
# Content-ID header value.
|
# Content-ID header value.
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
# The extracted ID value.
|
# The extracted ID value.
|
||||||
def header_to_id(header)
|
def header_to_id(header)
|
||||||
if !header.start_with?('<') || !header.end_with?('>') ||
|
if !header.start_with?('<') || !header.end_with?('>') ||
|
||||||
|
@ -224,7 +225,7 @@ module Google
|
||||||
raise BatchError, 'Invalid value for Content-ID: "%s"' % header
|
raise BatchError, 'Invalid value for Content-ID: "%s"' % header
|
||||||
end
|
end
|
||||||
|
|
||||||
base, call_id = header[1...-1].split('+')
|
_base, call_id = header[1...-1].split('+')
|
||||||
return Addressable::URI.unencode(call_id)
|
return Addressable::URI.unencode(call_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -236,7 +237,7 @@ module Google
|
||||||
# @param [String] response
|
# @param [String] response
|
||||||
# the response to parse.
|
# the response to parse.
|
||||||
#
|
#
|
||||||
# @return [Array<Hash>, String]
|
# @return [Array<Hash>, String]
|
||||||
# the headers and the body, separately.
|
# the headers and the body, separately.
|
||||||
def split_headers_and_body(response)
|
def split_headers_and_body(response)
|
||||||
headers = {}
|
headers = {}
|
||||||
|
@ -263,12 +264,12 @@ module Google
|
||||||
# @param [String] call_response
|
# @param [String] call_response
|
||||||
# the request to deserialize.
|
# the request to deserialize.
|
||||||
#
|
#
|
||||||
# @return [Google::APIClient::BatchedCallResponse]
|
# @return [Google::APIClient::BatchedCallResponse]
|
||||||
# the parsed and converted response.
|
# the parsed and converted response.
|
||||||
def deserialize_call_response(call_response)
|
def deserialize_call_response(call_response)
|
||||||
outer_headers, outer_body = split_headers_and_body(call_response)
|
outer_headers, outer_body = split_headers_and_body(call_response)
|
||||||
status_line, payload = outer_body.split("\n", 2)
|
status_line, payload = outer_body.split("\n", 2)
|
||||||
protocol, status, reason = status_line.split(' ', 3)
|
_protocol, status, _reason = status_line.split(' ', 3)
|
||||||
|
|
||||||
headers, body = split_headers_and_body(payload)
|
headers, body = split_headers_and_body(payload)
|
||||||
content_id = find_header('Content-ID', outer_headers)
|
content_id = find_header('Content-ID', outer_headers)
|
||||||
|
@ -284,7 +285,7 @@ module Google
|
||||||
# @param [Google::APIClient::Request] call
|
# @param [Google::APIClient::Request] call
|
||||||
# the call to serialize.
|
# the call to serialize.
|
||||||
#
|
#
|
||||||
# @return [Faraday::UploadIO]
|
# @return [Faraday::UploadIO]
|
||||||
# the serialized request
|
# the serialized request
|
||||||
def serialize_call(call_id, call)
|
def serialize_call(call_id, call)
|
||||||
method, uri, headers, body = call.to_http_request
|
method, uri, headers, body = call.to_http_request
|
||||||
|
@ -293,7 +294,7 @@ module Google
|
||||||
request << "\r\n%s: %s" % [header, value]
|
request << "\r\n%s: %s" % [header, value]
|
||||||
end
|
end
|
||||||
if body
|
if body
|
||||||
# TODO - CompositeIO if body is a stream
|
# TODO - CompositeIO if body is a stream
|
||||||
request << "\r\n\r\n"
|
request << "\r\n\r\n"
|
||||||
if body.respond_to?(:read)
|
if body.respond_to?(:read)
|
||||||
request << body.read
|
request << body.read
|
||||||
|
@ -303,7 +304,7 @@ module Google
|
||||||
end
|
end
|
||||||
Faraday::UploadIO.new(StringIO.new(request), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
|
Faraday::UploadIO.new(StringIO.new(request), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Convert an id to a Content-ID header value.
|
# Convert an id to a Content-ID header value.
|
||||||
#
|
#
|
||||||
|
@ -319,7 +320,7 @@ module Google
|
||||||
def id_to_header(call_id)
|
def id_to_header(call_id)
|
||||||
return '<%s+%s>' % [@base_id, Addressable::URI.encode(call_id)]
|
return '<%s+%s>' % [@base_id, Addressable::URI.encode(call_id)]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -27,7 +27,7 @@ module Google
|
||||||
# Represents an API request.
|
# Represents an API request.
|
||||||
class Request
|
class Request
|
||||||
include Google::APIClient::Logging
|
include Google::APIClient::Logging
|
||||||
|
|
||||||
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
|
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
|
||||||
|
|
||||||
# @return [Hash] Request parameters
|
# @return [Hash] Request parameters
|
||||||
|
@ -157,7 +157,7 @@ module Google
|
||||||
# @return [Google::APIClient::Result]
|
# @return [Google::APIClient::Result]
|
||||||
# result of API request
|
# result of API request
|
||||||
def send(connection, is_retry = false)
|
def send(connection, is_retry = false)
|
||||||
self.body.rewind if is_retry && self.body.respond_to?(:rewind)
|
self.body.rewind if is_retry && self.body.respond_to?(:rewind)
|
||||||
env = self.to_env(connection)
|
env = self.to_env(connection)
|
||||||
logger.debug { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" }
|
logger.debug { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" }
|
||||||
http_response = connection.app.call(env)
|
http_response = connection.app.call(env)
|
||||||
|
@ -244,7 +244,7 @@ module Google
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
request_env = http_request.to_env(connection)
|
http_request.to_env(connection)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -124,7 +124,7 @@ module Google
|
||||||
# Read the entire cache file from disk.
|
# Read the entire cache file from disk.
|
||||||
# Will avoid reading if there have been no changes.
|
# Will avoid reading if there have been no changes.
|
||||||
def read_file
|
def read_file
|
||||||
if !File.exists? @file_path
|
if !File.exist? @file_path
|
||||||
@cache = nil
|
@cache = nil
|
||||||
else
|
else
|
||||||
# Check for changes after our last read or write.
|
# Check for changes after our last read or write.
|
||||||
|
|
Loading…
Reference in New Issue