Class: Clarion::Authn

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

Constant Summary collapse

STATUSES =
%i(open verified)

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)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/clarion/authn.rb', line 24

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)

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

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



42
43
44
# File 'lib/clarion/authn.rb', line 42

def comment
  @comment
end

#created_atObject (readonly)

Returns the value of attribute created_at.



44
45
46
# File 'lib/clarion/authn.rb', line 44

def created_at
  @created_at
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



45
46
47
# File 'lib/clarion/authn.rb', line 45

def expires_at
  @expires_at
end

#idObject (readonly)

Returns the value of attribute id.



42
43
44
# File 'lib/clarion/authn.rb', line 42

def id
  @id
end

#keysObject (readonly)

Returns the value of attribute keys.



43
44
45
# File 'lib/clarion/authn.rb', line 43

def keys
  @keys
end

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/clarion/authn.rb', line 42

def name
  @name
end

#statusObject (readonly)

Returns the value of attribute status.



46
47
48
# File 'lib/clarion/authn.rb', line 46

def status
  @status
end

#verified_atObject (readonly)

Returns the value of attribute verified_at.



47
48
49
# File 'lib/clarion/authn.rb', line 47

def verified_at
  @verified_at
end

#verified_keyObject (readonly)

Returns the value of attribute verified_key.



47
48
49
# File 'lib/clarion/authn.rb', line 47

def verified_key
  @verified_key
end

Class Method Details

.make(**kwargs) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/clarion/authn.rb', line 10

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

.random_idObject



19
20
21
# File 'lib/clarion/authn.rb', line 19

def random_id
  SecureRandom.urlsafe_base64(64)
end

Instance Method Details

#as_json(*args) ⇒ Object



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

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

#expired?Boolean

Returns:

  • (Boolean)


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

def expired?
  Time.now > expires_at
end

#key_for_handle(handle) ⇒ Object



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

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

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  status == :open
end

#to_h(all = false) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/clarion/authn.rb', line 75

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



102
103
104
# File 'lib/clarion/authn.rb', line 102

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

#verified?Boolean

Returns:

  • (Boolean)


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

def verified?
  status == :verified
end

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



65
66
67
68
69
70
71
72
73
# File 'lib/clarion/authn.rb', line 65

def verify(key, verified_at: Time.now)
  unless key_for_handle(key.handle)
    return false
  end
  @verified_at = verified_at
  @verified_key = key
  @status = :verified
  true
end