Class: Clarion::Authn

Inherits:
Object
  • Object
show all
Defined in:
lib/clarion/authn.rb

Constant Summary collapse

STATUSES =
%i(open cancelled verified expired)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name: nil, comment: nil, keys: [], created_at:, expires_at:, status:, verified_at: nil, verified_key: nil) ⇒ Authn

Returns a new instance of Authn.

Raises:

  • (ArgumentError)


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

def initialize(id:, name: nil, comment: nil, keys: [], created_at:, expires_at:, status:, verified_at: nil, verified_key: nil)
  @id = id
  @name = name
  @comment = comment
  @keys = keys.map{ |_| _.is_a?(Hash) ? Key.new(**_) : _}
  @created_at = created_at
  @expires_at = expires_at
  @status = status.to_sym
  @verified_at = verified_at
  @verified_key = verified_key.is_a?(Hash) ? Key.new(**verified_key) : verified_key

  @created_at = Time.xmlschema(@created_at) if @created_at && @created_at.is_a?(String)
  @expires_at = Time.xmlschema(@expires_at) if @expires_at && @expires_at.is_a?(String)
  @verified_at = Time.xmlschema(@verified_at) if @verified_at && @verified_at.is_a?(String)

  @status = :expired if expired?

  raise ArgumentError, ":status not valid" unless STATUSES.include?(@status)
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



49
50
51
# File 'lib/clarion/authn.rb', line 49

def comment
  @comment
end

#created_atObject (readonly)

Returns the value of attribute created_at.



51
52
53
# File 'lib/clarion/authn.rb', line 51

def created_at
  @created_at
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



52
53
54
# File 'lib/clarion/authn.rb', line 52

def expires_at
  @expires_at
end

#idObject (readonly)

Returns the value of attribute id.



49
50
51
# File 'lib/clarion/authn.rb', line 49

def id
  @id
end

#keysObject (readonly)

Returns the value of attribute keys.



50
51
52
# File 'lib/clarion/authn.rb', line 50

def keys
  @keys
end

#nameObject (readonly)

Returns the value of attribute name.



49
50
51
# File 'lib/clarion/authn.rb', line 49

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



53
54
55
# File 'lib/clarion/authn.rb', line 53

def status
  @status
end

#verified_atObject (readonly)

Returns the value of attribute verified_at.



54
55
56
# File 'lib/clarion/authn.rb', line 54

def verified_at
  @verified_at
end

#verified_keyObject (readonly)

Returns the value of attribute verified_key.



54
55
56
# File 'lib/clarion/authn.rb', line 54

def verified_key
  @verified_key
end

Class Method Details

.make(**kwargs) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/clarion/authn.rb', line 10

def make(**kwargs)
  kwargs.delete(:id)
  kwargs.delete(:created_at)
  kwargs.delete(:status)
  kwargs.delete(:verified_at)
  kwargs.delete(:verified_key)
  new(
    id: random_id,
    created_at: Time.now,
    status: :open,
    **kwargs,
  )
end

.random_idObject



24
25
26
# File 'lib/clarion/authn.rb', line 24

def random_id
  SecureRandom.urlsafe_base64(64)
end

Instance Method Details

#as_json(*args) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/clarion/authn.rb', line 120

def as_json(*args)
  to_h(*args).tap { |_|
    _[:created_at] = _[:created_at].xmlschema if _[:created_at]
    _[:verified_at] = _[:verified_at].xmlschema if _[:verified_at]
    _[:expires_at] = _[:expires_at].xmlschema if _[:expires_at]
  }
end

#cancel!Object



96
97
98
99
# File 'lib/clarion/authn.rb', line 96

def cancel!
  @status = :cancelled
  true
end

#cancelled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/clarion/authn.rb', line 68

def cancelled?
  status == :cancelled
end

#closed?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/clarion/authn.rb', line 72

def closed?
  !open? || expired? 
end

#expired?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/clarion/authn.rb', line 56

def expired?
  Time.now > expires_at
end

#key_for_handle(handle) ⇒ Object



76
77
78
# File 'lib/clarion/authn.rb', line 76

def key_for_handle(handle)
  keys.find { |_| _.handle == handle }
end

#open?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/clarion/authn.rb', line 60

def open?
  status == :open
end

#to_h(all = false) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/clarion/authn.rb', line 101

def to_h(all=false)
  {
    id: id,
    status: status,
    name: name,
    comment: comment,
    created_at: created_at,
    expires_at: expires_at,
  }.tap do |h|
    if verified_key
      h[:verified_at] = verified_at
      h[:verified_key] = verified_key.to_h(all)
    end
    if all
      h[:keys] = keys.map{ |_| _.to_h(all) }
    end
  end
end

#to_json(*args) ⇒ Object



128
129
130
# File 'lib/clarion/authn.rb', line 128

def to_json(*args)
  as_json(*args).to_json
end

#verified?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/clarion/authn.rb', line 64

def verified?
  status == :verified
end

#verify(key, verified_at: Time.now) ⇒ Object



89
90
91
92
93
94
# File 'lib/clarion/authn.rb', line 89

def verify(key, verified_at: Time.now)
  @verified_at = verified_at
  @verified_key = key
  @status = :verified
  true
end

#verify_by_handle(handle, verified_at: Time.now) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/clarion/authn.rb', line 80

def verify_by_handle(handle, verified_at: Time.now)
  key = key_for_handle(handle)
  unless key
    return nil
  end
  verify(key)
  return key
end