Class: TentClient::Middleware::Authentication

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/tent-client/middleware/authentication.rb

Constant Summary collapse

AUTHORIZATION_HEADER =
'Authorization'.freeze
CONTENT_TYPE_HEADER =
'Content-Type'.freeze
MAX_RETRIES =
1.freeze

Instance Method Summary collapse

Constructor Details

#initialize(app, credentials, options = {}) ⇒ Authentication

Returns a new instance of Authentication.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tent-client/middleware/authentication.rb', line 12

def initialize(app, credentials, options = {})
  super(app)

  @credentials = {
    :id => credentials[:id],
    :key => credentials[:hawk_key],
    :algorithm => credentials[:hawk_algorithm]
  } if credentials

  @ts_skew = options[:ts_skew] || 0
  @update_ts_skew = options[:update_ts_skew] || lambda {}
  @ts_skew_retry_enabled = options[:ts_skew_retry_enabled]
  @retry_count = 0
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/tent-client/middleware/authentication.rb', line 27

def call(env)
  set_auth_header(env)
  res = @app.call(env)

  tsm_header = res.env[:response_headers]['WWW-Authenticate']

  if tsm_header && (tsm_header =~ /tsm/) && (skew = timestamp_skew_from_header(tsm_header)) && @retry_count < MAX_RETRIES
    @update_ts_skew.call(skew)
    @ts_skew = skew

    @retry_count += 1

    return call(env) if @ts_skew_retry_enabled
  end

  res
end