Class: Virgil::Jwt::JwtBodyContent

Inherits:
Object
  • Object
show all
Defined in:
lib/virgil/jwt/jwt_body_content.rb

Overview

Represents content of [Jwt]

Constant Summary collapse

IDENTITY_PREFIX =
'identity-'.freeze
SUBJECT_PREFIX =
'virgil-'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, identity:, issued_at:, expires_at:, data:) ⇒ JwtBodyContent

Initializes a new instance of the class

Parameters:

  • app_id (String)

    Application ID. Take it from https://dashboard.virgilsecurity.com

  • identity (String)

    identity (must be equal to RawSignedModel identity when publishing card)

  • issued_at (Time)

    issued data

  • expires_at (Time)

    expiration date

  • data (Hash)

    hash with additional data



76
77
78
79
80
81
82
83
84
# File 'lib/virgil/jwt/jwt_body_content.rb', line 76

def initialize(app_id:, identity:, issued_at:, expires_at:, data:)
  @app_id = app_id
  @identity = identity
  @issued_at = issued_at
  @expires_at = expires_at
  @additional_data = data
  @issuer = "#{SUBJECT_PREFIX}#{@app_id}"
  @subject = "#{IDENTITY_PREFIX}#{@identity}"
end

Instance Attribute Details

#additional_dataHash (readonly)

Jwt additional data.

Returns:

  • (Hash)


68
69
70
# File 'lib/virgil/jwt/jwt_body_content.rb', line 68

def additional_data
  @additional_data
end

#app_idString (readonly)

Jwt application id.

Returns:

  • (String)


44
45
46
# File 'lib/virgil/jwt/jwt_body_content.rb', line 44

def app_id
  @app_id
end

#expires_atTime (readonly)

When Jwt will expire.

Returns:

  • (Time)


64
65
66
# File 'lib/virgil/jwt/jwt_body_content.rb', line 64

def expires_at
  @expires_at
end

#identityString (readonly)

Jwt identity.

Returns:

  • (String)


48
49
50
# File 'lib/virgil/jwt/jwt_body_content.rb', line 48

def identity
  @identity
end

#issued_atTime (readonly)

When Jwt was issued.

Returns:

  • (Time)


60
61
62
# File 'lib/virgil/jwt/jwt_body_content.rb', line 60

def issued_at
  @issued_at
end

#issuerString (readonly)

Jwt issuer.

Returns:

  • (String)


52
53
54
# File 'lib/virgil/jwt/jwt_body_content.rb', line 52

def issuer
  @issuer
end

#subjectString (readonly)

Jwt subject.

Returns:

  • (String)


56
57
58
# File 'lib/virgil/jwt/jwt_body_content.rb', line 56

def subject
  @subject
end

Class Method Details

.restore_from_json(str_json) ⇒ JwtBodyContent

Restore body content from json

Returns:



100
101
102
103
104
105
106
107
# File 'lib/virgil/jwt/jwt_body_content.rb', line 100

def self.restore_from_json(str_json)
  model = JSON.parse(str_json)
  new(app_id: model['iss'].gsub(JwtBodyContent::SUBJECT_PREFIX, ''),
      identity: model['sub'].gsub(JwtBodyContent::IDENTITY_PREFIX, ''),
      issued_at: Time.at(model['iat']).utc,
      expires_at: Time.at(model['exp']).utc,
      data: model['ada'])
end

Instance Method Details

#to_jsonString

Json representation of body content

Returns:

  • (String)


88
89
90
91
92
93
94
95
96
# File 'lib/virgil/jwt/jwt_body_content.rb', line 88

def to_json
  model = {
    'iss': issuer,
    'sub': subject,
    'iat': issued_at.to_i,
    'exp': expires_at.to_i,
    'ada': additional_data}
  model.to_json
end