Class: Gcloud::Storage::File::Signer
- Inherits:
-
Object
- Object
- Gcloud::Storage::File::Signer
- Defined in:
- lib/gcloud/storage/file.rb
Overview
Create a signed_url for a file.
Instance Method Summary collapse
- #apply_option_defaults(options) ⇒ Object
- #determine_issuer(options = {}) ⇒ Object
- #determine_signing_key(options = {}) ⇒ Object
-
#ext_path ⇒ Object
The external path to the file.
-
#ext_url ⇒ Object
The external url to the file.
- #generate_signature(signing_key, options = {}) ⇒ Object
- #generate_signed_url(issuer, signed_string, expires) ⇒ Object
-
#initialize(file) ⇒ Signer
constructor
:nodoc:.
- #signature_str(options) ⇒ Object
- #signed_url(options) ⇒ Object
Constructor Details
#initialize(file) ⇒ Signer
:nodoc:
512 513 514 |
# File 'lib/gcloud/storage/file.rb', line 512 def initialize file @file = file end |
Instance Method Details
#apply_option_defaults(options) ⇒ Object
528 529 530 531 532 533 |
# File 'lib/gcloud/storage/file.rb', line 528 def apply_option_defaults adjusted_expires = (Time.now.utc + ([:expires] || 300)).to_i [:expires] = adjusted_expires [:method] ||= "GET" end |
#determine_issuer(options = {}) ⇒ Object
546 547 548 549 |
# File 'lib/gcloud/storage/file.rb', line 546 def determine_issuer = {} [:issuer] || [:client_email] || @file.connection.credentials.issuer end |
#determine_signing_key(options = {}) ⇒ Object
541 542 543 544 |
# File 'lib/gcloud/storage/file.rb', line 541 def determine_signing_key = {} [:signing_key] || [:private_key] || @file.connection.credentials.signing_key end |
#ext_path ⇒ Object
The external path to the file.
518 519 520 |
# File 'lib/gcloud/storage/file.rb', line 518 def ext_path "/#{@file.bucket}/#{@file.name}" end |
#ext_url ⇒ Object
The external url to the file.
524 525 526 |
# File 'lib/gcloud/storage/file.rb', line 524 def ext_url "https://storage.googleapis.com#{ext_path}" end |
#generate_signature(signing_key, options = {}) ⇒ Object
563 564 565 566 567 568 |
# File 'lib/gcloud/storage/file.rb', line 563 def generate_signature signing_key, = {} unless signing_key.respond_to? :sign signing_key = OpenSSL::PKey::RSA.new signing_key end signing_key.sign OpenSSL::Digest::SHA256.new, signature_str() end |
#generate_signed_url(issuer, signed_string, expires) ⇒ Object
570 571 572 573 574 575 |
# File 'lib/gcloud/storage/file.rb', line 570 def generate_signed_url issuer, signed_string, expires signature = Base64.encode64(signed_string).delete("\n") "#{ext_url}?GoogleAccessId=#{CGI.escape issuer}" \ "&Expires=#{expires}" \ "&Signature=#{CGI.escape signature}" end |
#signature_str(options) ⇒ Object
535 536 537 538 539 |
# File 'lib/gcloud/storage/file.rb', line 535 def signature_str [[:method], [:content_md5], [:content_type], [:expires], ext_path].join "\n" end |
#signed_url(options) ⇒ Object
551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/gcloud/storage/file.rb', line 551 def signed_url = apply_option_defaults i = determine_issuer s = determine_signing_key fail SignedUrlUnavailable unless i && s sig = generate_signature s, generate_signed_url i, sig, [:expires] end |