Class: Apes::Serializers::JWT

Inherits:
Object
  • Object
show all
Defined in:
lib/apes/serializers.rb

Overview

JWT encoded serialized value.

Class Method Summary (collapse)

Class Method Details

+ (String) dump(data)

Saves serialized data.

Parameters:

  • data (Object)

    The data to serialize.

Returns:

  • (String)

    Serialized data.



77
78
79
# File 'lib/apes/serializers.rb', line 77

def dump(data)
  ::JWT.encode({aud: "data", sub: data.as_json}, jwt_secret, "HS256")
end

+ (Object) jwt_secret

:nodoc:



82
83
84
# File 'lib/apes/serializers.rb', line 82

def jwt_secret
  Apes::RuntimeConfiguration.jwt_token
end

+ (Object) load(serialized, raise_errors = false, default = {})

Loads serialized data.

Parameters:

  • serialized (String)

    The serialized data.

  • raise_errors (Boolean) (defaults to: false)

    Whether to raise decoding errors.

  • default (Object) (defaults to: {})

    A fallback value to return when not raising errors.

Returns:

  • (Object)

    A deserialized value.



64
65
66
67
68
69
70
71
# File 'lib/apes/serializers.rb', line 64

def load(serialized, raise_errors = false, default = {})
  data = ::JWT.decode(serialized, jwt_secret, true, {algorithm: "HS256", verify_aud: true, aud: "data"}).dig(0, "sub")
  data = data.with_indifferent_access if data.is_a?(Hash)
  data
rescue => e
  raise(e) if raise_errors
  default
end