Class: Ably::Models::Token

Inherits:
Object
  • Object
show all
Includes:
Ably::Modules::ModelCommon
Defined in:
lib/ably/models/token.rb

Overview

Authentication token issued by Ably in response to an token request

Constant Summary collapse

DEFAULTS =
{
  capability: { '*' => ['*'] },
  ttl:        60 * 60 # 1 hour
}
TOKEN_EXPIRY_BUFFER =

Buffer in seconds given to the use of a token prior to it being considered unusable For example, if buffer is 10s, the token can no longer be used for new requests 9s before it expires

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ably::Modules::ModelCommon

#==, #[], #as_json, #to_json

Methods included from Ably::Modules::MessagePack

#to_msgpack

Constructor Details

#initialize(attributes) ⇒ Token

Returns a new instance of Token.



15
16
17
# File 'lib/ably/models/token.rb', line 15

def initialize(attributes)
  @hash_object = IdiomaticRubyWrapper(attributes.clone.freeze)
end

Instance Attribute Details

#capabilityHash (readonly)

Returns Capabilities assigned to this token.

Returns:

  • (Hash)

    Capabilities assigned to this token



45
46
47
# File 'lib/ably/models/token.rb', line 45

def capability
  hash.fetch(:capability)
end

#client_idString (readonly)

Returns Optional client ID assigned to this token.

Returns:

  • (String)

    Optional client ID assigned to this token



51
52
53
# File 'lib/ably/models/token.rb', line 51

def client_id
  hash[:client_id]
end

#expires_atTime (readonly)

Returns Time the token expires.

Returns:

  • (Time)

    Time the token expires



39
40
41
# File 'lib/ably/models/token.rb', line 39

def expires_at
  as_time_from_epoch(hash.fetch(:expires), granularity: :s)
end

#hashHash (readonly)

Returns Access the token Hash object ruby’fied to use symbolized keys.

Returns:

  • (Hash)

    Access the token Hash object ruby’fied to use symbolized keys



70
71
72
# File 'lib/ably/models/token.rb', line 70

def hash
  @hash_object
end

#idString (readonly)

Returns Unique token ID used to authenticate requests.

Returns:

  • (String)

    Unique token ID used to authenticate requests



21
22
23
# File 'lib/ably/models/token.rb', line 21

def id
  hash.fetch(:id)
end

#issued_atTime (readonly)

Returns Time the token was issued.

Returns:

  • (Time)

    Time the token was issued



33
34
35
# File 'lib/ably/models/token.rb', line 33

def issued_at
  as_time_from_epoch(hash.fetch(:issued_at), granularity: :s)
end

#key_idString (readonly)

Returns Key ID used to create this token.

Returns:

  • (String)

    Key ID used to create this token



27
28
29
# File 'lib/ably/models/token.rb', line 27

def key_id
  hash.fetch(:key)
end

#nonceString (readonly)

Returns unique nonce used to generate Token and ensure token generation cannot be replayed.

Returns:

  • (String)

    unique nonce used to generate Token and ensure token generation cannot be replayed



57
58
59
# File 'lib/ably/models/token.rb', line 57

def nonce
  hash.fetch(:nonce)
end

Instance Method Details

#expired?Boolean

Returns true if token is expired or about to expire

Returns:

  • (Boolean)


64
65
66
# File 'lib/ably/models/token.rb', line 64

def expired?
  expires_at < Time.now + TOKEN_EXPIRY_BUFFER
end