Class: Vonage::JWT
- Inherits:
-
Object
- Object
- Vonage::JWT
- Defined in:
- lib/vonage-jwt/jwt.rb,
lib/vonage-jwt/version.rb
Constant Summary collapse
- VERSION =
'0.2.0'
Instance Attribute Summary collapse
-
#generator ⇒ Object
Returns the value of attribute generator.
-
#iat ⇒ Object
Returns the value of attribute iat.
-
#typ ⇒ Object
Returns the value of attribute typ.
Class Method Summary collapse
- .decode(token, secret = nil, verify = true, opts = {}, &block) ⇒ Object
- .verify_signature(token, signature_secret, algorithm) ⇒ Object
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(params = {}) ⇒ JWT
constructor
A new instance of JWT.
- #to_payload ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ JWT
Returns a new instance of JWT.
9 10 11 12 13 |
# File 'lib/vonage-jwt/jwt.rb', line 9 def initialize(params = {}) @generator = params.fetch(:generator) @typ = params.fetch(:typ, 'JWT') @iat = params.fetch(:iat, Time.now.to_i) end |
Instance Attribute Details
#generator ⇒ Object
Returns the value of attribute generator.
7 8 9 |
# File 'lib/vonage-jwt/jwt.rb', line 7 def generator @generator end |
#iat ⇒ Object
Returns the value of attribute iat.
7 8 9 |
# File 'lib/vonage-jwt/jwt.rb', line 7 def iat @iat end |
#typ ⇒ Object
Returns the value of attribute typ.
7 8 9 |
# File 'lib/vonage-jwt/jwt.rb', line 7 def typ @typ end |
Class Method Details
.decode(token, secret = nil, verify = true, opts = {}, &block) ⇒ Object
33 34 35 |
# File 'lib/vonage-jwt/jwt.rb', line 33 def self.decode(token, secret = nil, verify = true, opts = {}, &block) ::JWT.decode(token, secret, verify, opts, &block) end |
.verify_signature(token, signature_secret, algorithm) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/vonage-jwt/jwt.rb', line 37 def self.verify_signature(token, signature_secret, algorithm) begin decode(token, signature_secret, true, {algorithm: algorithm}) return true rescue ::JWT::VerificationError return false end end |
Instance Method Details
#generate ⇒ Object
15 16 17 |
# File 'lib/vonage-jwt/jwt.rb', line 15 def generate ::JWT.encode(to_payload, generator.private_key, generator.alg, header_fields={typ: typ}) end |
#to_payload ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/vonage-jwt/jwt.rb', line 19 def to_payload hash = { iat: iat, jti: generator.jti, exp: generator.exp || iat + generator.ttl, application_id: generator.application_id } hash.merge!(generator.paths) if generator.paths hash.merge!(sub: generator.subject) if generator.subject hash.merge!(nbf: generator.nbf) if generator.nbf hash.merge!(generator.additional_claims) if !generator.additional_claims.empty? hash end |