Class: Zas::Middleware::ZasAuthenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/zas/middleware/zas_authenticator.rb

Overview

A Rack Middleware implementation that can pass HTTP basic auth credentials to a ZAS service for authentication. If the requestor is authenticated then the user’s identifier will be added to the request environment as ‘env`.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ZasAuthenticator

Initialize the middleware with given app.



8
9
10
11
12
13
14
15
# File 'lib/zas/middleware/zas_authenticator.rb', line 8

def initialize(app)
  @app = app

  require 'zas/client'
  client_config = Zas::ClientConfiguration.new
  client_config.logger.level = Logger::INFO
  @zas_client = Zas::Client.new(client_config)
end

Instance Method Details

#call(env) ⇒ Object

Call the middleware



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zas/middleware/zas_authenticator.rb', line 18

def call(env)
  type, credentials = authorization_header_value(env)
  if type && type.downcase == 'basic'
    auth_result = @zas_client.authenticate(Zas::HttpBasicCredentials.new(credentials))
    if auth_result.authenticated?
      env['zas.user.identifier'] = auth_result.identifier
      @app.call(env)
    else
      authentication_failed
    end
  else
    authentication_failed
  end
end