Class: Vonage::JWT

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

Constant Summary collapse

VERSION =
'0.2.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#generatorObject

Returns the value of attribute generator.



7
8
9
# File 'lib/vonage-jwt/jwt.rb', line 7

def generator
  @generator
end

#iatObject

Returns the value of attribute iat.



7
8
9
# File 'lib/vonage-jwt/jwt.rb', line 7

def iat
  @iat
end

#typObject

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

#generateObject



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_payloadObject



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