Class: Vonage::JWTBuilder
- Inherits:
-
Object
- Object
- Vonage::JWTBuilder
- Defined in:
- lib/vonage-jwt/jwt_builder.rb
Instance Attribute Summary collapse
-
#additional_claims ⇒ String
Generate an encoded JSON Web Token.
-
#alg ⇒ String
Generate an encoded JSON Web Token.
-
#application_id ⇒ String
Generate an encoded JSON Web Token.
-
#exp ⇒ String
Generate an encoded JSON Web Token.
-
#jti ⇒ String
Generate an encoded JSON Web Token.
-
#jwt ⇒ String
Generate an encoded JSON Web Token.
-
#nbf ⇒ String
Generate an encoded JSON Web Token.
-
#paths ⇒ String
Generate an encoded JSON Web Token.
-
#private_key ⇒ String
Generate an encoded JSON Web Token.
-
#subject ⇒ String
Generate an encoded JSON Web Token.
-
#ttl ⇒ String
Generate an encoded JSON Web Token.
Class Method Summary collapse
Instance Method Summary collapse
- #after_initialize!(builder) ⇒ Object
-
#initialize(params = {}) ⇒ JWTBuilder
constructor
A new instance of JWTBuilder.
- #set_additional_claims(params) ⇒ Object
- #set_application_id(application_id) ⇒ Object
- #set_exp ⇒ Object
- #set_private_key(private_key) ⇒ Object
- #validate_application_id(application_id) ⇒ Object
- #validate_not_before(nbf) ⇒ Object
- #validate_paths(acl_paths) ⇒ Object
- #validate_private_key(private_key) ⇒ Object
- #validate_subject(subject) ⇒ Object
- #validate_time_to_live(ttl) ⇒ Object
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_claims ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def additional_claims @additional_claims end |
#alg ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def alg @alg end |
#application_id ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def application_id @application_id end |
#exp ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def exp @exp end |
#jti ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def jti @jti end |
#jwt ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def jwt @jwt end |
#nbf ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def nbf @nbf end |
#paths ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def paths @paths end |
#private_key ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def private_key @private_key end |
#subject ⇒ String
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:
44 45 46 |
# File 'lib/vonage-jwt/jwt_builder.rb', line 44 def subject @subject end |
#ttl ⇒ String
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:
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
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_exp ⇒ Object
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
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
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
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
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
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
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 |