Class: Nexmo::JWT

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

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ JWT

Returns a new instance of JWT.



9
10
11
12
13
# File 'lib/nexmo-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/nexmo-jwt/jwt.rb', line 7

def generator
  @generator
end

#iatObject

Returns the value of attribute iat.



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

def iat
  @iat
end

#typObject

Returns the value of attribute typ.



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

def typ
  @typ
end

Instance Method Details

#generateObject



15
16
17
# File 'lib/nexmo-jwt/jwt.rb', line 15

def generate
  ::JWT.encode(to_payload, generator.private_key, generator.alg)
end

#to_payloadObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/nexmo-jwt/jwt.rb', line 19

def to_payload
  hash = {
    iat: iat,
    jti: generator.jti,
    exp: generator.exp || iat + generator.ttl,
    sub: generator.subject,
    application_id: generator.application_id,
    typ: typ
  }
  hash.merge!(generator.paths) if generator.paths
  hash.merge!(nbf: generator.nbf) if generator.nbf
  hash
end