Class: Vonage::JWT

Inherits:
Object
  • Object
show all
Defined in:
lib/vonage/jwt.rb

Class Method Summary collapse

Class Method Details

.generate(payload, private_key = nil) ⇒ String

Generate an encoded JSON Web Token.

By default the Vonage Ruby SDK generates a short lived JWT per request.

To generate a long lived JWT for multiple requests or to specify JWT claims directly call generate to generate a token, and set the token attribute on the client object.

Documentation for the Vonage Ruby JWT generator gem can be found at https://www.rubydoc.info/github/Nexmo/nexmo-jwt-ruby

Examples:

claims = {
  application_id: application_id,
  ttl: 800,
  subject: 'My_Subject'
}

private_key = File.read('path/to/private.key')

client.config.token = Vonage::JWT.generate(claims, private_key)

Parameters:

  • payload (Hash)
  • private_key (String, OpenSSL::PKey::RSA) (defaults to: nil)

Returns:

  • (String)

36
37
38
39
40
41
# File 'lib/vonage/jwt.rb', line 36

def self.generate(payload, private_key = nil)
  raise "Expecting 'private_key' in either the payload or as a separate parameter" if !payload[:private_key] && !private_key

  payload[:private_key] = private_key if private_key && !payload[:private_key]
  @token = Nexmo::JWTBuilder.new(payload).jwt.generate
end