faraday_middleware.rb 2.38 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
class Acme::Client::FaradayMiddleware < Faraday::Middleware
  attr_reader :env, :response, :client

6
  CONTENT_TYPE = 'application/jose+json'
7

8
  def initialize(app, client:, mode:)
9
10
    super(app)
    @client = client
11
    @mode = mode
12
13
14
15
  end

  def call(env)
    @env = env
16
17
18
19
20
21
22
    @env[:request_headers]['User-Agent'] = Acme::Client::USER_AGENT
    @env[:request_headers]['Content-Type'] = CONTENT_TYPE

    if @env.method != :get
      @env.body = client.jwk.jws(header: jws_header, payload: env.body)
    end

23
    @app.call(env).on_complete { |response_env| on_complete(response_env) }
24
  rescue Faraday::TimeoutError, Faraday::ConnectionFailed
25
    raise Acme::Client::Error::Timeout
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  end

  def on_complete(env)
    @env = env

    raise_on_not_found!
    store_nonce
    env.body = decode_body
    env.response_headers['Link'] = decode_link_headers

    return if env.success?

    raise_on_error!
  end

  private

43
44
45
46
47
48
  def jws_header
    headers = { nonce: pop_nonce, url: env.url.to_s }
    headers[:kid] = client.kid if @mode == :kid
    headers
  end

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  def raise_on_not_found!
    raise Acme::Client::Error::NotFound, env.url.to_s if env.status == 404
  end

  def raise_on_error!
    raise error_class, error_message
  end

  def error_message
    if env.body.is_a? Hash
      env.body['detail']
    else
      "Error message: #{env.body}"
    end
  end

  def error_class
66
    Acme::Client::Error::ACME_ERRORS.fetch(error_name, Acme::Client::Error)
67
68
  end

69
  def error_name
70
71
72
    return unless env.body.is_a?(Hash)
    return unless env.body.key?('type')
    env.body['type']
73
74
  end

75
  def decode_body
76
    content_type = env.response_headers['Content-Type'].to_s
77

78
    if content_type.start_with?('application/json', 'application/problem+json')
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
      JSON.load(env.body)
    else
      env.body
    end
  end

  LINK_MATCH = /<(.*?)>;rel="([\w-]+)"/

  def decode_link_headers
    return unless env.response_headers.key?('Link')
    link_header = env.response_headers['Link']

    links = link_header.split(', ').map { |entry|
      _, link, name = *entry.match(LINK_MATCH)
      [name, link]
    }

    Hash[*links.flatten]
  end

  def store_nonce
100
101
    nonce = env.response_headers['replay-nonce']
    nonces << nonce if nonce
102
103
104
105
106
107
  end

  def pop_nonce
    if nonces.empty?
      get_nonce
    end
108
109

    nonces.pop
110
111
112
  end

  def get_nonce
113
    client.get_nonce
114
115
116
117
118
119
  end

  def nonces
    client.nonces
  end
end