Class: Twilio::Util::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/twilio-ruby/util/access_token.rb

Defined Under Namespace

Classes: ConversationsGrant, IpMessagingGrant

Instance Method Summary collapse

Constructor Details

#initialize(account_sid, signing_key_id, secret, ttl = 3600, identity = nil) ⇒ AccessToken

Returns a new instance of AccessToken.



4
5
6
7
8
9
10
11
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def initialize(, signing_key_id, secret, ttl=3600, identity=nil)
  @account_sid = 
  @signing_key_sid = signing_key_id
  @secret = secret
  @ttl = ttl
  @identity = identity
  @grants = []
end

Instance Method Details

#add_grant(grant) ⇒ Object



13
14
15
# File 'lib/twilio-ruby/util/access_token.rb', line 13

def add_grant(grant)
  @grants.push(grant)
end

#to_jwt(algorithm = 'HS256') ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twilio-ruby/util/access_token.rb', line 17

def to_jwt(algorithm='HS256')
  now = Time.now.to_i - 1
  headers = {
      'cty' => 'twilio-fpa;v=1',
      'typ' => 'JWT'
  }

  grants = {}
  if @identity
    grants['identity'] = @identity
  end

  @grants.each { |grant| grants[grant.key] = grant.payload }

  payload = {
      'jti' => "#{@signing_key_sid}-#{now}",
      'iss' => @signing_key_sid,
      'sub' => @account_sid,
      'nbf' => now,
      'exp' => now + @ttl,
      'grants' => grants
  }
  JWT.encode payload, @secret, algorithm, headers
end

#to_sObject



42
43
44
# File 'lib/twilio-ruby/util/access_token.rb', line 42

def to_s
  to_jwt
end