class GDS::Metrics::Auth

Attributes

app[RW]

Public Class Methods

new(app) click to toggle source
# File lib/gds_metrics/auth.rb, line 6
def initialize(app)
  self.app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/gds_metrics/auth.rb, line 10
def call(env)
  return app.call(env) unless metrics_path?(env)
  return app.call(env) unless config.auth_enabled?
  return app.call(env) if authorized?(env)

  unauthorized
end

Private Instance Methods

authorized?(env) click to toggle source
# File lib/gds_metrics/auth.rb, line 25
def authorized?(env)
  header = env.fetch("HTTP_AUTHORIZATION", "")
  token = header[/Bearer (.*)/i, 1]

  token == config.application_id
end
config() click to toggle source
# File lib/gds_metrics/auth.rb, line 32
def config
  Config.instance
end
metrics_path?(env) click to toggle source
# File lib/gds_metrics/auth.rb, line 20
def metrics_path?(env)
  path = env.fetch("PATH_INFO")
  path == config.prometheus_metrics_path
end
unauthorized() click to toggle source
# File lib/gds_metrics/auth.rb, line 36
def unauthorized
  [401, { "Content-Type" => "text/plain" }, ["Unauthorized"]]
end