Class: Keycard::Authentication::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/keycard/authentication/result.rb

Overview

A Result is the central point of information about an authentication attempt. It logs the authentication methods attempted with their statuses and reports the overall status. When authentication is successful, it holds the user/account that was verified.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



13
14
15
16
17
18
# File 'lib/keycard/authentication/result.rb', line 13

def initialize
  @account = nil
  @log = []
  @failed = false
  @csrf_safe = false
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



10
11
12
# File 'lib/keycard/authentication/result.rb', line 10

def 
  @account
end

#logObject (readonly)

Returns the value of attribute log.



11
12
13
# File 'lib/keycard/authentication/result.rb', line 11

def log
  @log
end

Instance Method Details

#authenticated?Boolean

Has this authentication completed successfully?

Returns:

  • (Boolean)


21
22
23
# File 'lib/keycard/authentication/result.rb', line 21

def authenticated?
  !.nil?
end

#csrf_safe?Boolean

Does a completed verification protect from Cross-Site Request Forgery?

This should be true in cases where the client presents authentication that is not automatic, like an authentication token, rather than automatic credentials like cookies or proxy-applied headers.

Returns:

  • (Boolean)


35
36
37
# File 'lib/keycard/authentication/result.rb', line 35

def csrf_safe?
  @csrf_safe
end

#failed(message) ⇒ Boolean

Log that the authentication method failed; terminate the chain.

Parameters:

  • message (String)

    a message about how the authentication method failed

Returns:

  • (Boolean)

    true, indicating that further authentication should not occur



52
53
54
55
# File 'lib/keycard/authentication/result.rb', line 52

def failed(message)
  log << "[FAILURE] #{message}"
  @failed = true
end

#failed?Boolean

Was there a failure for an attempted authentication method?

Returns:

  • (Boolean)


26
27
28
# File 'lib/keycard/authentication/result.rb', line 26

def failed?
  @failed
end

#skipped(message) ⇒ Boolean

Log that the authentication method was not applicable; continue the chain.

Parameters:

  • message (String)

    a message about why the authentication method was skipped

Returns:

  • (Boolean)

    false, indicating that the authentication method was inconclusive



43
44
45
46
# File 'lib/keycard/authentication/result.rb', line 43

def skipped(message)
  log << "[SKIPPED] #{message}"
  false
end

#succeeded(account, message, csrf_safe: false) ⇒ Boolean

Log that the authentication method succeeded; terminate the chain.

Parameters:

  • account (User|Account)

    Object/model representing the authenticated account

  • message (String)

    a message about how the authentication method succeeded

  • csrf_safe (Boolean) (defaults to: false)

    set to true if this authentication method precludes Cross-Site Request Forgery, as with a non-cookie token sent with the request

Returns:

  • (Boolean)

    true, indicating that further authentication should not occur



64
65
66
67
68
69
# File 'lib/keycard/authentication/result.rb', line 64

def succeeded(, message, csrf_safe: false)
  @account = 
  @csrf_safe ||= csrf_safe
  log << "[SUCCESS] #{message}"
  true
end