9 !!@locator.match(/^d41d8cd98f00b204e9800998ecf8427e(\+.*)?$/)
12 # In order to get a Blob from Keep, you have to prove either
13 # [a] you have recently written it to Keep yourself, or
14 # [b] apiserver has recently decided that you should be able to read it
16 # To ensure that the requestor of a blob is authorized to read it,
17 # Keep requires clients to timestamp the blob locator with an expiry
18 # time, and to sign the timestamped locator with their API token.
20 # A signed blob locator has the form:
21 # locator_hash +A blob_signature @ timestamp
22 # where the timestamp is a Unix time expressed as a hexadecimal value,
23 # and the blob_signature is the signed locator_hash + API token + timestamp.
25 class InvalidSignatureError < StandardError
28 # Blob.sign_locator: return a signed and timestamped blob locator.
30 # The 'opts' argument should include:
31 # [required] :key - the Arvados server-side blobstore key
32 # [required] :api_token - user's API token
33 # [optional] :ttl - number of seconds before signature should expire
34 # [optional] :expire - unix timestamp when signature should expire
36 def self.sign_locator blob_locator, opts
37 # We only use the hash portion for signatures.
38 blob_hash = blob_locator.split('+').first
40 # Generate an expiry timestamp (seconds after epoch, base 16)
43 raise "Cannot specify both :ttl and :expire options"
45 timestamp = opts[:expire]
47 timestamp = db_current_time.to_i + (opts[:ttl] || 1209600)
49 timestamp_hex = timestamp.to_s(16)
52 # Generate a signature.
54 generate_signature opts[:key], blob_hash, opts[:api_token], timestamp_hex
56 blob_locator + '+A' + signature + '@' + timestamp_hex
59 # Blob.verify_signature
60 # Safely verify the signature on a blob locator.
61 # Return value: true if the locator has a valid signature, false otherwise
62 # Arguments: signed_blob_locator, opts
64 def self.verify_signature *args
66 self.verify_signature! *args
68 rescue Blob::InvalidSignatureError
73 # Blob.verify_signature!
74 # Verify the signature on a blob locator.
75 # Return value: true if the locator has a valid signature
76 # Arguments: signed_blob_locator, opts
78 # Blob::InvalidSignatureError if the blob locator does not include a
81 def self.verify_signature! signed_blob_locator, opts
82 blob_hash = signed_blob_locator.split('+').first
83 given_signature, timestamp = signed_blob_locator.
89 raise Blob::InvalidSignatureError.new 'No signature provided.'
91 if !timestamp.match /^[\da-f]+$/
92 raise Blob::InvalidSignatureError.new 'Timestamp is not a base16 number.'
94 if timestamp.to_i(16) < db_current_time.to_i
95 raise Blob::InvalidSignatureError.new 'Signature expiry time has passed.'
99 generate_signature opts[:key], blob_hash, opts[:api_token], timestamp
101 if my_signature != given_signature
102 raise Blob::InvalidSignatureError.new 'Signature is invalid.'
108 def self.generate_signature key, blob_hash, api_token, timestamp
109 OpenSSL::HMAC.hexdigest('sha1', key,
112 timestamp].join('@'))