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

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

6
7
8
  repo_url = 'https://github.com/unixcharles/acme-client'
  USER_AGENT = "Acme::Client v#{Acme::Client::VERSION} (#{repo_url})".freeze

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

  def call(env)
    @env = env
16
17
    @env[:request_headers]['User-Agent'] = USER_AGENT
    @env.body = client.jwk.jws(header: { nonce: pop_nonce }, payload: env.body)
18
    @app.call(env).on_complete { |response_env| on_complete(response_env) }
19
20
  rescue Faraday::TimeoutError
    raise Acme::Client::Error::Timeout
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  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

  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
55
56
    if error_name && !error_name.empty? && Acme::Client::Error.const_defined?(error_name)
      Object.const_get("Acme::Client::Error::#{error_name}")
57
58
59
60
61
    else
      Acme::Client::Error
    end
  end

62
63
64
65
66
67
68
69
70
71
72
73
74
  def error_name
    @error_name ||= begin
      return unless env.body.is_a?(Hash)
      return unless env.body.key?('type')

      error_type_to_klass env.body['type']
    end
  end

  def error_type_to_klass(type)
    type.gsub('urn:acme:error:', '').split(/[_-]/).map { |type_part| type_part[0].upcase + type_part[1..-1] }.join
  end

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  def decode_body
    content_type = env.response_headers['Content-Type']

    if content_type == 'application/json' || content_type == 'application/problem+json'
      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
    nonces << env.response_headers['replay-nonce']
  end

  def pop_nonce
    if nonces.empty?
      get_nonce
    else
      nonces.pop
    end
  end

  def get_nonce
112
    response = Faraday.head(env.url, nil, 'User-Agent' => USER_AGENT)
113
114
115
116
117
118
119
    response.headers['replay-nonce']
  end

  def nonces
    client.nonces
  end
end