Class: Twilio::JWT::BaseJWT

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

Instance Method Summary collapse

Constructor Details

#initialize(secret_key: nil, issuer: nil, subject: nil, nbf: nil, ttl: 3600, valid_until: nil) ⇒ BaseJWT

valid_until overrides ttl if specified



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/twilio-ruby/jwt/jwt.rb', line 13

def initialize(secret_key: nil, issuer: nil, subject: nil, nbf: nil, ttl: 3600, valid_until: nil)
  if secret_key.nil?
    raise ArgumentError, 'JWT does not have a signing key'
  end

  @secret_key = secret_key
  @issuer = issuer
  @subject = subject
  @algorithm = 'HS256'
  @nbf = nbf
  @ttl = ttl
  @valid_until = valid_until
end

Instance Method Details

#_generate_headersObject



27
28
29
# File 'lib/twilio-ruby/jwt/jwt.rb', line 27

def _generate_headers
  {}
end

#_generate_payloadObject

Raises:

  • (NotImplementedError)


31
32
33
# File 'lib/twilio-ruby/jwt/jwt.rb', line 31

def _generate_payload
  raise NotImplementedError
end

#headersObject



35
36
37
38
39
40
# File 'lib/twilio-ruby/jwt/jwt.rb', line 35

def headers
  headers = _generate_headers.clone
  headers['typ'] = 'JWT'
  headers['alg'] = @algorithm
  headers
end

#payloadObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/twilio-ruby/jwt/jwt.rb', line 42

def payload
  payload = _generate_payload.clone

  payload[:iss] = @issuer
  payload[:nbf] = @nbf || Time.now.to_i
  payload[:exp] = @valid_until.nil? ? Time.now.to_i + @ttl : @valid_until
  payload[:sub] = @subject unless @subject.nil?

  payload
end

#to_jwtObject Also known as: to_s



53
54
55
# File 'lib/twilio-ruby/jwt/jwt.rb', line 53

def to_jwt
  ::JWT.encode payload, @secret_key, @algorithm, headers
end