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

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Signer

Returns a new instance of Signer.



696
697
698
# File 'lib/gcloud/storage/file.rb', line 696

def initialize file
  @file = file
end

Instance Method Details

#apply_option_defaults(options) ⇒ Object



712
713
714
715
716
717
# File 'lib/gcloud/storage/file.rb', line 712

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



730
731
732
733
# File 'lib/gcloud/storage/file.rb', line 730

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

#determine_signing_key(options = {}) ⇒ Object



725
726
727
728
# File 'lib/gcloud/storage/file.rb', line 725

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.



702
703
704
# File 'lib/gcloud/storage/file.rb', line 702

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

#ext_urlObject

The external url to the file.



708
709
710
# File 'lib/gcloud/storage/file.rb', line 708

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

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



747
748
749
750
751
752
# File 'lib/gcloud/storage/file.rb', line 747

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



754
755
756
757
758
759
# File 'lib/gcloud/storage/file.rb', line 754

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



719
720
721
722
723
# File 'lib/gcloud/storage/file.rb', line 719

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

#signed_url(options) ⇒ Object



735
736
737
738
739
740
741
742
743
744
745
# File 'lib/gcloud/storage/file.rb', line 735

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