Class: Philote::AccessKey

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read: [], write: [], allowed_uses: 1, uses: 0, token: nil) ⇒ AccessKey

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.


47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#allowed_usesObject

Returns the value of attribute allowed_uses.



27
28
29
# File 'lib/philote.rb', line 27

def allowed_uses
  @allowed_uses
end

#readObject

Returns the value of attribute read.



27
28
29
# File 'lib/philote.rb', line 27

def read
  @read
end

#tokenObject

Returns the value of attribute token.



27
28
29
# File 'lib/philote.rb', line 27

def token
  @token
end

#usesObject

Returns the value of attribute uses.



27
28
29
# File 'lib/philote.rb', line 27

def uses
  @uses
end

#writeObject

Returns the value of attribute write.



27
28
29
# File 'lib/philote.rb', line 27

def write
  @write
end

Class Method Details

.create(**args) ⇒ Object



76
77
78
79
80
81
# File 'lib/philote.rb', line 76

def self.create(**args)
  key = self.new(**args)
  key.save

  return key
end

.load(token) ⇒ Object



108
109
110
111
112
113
114
115
# 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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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

Instance Method Details

#saveObject



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

def save
  Philote.redis.call('SET', "#{ Philote.prefix }:access_key:#{ token }", self.to_json)
end

#to_hObject Also known as: to_hash



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

def to_h
  {
    read: read,
    write: write,
    allowed_uses: allowed_uses,
    uses: uses
  }
end

#to_jsonObject



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

def to_json
  self.to_h.to_json
end