Class: Clickwrap::AuthorityDecision

Inherits:
Data
  • Object
show all
Defined in:
lib/clickwrap/authority.rb

Overview

The host's answer to "may this actor act for that represented party?". Clickwrap records the answer and its provenance; it does not decide whether the authority is legally sufficient.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorized: false, source: nil, role: nil, verified_at: nil, details: {}) ⇒ AuthorityDecision

Returns a new instance of AuthorityDecision.



72
73
74
75
76
77
78
79
80
# File 'lib/clickwrap/authority.rb', line 72

def initialize(authorized: false, source: nil, role: nil, verified_at: nil, details: {})
  super(
    authorized: authorized == true,
    source: source&.to_s,
    role: role&.to_s,
    verified_at: verified_at,
    details: (details || {}).to_h.deep_stringify_keys.freeze
  )
end

Instance Attribute Details

#authorizedObject (readonly)

Returns the value of attribute authorized

Returns:

  • (Object)

    the current value of authorized



71
72
73
# File 'lib/clickwrap/authority.rb', line 71

def authorized
  @authorized
end

#detailsObject (readonly)

Returns the value of attribute details

Returns:

  • (Object)

    the current value of details



71
72
73
# File 'lib/clickwrap/authority.rb', line 71

def details
  @details
end

#roleObject (readonly)

Returns the value of attribute role

Returns:

  • (Object)

    the current value of role



71
72
73
# File 'lib/clickwrap/authority.rb', line 71

def role
  @role
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



71
72
73
# File 'lib/clickwrap/authority.rb', line 71

def source
  @source
end

#verified_atObject (readonly)

Returns the value of attribute verified_at

Returns:

  • (Object)

    the current value of verified_at



71
72
73
# File 'lib/clickwrap/authority.rb', line 71

def verified_at
  @verified_at
end

Class Method Details

.from(value) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/clickwrap/authority.rb', line 94

def self.from(value)
  return value if value.is_a?(self)

  if value == true
    raise ConfigurationError,
          "The represented-party authority callback returned bare true. Return a " \
          "Clickwrap::AuthorityDecision (or a hash) with `authorized:`, `source:`, " \
          "`role:`, and `verified_at:` so the evidence records why the actor was authorized."
  end

  return new(authorized: false) if value.nil? || value == false

  unless value.respond_to?(:to_h)
    raise ConfigurationError,
          "The represented-party authority callback must return false, nil, a " \
          "Clickwrap::AuthorityDecision, or a hash with `authorized:`, `source:`, " \
          "`role:`, and `verified_at:`. It returned #{value.class.name}."
  end

  attributes = value.to_h.symbolize_keys
  unknown = attributes.keys - AUTHORITY_DECISION_ATTRIBUTES
  if unknown.any?
    label = unknown.one? ? "attribute" : "attributes"
    raise ConfigurationError,
          "The represented-party authority callback returned unknown #{label} " \
          "#{unknown.map { |key| "`#{key}:`" }.join(", ")}. Supported attributes are " \
          "#{AUTHORITY_DECISION_ATTRIBUTES.map { |key| "`#{key}:`" }.join(", ")}."
  end

  new(**attributes)
end

Instance Method Details

#authorized?Boolean

Returns:

  • (Boolean)


82
# File 'lib/clickwrap/authority.rb', line 82

def authorized? = authorized == true

#to_snapshotObject



84
85
86
87
88
89
90
91
92
# File 'lib/clickwrap/authority.rb', line 84

def to_snapshot
  {
    "state" => authorized? ? "verified" : "not_verified",
    "source" => source,
    "role" => role,
    "verified_at" => verified_at&.iso8601(6),
    "details" => details.presence
  }.compact
end