class Virgil::Jwt::Jwt

Implements [AccessToken] in terms of Virgil JWT.

Attributes

body_content[R]

Gets a jwt body @return [JwtBodyContent]

header_content[R]

Gets a jwt header @return [JwtHeaderContent]

signature_data[R]

Gets a digital signature of jwt @return [Bytes]

string_representation[R]
unsigned_data[R]

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

Public Class Methods

from(jwt_str) click to toggle source
Initializes a new instance of the [Jwt] class using

its string representation @param jwt_str [String] string representation of signed jwt. It must be equal to:

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

+ Base64.urlsafe_encode64(JWT Body) “.” + Base64.urlsafe_encode64(Jwt Signature). @return [Jwt]

# 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
new(header_content:, body_content:, signature_data:) click to toggle source
Initializes a new instance of the [Jwt] class using specified header,

body and signature. @param header_content [JwtHeaderContext] jwt header @param body_content [JwtBodyContent] jwt body @param signature_data [Bytes] jwt signature data

# 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

Private Class Methods

parse_body_content(str) click to toggle source
# File lib/virgil/jwt/jwt.rb, line 111
def self.parse_body_content(str)
  body_json =  Base64URL.decode(str)
  JwtBodyContent.restore_from_json(body_json)
end
parse_header_content(str) click to toggle source
# File lib/virgil/jwt/jwt.rb, line 116
def self.parse_header_content(str)
  header_json = Base64URL.decode(str)
  JwtHeaderContent.restore_from_json(header_json)
end

Public Instance Methods

expired?() click to toggle source

Whether or not token is expired. @return [TrueClass]

# File lib/virgil/jwt/jwt.rb, line 103
def expired?
  Time.now.utc >= @body_content.expires_at
end
to_s() click to toggle source

String representation of jwt. @return [String]

# File lib/virgil/jwt/jwt.rb, line 97
def to_s
  @string_representation
end

Private Instance Methods

body_base64() click to toggle source
# File lib/virgil/jwt/jwt.rb, line 125
def body_base64
  Base64URL.encode(@body_content.to_json)
end
header_base64() click to toggle source
# File lib/virgil/jwt/jwt.rb, line 121
def header_base64
  Base64URL.encode(@header_content.to_json)
end
signature_base64() click to toggle source
# File lib/virgil/jwt/jwt.rb, line 129
def signature_base64
  Base64URL.encode(@signature_data.to_s)
end