Class: Gcloud::Storage::File::Signer

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/storage/file.rb

Overview

Create a signed_url for a file.

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Signer

:nodoc:



505
506
507
# File 'lib/gcloud/storage/file.rb', line 505

def initialize file
  @file = file
end

Instance Method Details

#apply_option_defaults(options) ⇒ Object



521
522
523
524
525
526
# File 'lib/gcloud/storage/file.rb', line 521

def apply_option_defaults options
  adjusted_expires = (Time.now.utc + (options[:expires] || 300)).to_i
  options[:expires] = adjusted_expires
  options[:method]  ||= "GET"
  options
end

#determine_issuer(options = {}) ⇒ Object



539
540
541
542
# File 'lib/gcloud/storage/file.rb', line 539

def determine_issuer options = {}
  options[:issuer] || options[:client_email] ||
    @file.connection.credentials.issuer
end

#determine_signing_key(options = {}) ⇒ Object



534
535
536
537
# File 'lib/gcloud/storage/file.rb', line 534

def determine_signing_key options = {}
  options[:signing_key] || options[:private_key] ||
    @file.connection.credentials.signing_key
end

#ext_pathObject

The external path to the file.



511
512
513
# File 'lib/gcloud/storage/file.rb', line 511

def ext_path
  "/#{@file.bucket}/#{@file.name}"
end

#ext_urlObject

The external url to the file.



517
518
519
# File 'lib/gcloud/storage/file.rb', line 517

def ext_url
  "https://storage.googleapis.com#{ext_path}"
end

#generate_signature(signing_key, options = {}) ⇒ Object



556
557
558
559
560
561
# File 'lib/gcloud/storage/file.rb', line 556

def generate_signature signing_key, options = {}
  unless signing_key.respond_to? :sign
    signing_key = OpenSSL::PKey::RSA.new signing_key
  end
  signing_key.sign OpenSSL::Digest::SHA256.new, signature_str(options)
end

#generate_signed_url(issuer, signed_string, expires) ⇒ Object



563
564
565
566
567
568
# File 'lib/gcloud/storage/file.rb', line 563

def generate_signed_url issuer, signed_string, expires
  signature = Base64.encode64(signed_string).gsub("\n", "")
  "#{ext_url}?GoogleAccessId=#{CGI.escape issuer}" \
            "&Expires=#{expires}" \
            "&Signature=#{CGI.escape signature}"
end

#signature_str(options) ⇒ Object



528
529
530
531
532
# File 'lib/gcloud/storage/file.rb', line 528

def signature_str options
  [options[:method], options[:content_md5],
   options[:content_type], options[:expires],
   ext_path].join "\n"
end

#signed_url(options) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
# File 'lib/gcloud/storage/file.rb', line 544

def signed_url options
  options = apply_option_defaults options

  i = determine_issuer options
  s = determine_signing_key options

  fail SignedUrlUnavailable unless i && s

  sig = generate_signature s, options
  generate_signed_url i, sig, options[:expires]
end