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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of AccessToken.



11
12
13
14
15
16
17
18
19
# File 'lib/twilio-ruby/util/access_token.rb', line 11

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

Instance Attribute Details

#account_sidObject

Returns the value of attribute account_sid.



4
5
6
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def 
  @account_sid
end

#identityObject

Returns the value of attribute identity.



4
5
6
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def identity
  @identity
end

#nbfObject

Returns the value of attribute nbf.



4
5
6
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def nbf
  @nbf
end

#secretObject

Returns the value of attribute secret.



4
5
6
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def secret
  @secret
end

#signing_key_idObject

Returns the value of attribute signing_key_id.



4
5
6
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def signing_key_id
  @signing_key_id
end

#ttlObject

Returns the value of attribute ttl.



4
5
6
# File 'lib/twilio-ruby/util/access_token.rb', line 4

def ttl
  @ttl
end

Instance Method Details

#add_grant(grant) ⇒ Object



21
22
23
# File 'lib/twilio-ruby/util/access_token.rb', line 21

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

#to_jwt(algorithm = 'HS256') ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/twilio-ruby/util/access_token.rb', line 25

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,
      'exp' => now + @ttl,
      'grants' => grants
  }
  payload['nbf'] = @nbf unless @nbf.nil?

  JWT.encode payload, @secret, algorithm, headers
end

#to_sObject



51
52
53
# File 'lib/twilio-ruby/util/access_token.rb', line 51

def to_s
  to_jwt
end