Class: Vonage::JWTBuilder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ JWTBuilder

Returns a new instance of JWTBuilder.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vonage-jwt/jwt_builder.rb', line 46

def initialize(params = {})
  Vonage::JWTBuilder.validate_parameters_not_conflicting(params)

  @application_id = set_application_id(params.fetch(:application_id))
  @private_key = set_private_key(params.fetch(:private_key))
  @jti = params.fetch(:jti, SecureRandom.uuid)
  @nbf = params.fetch(:nbf, nil)
  @ttl = params.fetch(:ttl, 900)
  @exp = params.fetch(:exp, nil)
  @alg = params.fetch(:alg, 'RS256')
  @paths = params.fetch(:paths, nil)
  @subject = params.fetch(:subject, nil)
  @additional_claims = set_additional_claims(params)
  @jwt = Vonage::JWT.new(generator: self)

  after_initialize!(self)
end

Instance Attribute Details

#additional_claimsString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def additional_claims
  @additional_claims
end

#algString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def alg
  @alg
end

#application_idString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def application_id
  @application_id
end

#expString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def exp
  @exp
end

#jtiString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def jti
  @jti
end

#jwtString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def jwt
  @jwt
end

#nbfString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def nbf
  @nbf
end

#pathsString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def paths
  @paths
end

#private_keyString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def private_key
  @private_key
end

#subjectString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def subject
  @subject
end

#ttlString

Generate an encoded JSON Web Token.

By default the Vonage JWT generator creates a short lived (15 minutes) JWT per request.

To generate a long lived JWT for multiple requests, specify a longer value in the ‘exp` parameter during initialization.

Example with no custom configuration:

Example providing custom configuration options:

Examples:

@builder = Vonage::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Vonage::JWTBuilder.new(
  application_id: YOUR_APPLICATION_ID,
  private_key: YOUR_PRIVATE_KEY,
  ttl: 500,
  subject: 'My_Custom_Subject'
)
@token = @builder.jwt.generate

Parameters:

  • application_id (String)
  • iat (Integer)
  • jti (SecureRandom::UUIDv4)
  • nbf (Integer)
  • exp (Integer)
  • paths (Hash)
  • sub (String)
  • private_key (String, OpenSSL::PKey::RSA)

Returns:

  • (String)


44
45
46
# File 'lib/vonage-jwt/jwt_builder.rb', line 44

def ttl
  @ttl
end

Class Method Details

.validate_parameters_not_conflicting(params) ⇒ Object

Raises:

  • (ArgumentError)


64
65
66
67
68
# File 'lib/vonage-jwt/jwt_builder.rb', line 64

def self.validate_parameters_not_conflicting(params)
  return unless params[:ttl] && params[:exp]

  raise ArgumentError, "Expected either 'ttl' or 'exp' parameter, preference is to set 'ttl' parameter"
end

Instance Method Details

#after_initialize!(builder) ⇒ Object



70
71
72
73
74
75
# File 'lib/vonage-jwt/jwt_builder.rb', line 70

def after_initialize!(builder)
  validate_not_before(builder.nbf) if builder.nbf
  validate_time_to_live(builder.ttl)
  validate_paths(builder.paths) if builder.paths
  validate_subject(builder.subject) if builder.subject
end

#set_additional_claims(params) ⇒ Object



93
94
95
# File 'lib/vonage-jwt/jwt_builder.rb', line 93

def set_additional_claims(params)
  params.dup.delete_if {|k,v| [:application_id, :private_key, :jti, :nbf, :ttl, :exp, :alg, :paths, :subject].include?(k) }
end

#set_application_id(application_id) ⇒ Object



77
78
79
80
81
# File 'lib/vonage-jwt/jwt_builder.rb', line 77

def set_application_id(application_id)
  validate_application_id(application_id)

  application_id
end

#set_expObject



97
98
99
# File 'lib/vonage-jwt/jwt_builder.rb', line 97

def set_exp
  Time.now.to_i
end

#set_private_key(private_key) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/vonage-jwt/jwt_builder.rb', line 83

def set_private_key(private_key)
  validate_private_key(private_key)

  if File.exist?(private_key)
    OpenSSL::PKey::RSA.new(File.read(private_key))
  else
    OpenSSL::PKey::RSA.new(private_key)
  end
end

#validate_application_id(application_id) ⇒ Object

Raises:

  • (ArgumentError)


101
102
103
# File 'lib/vonage-jwt/jwt_builder.rb', line 101

def validate_application_id(application_id)
  raise ArgumentError, "Missing required 'application_id' parameter" if application_id.nil?
end

#validate_not_before(nbf) ⇒ Object

Raises:

  • (ArgumentError)


109
110
111
# File 'lib/vonage-jwt/jwt_builder.rb', line 109

def validate_not_before(nbf)
  raise ArgumentError, "Expected Integer parameter type for NotBefore 'nbf' parameter" unless nbf.is_a?(Integer)
end

#validate_paths(acl_paths) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
# File 'lib/vonage-jwt/jwt_builder.rb', line 117

def validate_paths(acl_paths)
  raise ArgumentError, "Expected Hash parameter type for Paths 'paths' parameter" unless acl_paths.is_a?(Hash)
end

#validate_private_key(private_key) ⇒ Object

Raises:

  • (ArgumentError)


105
106
107
# File 'lib/vonage-jwt/jwt_builder.rb', line 105

def validate_private_key(private_key)
  raise ArgumentError, "Missing required 'private_key' parameter" if private_key.nil?
end

#validate_subject(subject) ⇒ Object

Raises:

  • (ArgumentError)


121
122
123
# File 'lib/vonage-jwt/jwt_builder.rb', line 121

def validate_subject(subject)
  raise ArgumentError, "Expected String parameter type for Subject 'subject' parameter" unless subject.is_a?(String)
end

#validate_time_to_live(ttl) ⇒ Object

Raises:

  • (ArgumentError)


113
114
115
# File 'lib/vonage-jwt/jwt_builder.rb', line 113

def validate_time_to_live(ttl)
  raise ArgumentError, "Expected Integer parameter type for TimeToLive 'ttl' parameter" unless ttl.is_a?(Integer)
end