Module: EndPointBlank::Commands::EndpointAuthorizeMethods::ClassMethods

Defined in:
lib/end_point_blank/commands/endpoint_authorize.rb

Instance Method Summary collapse

Instance Method Details

#authorize(request) ⇒ Object



18
19
20
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/end_point_blank/commands/endpoint_authorize.rb', line 18

def authorize(request)
  client_auth = request.headers['Authorization'].to_s
  method      = request.request_method
  path        = request.route_uri_pattern.to_s.gsub(/\([^)]*\)/, '')
  app_name    = Configuration.instance.app_name
  # The version is part of the key because authorization is decided per
  # endpoint version, and so is the deprecation carried back with it.
  # Without it, two callers on different versions of the same route share
  # one entry: whichever authorizes first decides both, so a client on a
  # deprecated version can get no warning, or one on a current version
  # can be told it is retiring.
  version     = VersionFinder.new.find(request)
  cache_key   = "epb_auth:#{client_auth}:#{path}:#{method}:#{app_name}:#{version}"

  cache = AuthenticationCache.instance
  # The cached value is the authorize response body, not a truthy
  # marker.
  #
  # It has to be, for two reasons. Callers parse the body — a cache hit
  # returning '' made JSON.parse raise, so a cached authorization became
  # a 500 rather than a fast success. And the body is where the
  # deprecation block lives; without it the Deprecation and Sunset
  # headers would appear only on cache misses, which reads as a flaky
  # feature rather than a missing one.
  if (cached = cache.retrieve(cache_key))
    return CachedResponse.new(201, cached)
  end

  # Host header only, never the forwarded chain -- see
  # BaseUrl.hostname_from_rack_env. request.host would read
  # X-Forwarded-Host unconditionally.
  hostname = EndPointBlank::BaseUrl.hostname_from_rack_env(request.env)
  body = {
    path: path,
    http_method: method,
    client_auth: client_auth,
    target_hostname: hostname,
    application: app_name,
    endpoint_version: version,
    source_ip: request.remote_ip,
    uuid: request.uuid
  }

  # Basic, not Bearer. This call is to intake, which already holds
  # this service's credential -- minting a token to present it back
  # was a hop that bought nothing. With no Bearer there is no stale
  # token, so the 401 retry that used to live here is gone: a 401 now
  # means the credential is wrong, which is worth surfacing rather
  # than retrying.
  response = Http.post(configuration.authorize_url, Authorization.header, body)

  return nil if response.nil?
  EndPointBlank.logger.info "Authentication response: #{response.status} - #{response.body}"
  if response.status == 201
    cache.store(cache_key, response.body)
  elsif response.status > 299
    EndPointBlank.logger.error "Failed to authorize endpoint: #{response.status} - #{response.body}"
  end
  response
end

#configurationObject



14
15
16
# File 'lib/end_point_blank/commands/endpoint_authorize.rb', line 14

def configuration
  EndPointBlank::Configuration.instance
end