Class: ETokenAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/e-core/instance/setup/auth.rb

Overview

borrowed from Rails and adapted to Espresso needs

Constant Summary collapse

TOKEN_REGEX =
/^Token /
AUTHN_PAIR_DELIMITERS =
/(?:,|;|\t+)/

Instance Method Summary collapse

Constructor Details

#initialize(controller_instance) ⇒ ETokenAuth

Returns a new instance of ETokenAuth.



153
154
155
# File 'lib/e-core/instance/setup/auth.rb', line 153

def initialize controller_instance
  @controller_instance = controller_instance
end

Instance Method Details

#request_token_auth(realm) ⇒ Object

Sets a WWW-Authenticate and halt to let the client know a token is required.

Parameters:

  • realm (String)
    • realm to use in the header



176
177
178
179
180
# File 'lib/e-core/instance/setup/auth.rb', line 176

def request_token_auth(realm)
  @controller_instance.response[EConstants::HEADER__AUTHENTICATE] = \
    EUtils.encode_token_auth_credentials(realm: realm.delete('"'))
  @controller_instance.halt(EConstants::STATUS__PROTECTED, "HTTP Token: Access denied.\n")
end

#validate_token_auth(&proc) ⇒ Object

If token Authorization header is present, call the given proc with the present token and options.

Parameters:

  • proc (Proc)

    Proc to call if a token is present. The Proc should take two arguments:

    validate_token_auth { |token, options| ... }
    

Returns:

  • the return value of ‘proc` if a token is found

  • ‘nil` if no token found



167
168
169
170
# File 'lib/e-core/instance/setup/auth.rb', line 167

def validate_token_auth &proc
  token, options = token_and_options
  token && token.size > 0 && proc.call(token, options)
end