Class: Google::Cloud::Storage::File::Signer
- Inherits:
-
Object
- Object
- Google::Cloud::Storage::File::Signer
- Defined in:
- lib/google/cloud/storage/file.rb
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
A new instance of Signer.
- #signature_str(options) ⇒ Object
- #signed_url(options) ⇒ Object
Constructor Details
#initialize(file) ⇒ Signer
Returns a new instance of Signer.
733 734 735 |
# File 'lib/google/cloud/storage/file.rb', line 733 def initialize file @file = file end |
Instance Method Details
#apply_option_defaults(options) ⇒ Object
749 750 751 752 753 754 |
# File 'lib/google/cloud/storage/file.rb', line 749 def apply_option_defaults adjusted_expires = (Time.now.utc + ([:expires] || 300)).to_i [:expires] = adjusted_expires [:method] ||= "GET" end |
#determine_issuer(options = {}) ⇒ Object
767 768 769 770 |
# File 'lib/google/cloud/storage/file.rb', line 767 def determine_issuer = {} [:issuer] || [:client_email] || @file.service.credentials.issuer end |
#determine_signing_key(options = {}) ⇒ Object
762 763 764 765 |
# File 'lib/google/cloud/storage/file.rb', line 762 def determine_signing_key = {} [:signing_key] || [:private_key] || @file.service.credentials.signing_key end |
#ext_path ⇒ Object
The external path to the file.
739 740 741 |
# File 'lib/google/cloud/storage/file.rb', line 739 def ext_path "/#{@file.bucket}/#{@file.name}" end |
#ext_url ⇒ Object
The external url to the file.
745 746 747 |
# File 'lib/google/cloud/storage/file.rb', line 745 def ext_url "https://storage.googleapis.com#{ext_path}" end |
#generate_signature(signing_key, options = {}) ⇒ Object
784 785 786 787 788 789 |
# File 'lib/google/cloud/storage/file.rb', line 784 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
791 792 793 794 795 796 |
# File 'lib/google/cloud/storage/file.rb', line 791 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
756 757 758 759 760 |
# File 'lib/google/cloud/storage/file.rb', line 756 def signature_str [[:method], [:content_md5], [:content_type], [:expires], ext_path].join "\n" end |
#signed_url(options) ⇒ Object
772 773 774 775 776 777 778 779 780 781 782 |
# File 'lib/google/cloud/storage/file.rb', line 772 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 |