Class: Virgil::Jwt::Jwt

Inherits:
AccessToken show all
Defined in:
lib/virgil/jwt/jwt.rb

Overview

Implements [AccessToken] in terms of Virgil JWT.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AccessToken

#identity

Constructor Details

#initialize(header_content:, body_content:, signature_data:) ⇒ Jwt

Initializes a new instance of the [Jwt] class using specified header, body and signature.

Parameters:

  • header_content (JwtHeaderContext)

    jwt header

  • body_content (JwtBodyContent)

    jwt body

  • signature_data (Bytes)

    jwt signature data



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

def initialize(header_content:, body_content:, signature_data:)
  @header_content = header_content
  @body_content = body_content
  @signature_data = signature_data
  @string_representation = "#{header_base64}.#{body_base64}"
  @unsigned_data = Bytes.from_string(@string_representation)
  @string_representation += ".#{signature_base64}" unless @signature_data.nil?
end

Instance Attribute Details

#body_contentJwtBodyContent (readonly)

Gets a jwt body

Returns:



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

def body_content
  @body_content
end

#header_contentJwtHeaderContent (readonly)

Gets a jwt header

Returns:



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

def header_content
  @header_content
end

#signature_dataBytes (readonly)

Gets a digital signature of jwt

Returns:



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

def signature_data
  @signature_data
end

#unsigned_dataString (readonly)

String representation of jwt without signature. It equals to: Base64.urlsafe_encode64(JWT Header) + “.” + Base64.urlsafe_encode64(JWT Body)

Returns:

  • (String)


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

def unsigned_data
  @unsigned_data
end

Class Method Details

.from(jwt_str) ⇒ Jwt

Initializes a new instance of the [Jwt] class using its string representation It must be equal to:

Base64.urlsafe_encode64(jwt_header.to_base64) + "."

+ Base64.urlsafe_encode64(JWT Body) “.” + Base64.urlsafe_encode64(Jwt Signature).

Parameters:

  • jwt_str (String)

    string representation of signed jwt.

Returns:



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/virgil/jwt/jwt.rb', line 81

def self.from(jwt_str)
  begin
    parts = jwt_str.split('.')
    raise ArgumentError unless parts.size == 3
    signature_data = Bytes.new(Base64URL.decode(parts[2]).bytes)
    new(header_content: parse_header_content(parts[0]),
        body_content: parse_body_content(parts[1]),
        signature_data: signature_data)
  rescue StandardError
    raise ArgumentError, 'Wrong JWT format.'
  end

end

Instance Method Details

#expired?TrueClass

Whether or not token is expired.

Returns:

  • (TrueClass)


103
104
105
# File 'lib/virgil/jwt/jwt.rb', line 103

def expired?
  Time.now.utc >= @body_content.expires_at
end

#to_sString

String representation of jwt.

Returns:

  • (String)


97
98
99
# File 'lib/virgil/jwt/jwt.rb', line 97

def to_s
  @string_representation
end