X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/74c3346a12b71f8c85e4fae6c390a9e374bc39a8..67f0d86c20139eee996816d44ef75fa52288c515:/services/api/app/models/blob.rb diff --git a/services/api/app/models/blob.rb b/services/api/app/models/blob.rb index 11fab9fb59..799279d040 100644 --- a/services/api/app/models/blob.rb +++ b/services/api/app/models/blob.rb @@ -1,4 +1,13 @@ class Blob + extend DbCurrentTime + + def initialize locator + @locator = locator + end + + def empty? + !!@locator.match(/^d41d8cd98f00b204e9800998ecf8427e(\+.*)?$/) + end # In order to get a Blob from Keep, you have to prove either # [a] you have recently written it to Keep yourself, or @@ -21,21 +30,30 @@ class Blob # The 'opts' argument should include: # [required] :key - the Arvados server-side blobstore key # [required] :api_token - user's API token - # [optional] :ttl - number of seconds before this request expires + # [optional] :ttl - number of seconds before signature should expire + # [optional] :expire - unix timestamp when signature should expire # def self.sign_locator blob_locator, opts # We only use the hash portion for signatures. blob_hash = blob_locator.split('+').first - # Generate an expiry timestamp (seconds since epoch, base 16) - timestamp = (Time.now.to_i + (opts[:ttl] || 600)).to_s(16) + # Generate an expiry timestamp (seconds after epoch, base 16) + if opts[:expire] + if opts[:ttl] + raise "Cannot specify both :ttl and :expire options" + end + timestamp = opts[:expire] + else + timestamp = db_current_time.to_i + (opts[:ttl] || 1209600) + end + timestamp_hex = timestamp.to_s(16) # => "53163cb4" # Generate a signature. signature = - generate_signature opts[:key], blob_hash, opts[:api_token], timestamp + generate_signature opts[:key], blob_hash, opts[:api_token], timestamp_hex - blob_locator + '+A' + signature + '@' + timestamp + blob_locator + '+A' + signature + '@' + timestamp_hex end # Blob.verify_signature @@ -73,7 +91,7 @@ class Blob if !timestamp.match /^[\da-f]+$/ raise Blob::InvalidSignatureError.new 'Timestamp is not a base16 number.' end - if timestamp.to_i(16) < Time.now.to_i + if timestamp.to_i(16) < db_current_time.to_i raise Blob::InvalidSignatureError.new 'Signature expiry time has passed.' end