5decd77261a44bdc0ec4145cf5b3fce80aa7cb2b
[arvados.git] / services / api / app / models / blob.rb
1 class Blob
2
3   # In order to get a Blob from Keep, you have to prove either
4   # [a] you have recently written it to Keep yourself, or
5   # [b] apiserver has recently decided that you should be able to read it
6   #
7   # To ensure that the requestor of a blob is authorized to read it,
8   # Keep requires clients to timestamp the blob locator with an expiry
9   # time, and to sign the timestamped locator with their API token.
10   #
11   # A signed blob locator has the form:
12   #     locator_hash +A blob_signature @ timestamp
13   # where the timestamp is a Unix time expressed as a hexadecimal value,
14   # and the blob_signature is the signed locator_hash + API token + timestamp.
15   # 
16   class InvalidSignatureError < StandardError
17   end
18
19   # Blob.sign_locator: return a signed and timestamped blob locator.
20   #
21   # The 'opts' argument should include:
22   #   [required] :key       - the Arvados server-side blobstore key
23   #   [required] :api_token - user's API token
24   #   [optional] :ttl       - number of seconds before signature should expire
25   #   [optional] :expire    - unix timestamp when signature should expire
26   #
27   def self.sign_locator blob_locator, opts
28     # We only use the hash portion for signatures.
29     blob_hash = blob_locator.split('+').first
30
31     # Generate an expiry timestamp (seconds after epoch, base 16)
32     if opts[:expire]
33       if opts[:ttl]
34         raise "Cannot specify both :ttl and :expire options"
35       end
36       timestamp = opts[:expire]
37     else
38       timestamp = Time.now.to_i + (opts[:ttl] || 600)
39     end
40     timestamp_hex = timestamp.to_s(16)
41     # => "53163cb4"
42
43     # Generate a signature.
44     signature =
45       generate_signature opts[:key], blob_hash, opts[:api_token], timestamp_hex
46
47     blob_locator + '+A' + signature + '@' + timestamp_hex
48   end
49
50   # Blob.verify_signature
51   #   Safely verify the signature on a blob locator.
52   #   Return value: true if the locator has a valid signature, false otherwise
53   #   Arguments: signed_blob_locator, opts
54   #
55   def self.verify_signature *args
56     begin
57       self.verify_signature! *args
58       true
59     rescue Blob::InvalidSignatureError
60       false
61     end
62   end
63
64   # Blob.verify_signature!
65   #   Verify the signature on a blob locator.
66   #   Return value: true if the locator has a valid signature
67   #   Arguments: signed_blob_locator, opts
68   #   Exceptions:
69   #     Blob::InvalidSignatureError if the blob locator does not include a
70   #     valid signature
71   #
72   def self.verify_signature! signed_blob_locator, opts
73     blob_hash = signed_blob_locator.split('+').first
74     given_signature, timestamp = signed_blob_locator.
75       split('+A').last.
76       split('+').first.
77       split('@')
78
79     if !timestamp
80       raise Blob::InvalidSignatureError.new 'No signature provided.'
81     end
82     if !timestamp.match /^[\da-f]+$/
83       raise Blob::InvalidSignatureError.new 'Timestamp is not a base16 number.'
84     end
85     if timestamp.to_i(16) < Time.now.to_i
86       raise Blob::InvalidSignatureError.new 'Signature expiry time has passed.'
87     end
88
89     my_signature =
90       generate_signature opts[:key], blob_hash, opts[:api_token], timestamp
91
92     if my_signature != given_signature
93       raise Blob::InvalidSignatureError.new 'Signature is invalid.'
94     end
95
96     true
97   end
98
99   def self.generate_signature key, blob_hash, api_token, timestamp
100     OpenSSL::HMAC.hexdigest('sha1', key,
101                             [blob_hash,
102                              api_token,
103                              timestamp].join('@'))
104   end
105 end