class Philote::AccessKey

Attributes

allowed_uses[RW]
read[RW]
token[RW]
uses[RW]
write[RW]

Public Class Methods

create(**args) click to toggle source
# File lib/philote.rb, line 76
def self.create(**args)
  key = self.new(**args)
  key.save

  return key
end
load(token) click to toggle source
# File lib/philote.rb, line 108
def self.load(token)
  begin
    key = self.load!(token)
    return key
  rescue => exception
    return nil
  end
end
load!(token) click to toggle source
# File lib/philote.rb, line 83
def self.load!(token)
  raw_key = Philote.redis.call('GET', "philote:access_key:#{ token }")
  raise NonExistantAccessKey if raw_key.nil?

  begin
    parsed_key_attributes = JSON.parse(raw_key)
  rescue => exception
    raise UnparsableAccessKeyData.new(exception)
  end

  begin
    key_attributes = {
      read: parsed_key_attributes.fetch('read'),
      write: parsed_key_attributes.fetch('write'),
      allowed_uses: parsed_key_attributes.fetch('allowed_uses'),
      uses: parsed_key_attributes.fetch('uses'),
      token: token
    }
  rescue => exception
    raise InsufficientAccessKeyData.new(exception)
  end

  return self.new(key_attributes)
end
new(read: [], write: [], allowed_uses: 1, uses: 0, token: nil) click to toggle source

read:

an array of channel names the key user will be subscribed to.

write:

an array of channel names the key user will be able to write to.

allowed_uses:

the ammount of times a new websocket client will be able to authenticate
using this access key.

uses:

the ammount of times a new websocket client has authenticated using this
access key.

token:

the redis identifier.
# File lib/philote.rb, line 47
def initialize(read: [], write: [], allowed_uses: 1, uses: 0, token: nil)
  @token = token || SecureRandom.urlsafe_base64
  @read = read
  @write = write
  @allowed_uses = allowed_uses
  @uses = uses

  self
end

Public Instance Methods

save() click to toggle source
# File lib/philote.rb, line 61
def save
  Philote.redis.call('SET', "#{ Philote.prefix }:access_key:#{ token }", self.to_json)
end
to_h() click to toggle source
# File lib/philote.rb, line 65
def to_h
  {
    read: read,
    write: write,
    allowed_uses: allowed_uses,
    uses: uses
  }
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
to_json() click to toggle source
# File lib/philote.rb, line 57
def to_json
  self.to_h.to_json
end