Class: Virgil::Jwt::JwtHeaderContent

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

Overview

Represents header of [Jwt]

Constant Summary collapse

VIRGIL_CONTENT_TYPE =
'virgil-jwt;v=1'.freeze
JWT_TYPE =
'JWT'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm:, key_id:, type: JWT_TYPE, content_type: VIRGIL_CONTENT_TYPE) ⇒ JwtHeaderContent

Initializes a new instance of the class

Parameters:

  • algorithm (String)

    signature algorithm

  • type (String) (defaults to: JWT_TYPE)

    access token type

  • content_type (String) (defaults to: VIRGIL_CONTENT_TYPE)

    Access token content type

  • key_id (String)


63
64
65
66
67
68
69
# File 'lib/virgil/jwt/jwt_header_content.rb', line 63

def initialize(algorithm:, key_id:, type: JWT_TYPE, content_type: VIRGIL_CONTENT_TYPE)
  # todo validate
  @algorithm = algorithm
  @key_id = key_id
  @type = type
  @content_type = content_type
end

Instance Attribute Details

#algorithmString (readonly)

Signature algorithm

Returns:

  • (String)


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

def algorithm
  @algorithm
end

#content_typeString (readonly)

Access token content type.

Returns:

  • (String)


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

def content_type
  @content_type
end

#key_idString (readonly)

Id of public key which is used for jwt signature verification.

Returns:

  • (String)


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

def key_id
  @key_id
end

#typeString (readonly)

Access token type.

Returns:

  • (String)


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

def type
  @type
end

Class Method Details

.restore_from_json(str_json) ⇒ JwtHeaderContent

Restore header content from json

Returns:



85
86
87
88
89
90
91
# File 'lib/virgil/jwt/jwt_header_content.rb', line 85

def self.restore_from_json(str_json)
  model = JSON.parse(str_json)
  new(algorithm: model['alg'],
      key_id: model['kid'],
      type: model['typ'],
      content_type: model['cty'])
end

Instance Method Details

#to_jsonString

Json representation of header content

Returns:

  • (String)


73
74
75
76
77
78
79
80
81
# File 'lib/virgil/jwt/jwt_header_content.rb', line 73

def to_json
  model = {
    'alg': algorithm,
    'kid': key_id,
    'typ': type,
    'cty': content_type
  }
  model.to_json
end