class Clarion::Authn

Constants

STATUSES

Attributes

comment[R]
created_at[R]
expires_at[R]
id[R]
keys[R]
name[R]
status[R]
verified_at[R]
verified_key[R]

Public Class Methods

make(**kwargs) click to toggle source
# 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
new(id:, name: nil, comment: nil, keys: [], created_at:, expires_at:, status:, verified_at: nil, verified_key: nil) click to toggle source
# 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
random_id() click to toggle source
# File lib/clarion/authn.rb, line 24
def random_id
  SecureRandom.urlsafe_base64(64)
end

Public Instance Methods

as_json(*args) click to toggle source
# 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!() click to toggle source
# File lib/clarion/authn.rb, line 96
def cancel!
  @status = :cancelled
  true
end
cancelled?() click to toggle source
# File lib/clarion/authn.rb, line 68
def cancelled?
  status == :cancelled
end
closed?() click to toggle source
# File lib/clarion/authn.rb, line 72
def closed?
  !open? || expired? 
end
expired?() click to toggle source
# File lib/clarion/authn.rb, line 56
def expired?
  Time.now > expires_at
end
key_for_handle(handle) click to toggle source
# File lib/clarion/authn.rb, line 76
def key_for_handle(handle)
  keys.find { |_| _.handle == handle }
end
open?() click to toggle source
# File lib/clarion/authn.rb, line 60
def open?
  status == :open
end
to_h(all=false) click to toggle source
# 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) click to toggle source
# File lib/clarion/authn.rb, line 128
def to_json(*args)
  as_json(*args).to_json
end
verified?() click to toggle source
# File lib/clarion/authn.rb, line 64
def verified?
  status == :verified
end
verify(key, verified_at: Time.now) click to toggle source
# 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) click to toggle source
# 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