Class: Google::Cloud::Storage::File::Signer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, path, service) ⇒ Signer

Returns a new instance of Signer.



718
719
720
721
722
# File 'lib/google/cloud/storage/file.rb', line 718

def initialize bucket, path, service
  @bucket = bucket
  @path = path
  @service = service
end

Class Method Details

.from_bucket(bucket, path) ⇒ Object



728
729
730
# File 'lib/google/cloud/storage/file.rb', line 728

def self.from_bucket bucket, path
  new bucket.name, path, bucket.service
end

.from_file(file) ⇒ Object



724
725
726
# File 'lib/google/cloud/storage/file.rb', line 724

def self.from_file file
  new file.bucket, file.name, file.service
end

Instance Method Details

#apply_option_defaults(options) ⇒ Object



744
745
746
747
748
749
# File 'lib/google/cloud/storage/file.rb', line 744

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



762
763
764
765
# File 'lib/google/cloud/storage/file.rb', line 762

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

#determine_signing_key(options = {}) ⇒ Object



757
758
759
760
# File 'lib/google/cloud/storage/file.rb', line 757

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

#ext_pathObject

The external path to the file.



734
735
736
# File 'lib/google/cloud/storage/file.rb', line 734

def ext_path
  URI.escape "/#{@bucket}/#{@path}"
end

#ext_urlObject

The external url to the file.



740
741
742
# File 'lib/google/cloud/storage/file.rb', line 740

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

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



779
780
781
782
783
784
# File 'lib/google/cloud/storage/file.rb', line 779

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



786
787
788
789
790
791
# File 'lib/google/cloud/storage/file.rb', line 786

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

#signature_str(options) ⇒ Object



751
752
753
754
755
# File 'lib/google/cloud/storage/file.rb', line 751

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

#signed_url(options) ⇒ Object



767
768
769
770
771
772
773
774
775
776
777
# File 'lib/google/cloud/storage/file.rb', line 767

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