Class: Nexmo::JWTBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/nexmo-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
# File 'lib/nexmo-jwt/jwt_builder.rb', line 46

def initialize(params = {})
  Nexmo::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, 'Subject')
  @jwt = Nexmo::JWT.new(generator: self)

  after_initialize!(self)
end

Instance Attribute Details

#algString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def alg
  @alg
end

#application_idString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def application_id
  @application_id
end

#expString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def exp
  @exp
end

#jtiString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def jti
  @jti
end

#jwtString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def jwt
  @jwt
end

#nbfString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def nbf
  @nbf
end

#pathsString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def paths
  @paths
end

#private_keyString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def private_key
  @private_key
end

#subjectString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def subject
  @subject
end

#ttlString

Generate an encoded JSON Web Token.

By default the Nexmo 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 = Nexmo::JWTBuilder.new(application_id: YOUR_APPLICATION_ID, private_key: YOUR_PRIVATE_KEY)
@token = @builder.jwt.generate
@generator = Nexmo::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/nexmo-jwt/jwt_builder.rb', line 44

def ttl
  @ttl
end

Class Method Details

.validate_parameters_not_conflicting(params) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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_application_id(application_id) ⇒ Object



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

def set_application_id(application_id)
  validate_application_id(application_id)

  application_id
end

#set_expObject



92
93
94
# File 'lib/nexmo-jwt/jwt_builder.rb', line 92

def set_exp
  Time.now.to_i
end

#set_private_key(private_key) ⇒ Object



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

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)


96
97
98
# File 'lib/nexmo-jwt/jwt_builder.rb', line 96

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)


104
105
106
# File 'lib/nexmo-jwt/jwt_builder.rb', line 104

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)


112
113
114
# File 'lib/nexmo-jwt/jwt_builder.rb', line 112

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)


100
101
102
# File 'lib/nexmo-jwt/jwt_builder.rb', line 100

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

#validate_subject(subject) ⇒ Object

Raises:

  • (ArgumentError)


116
117
118
# File 'lib/nexmo-jwt/jwt_builder.rb', line 116

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)


108
109
110
# File 'lib/nexmo-jwt/jwt_builder.rb', line 108

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