Class: JWTea::Token::Payload

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jw_tea/token/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, exp:, iat: nil, jti: nil) ⇒ Payload

Returns a new instance of Payload.



19
20
21
22
23
24
# File 'lib/jw_tea/token/payload.rb', line 19

def initialize(data:, exp:, iat: nil, jti: nil)
  @data = data
  @exp = exp
  @iat = iat || Time.current.to_i
  @jti = jti || Digest::MD5.hexdigest([SecureRandom.hex, @iat].join(':'))
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/jw_tea/token/payload.rb', line 8

def data
  @data
end

#expObject (readonly)

Returns the value of attribute exp.



8
9
10
# File 'lib/jw_tea/token/payload.rb', line 8

def exp
  @exp
end

#iatObject (readonly)

Returns the value of attribute iat.



8
9
10
# File 'lib/jw_tea/token/payload.rb', line 8

def iat
  @iat
end

#jtiObject (readonly)

Returns the value of attribute jti.



8
9
10
# File 'lib/jw_tea/token/payload.rb', line 8

def jti
  @jti
end

Class Method Details

.from_hash(payload_hash) ⇒ Object



11
12
13
# File 'lib/jw_tea/token/payload.rb', line 11

def from_hash(payload_hash)
  new(payload_hash.transform_keys(&:to_sym))
end

Instance Method Details

#to_hObject



26
27
28
29
30
31
32
33
# File 'lib/jw_tea/token/payload.rb', line 26

def to_h
  {
    'data' => @data,
    'jti' => @jti,
    'iat' => @iat,
    'exp' => @exp,
  }
end