Class: MAuth::Rack::RequestAuthenticationFaker

Inherits:
RequestAuthenticator show all
Defined in:
lib/mauth/fake/rack.rb

Overview

This middleware bypasses actual authentication (it does not invoke mauth_client.authentic?). It instead uses a class attr method (is_authenic?) to determine if the request should be deemed authentic or not. Requests are authentic by default and RequestAuthenticationFaker.authentic = false must be called BEFORE EACH REQUEST in order to make a request inauthentic.

This is for testing environments where you do not wish to rely on a mauth service for making requests.

Note that if your application does not use env or env then it may be simpler to simply omit the request authentication middleware entirely in your test environment (rather than switching to this fake one), as all this does is add those keys to the request env.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RequestAuthenticator

#handle_head, #response_for_inauthentic_request, #response_for_missing_v2, #response_for_unable_to_authenticate, #should_authenticate?

Methods inherited from Middleware

#initialize, #mauth_client

Constructor Details

This class inherits a constructor from MAuth::Middleware

Class Method Details

.authentic=(is_auth = true) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



23
24
25
# File 'lib/mauth/fake/rack.rb', line 23

def authentic=(is_auth = true) # rubocop:disable Style/OptionalBooleanParameter
  @is_authentic = is_auth
end

.is_authentic?Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


19
20
21
# File 'lib/mauth/fake/rack.rb', line 19

def is_authentic? # rubocop:disable Naming/PredicateName
  @is_authentic.nil? ? true : @is_authentic
end

Instance Method Details

#call(env) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mauth/fake/rack.rb', line 28

def call(env)
  retval = if should_authenticate?(env)
             mauth_request = MAuth::Rack::Request.new(env)
             env['mauth.protocol_version'] = mauth_request.protocol_version

             if self.class.is_authentic?
               @app.call(env.merge!(MAuth::Client::RACK_ENV_APP_UUID_KEY => mauth_request.signature_app_uuid,
                 'mauth.authentic' => true))
             else
               response_for_inauthentic_request(env)
             end
           else
             @app.call(env)
           end

  # ensure that the next request is marked authenic unless the consumer of this middleware explicitly deems
  # otherwise
  self.class.authentic = true

  retval
end