Class: Masks::Credential

Inherits:
ApplicationModel show all
Defined in:
app/models/masks/credential.rb

Overview

A base class for credentials, which identify actors and check their access.

When a session is masked, a set of credentials are given the chance to inspect the session parameters, propose an actor, and approve or deny their access.

There are a few lifecycle methods available to credentials:

  • lookup - should return an identified actor if found

  • maskup - validates the session, actor, and any other data

  • backup - records the status of the credential’s check(s), if necessary

  • cleanup - deletes any recorded data for the credential

Sessions expect credentials to use checks to record their results, so there are helper methods to approve, deny, or skip associated checks—approve!, deny!, and skip! respectively.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.checks(value = nil) ⇒ Object



25
26
27
28
29
# File 'app/models/masks/credential.rb', line 25

def checks(value = nil)
  @checks ||= {}
  @checks[self.class.name] = value.to_s if value
  @checks[self.class.name]
end

Instance Method Details

#backupObject

write any data after all credentials/checks have run



74
75
76
# File 'app/models/masks/credential.rb', line 74

def backup
  nil # but overridable
end

#backup!Object



66
67
68
69
70
71
# File 'app/models/masks/credential.rb', line 66

def backup!
  self.passed_at = Time.current if check&.passed? &&
    check&.attempt_approved?(slug)

  backup
end

#checkObject



103
104
105
# File 'app/models/masks/credential.rb', line 103

def check
  session&.find_check(self.class.checks)
end

#cleanupObject

cleanup data re: the mask



79
80
81
# File 'app/models/masks/credential.rb', line 79

def cleanup
  nil # but overridable
end

#cleanup!Object



83
84
85
86
# File 'app/models/masks/credential.rb', line 83

def cleanup!
  cleanup
  reset!
end

#lookupObject

return an actor if it’s found and valid



45
46
47
# File 'app/models/masks/credential.rb', line 45

def lookup
  nil
end

#mask!Object



49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/masks/credential.rb', line 49

def mask!
  before_mask

  # existing checks (found from the session) can be
  # skipped when already present and not expired
  return if check&.passed? && check.attempts[slug] && valid?

  self.masked_at = Time.current

  maskup
end

#maskupObject

verify the session and actor



62
63
64
# File 'app/models/masks/credential.rb', line 62

def maskup
  nil
end

#nameObject



99
100
101
# File 'app/models/masks/credential.rb', line 99

def name
  I18n.t("auth.credentials.#{slug}.name")
end

#patch_paramsObject



107
108
109
# File 'app/models/masks/credential.rb', line 107

def patch_params
  session&.&.fetch(slug, {})
end

#slugObject



95
96
97
# File 'app/models/masks/credential.rb', line 95

def slug
  self.class.name.split("::").join("_").underscore
end