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



17
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
# File 'lib/end_point_blank/commands/endpoint_authorize.rb', line 17

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
  cache_key   = "epb_auth:#{client_auth}:#{path}:#{method}:#{app_name}"

  cache = AuthenticationCache.instance
  return CachedResponse.new(201, '') if cache.exists?(cache_key)

  hostname = request.host
  auth = Authorization.header(hostname)
  body = {
    path: path,
    http_method: method,
    client_auth: client_auth,
    target_hostname: hostname,
    application: app_name,
    endpoint_version: VersionFinder.new.find(request),
    source_ip: request.remote_ip,
    uuid: request.uuid
  }
  response = Http.post(configuration.authorize_url, auth, body)

  if response&.status == 401 && auth.to_s.start_with?("Bearer ")
    EndPointBlank::AccessTokens.instance.remove(hostname)
    auth = Authorization.header(hostname)
    response = Http.post(configuration.authorize_url, auth, body)
  end

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

#configurationObject



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

def configuration
  EndPointBlank::Configuration.instance
end