Class: AgoraDynamicKey::AccessToken

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

Constant Summary collapse

VERSION =
"006"
ONE_DAY =
864_00
SEED =
2 ** 32 - 1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ AccessToken

Returns a new instance of AccessToken.



20
21
22
23
24
25
26
27
28
29
# File 'lib/dynamic_key/access_token.rb', line 20

def initialize args={}
  @app_id = args[:app_id]
  @channel_name = args.fetch(:channel_name, "")
  @app_certificate = args[:app_certificate]
  @uid = "#{args.fetch(:uid, "")}"
  @privileges = {}
  @privilege_expired_ts = args[:privilege_expired_ts]
  @salt = SecureRandom.rand(SEED)
  @expired_ts = Time.now.to_i + ONE_DAY
end

Instance Attribute Details

#app_certificateObject

Returns the value of attribute app_certificate.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def app_certificate
  @app_certificate
end

#app_idObject

Returns the value of attribute app_id.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def app_id
  @app_id
end

#channel_nameObject

Returns the value of attribute channel_name.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def channel_name
  @channel_name
end

#expired_tsObject

Returns the value of attribute expired_ts.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def expired_ts
  @expired_ts
end

#privilege_expired_tsObject

Returns the value of attribute privilege_expired_ts.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def privilege_expired_ts
  @privilege_expired_ts
end

#privilegesObject

Returns the value of attribute privileges.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def privileges
  @privileges
end

#saltObject

Returns the value of attribute salt.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def salt
  @salt
end

#uidObject

Returns the value of attribute uid.



16
17
18
# File 'lib/dynamic_key/access_token.rb', line 16

def uid
  @uid
end

Class Method Details

.generate!(payload = {}, &block) ⇒ Object



49
50
51
52
53
# File 'lib/dynamic_key/access_token.rb', line 49

def self.generate! payload={}, &block
  token = AccessToken.new payload
  block.call token
  token.build!
end

Instance Method Details

#add_privilege(privilege, ts) ⇒ Object Also known as: grant



31
32
33
# File 'lib/dynamic_key/access_token.rb', line 31

def add_privilege privilege, ts
  privileges[privilege] = ts
end

#buildObject



37
38
39
# File 'lib/dynamic_key/access_token.rb', line 37

def build
  Sign.encode self
end

#build!Object



41
42
43
# File 'lib/dynamic_key/access_token.rb', line 41

def build!
  Sign.encode! self
end

#from_string(token) ⇒ Object



45
46
47
# File 'lib/dynamic_key/access_token.rb', line 45

def from_string token
  Sign.decode token
end