class Virgil::Jwt::Jwt
Implements [AccessToken] in terms of Virgil
JWT.
Attributes
Gets a jwt body @return [JwtBodyContent]
Gets a jwt header @return [JwtHeaderContent]
Gets a digital signature of jwt @return [Bytes]
String representation of jwt without signature. It equals to: Base64.urlsafe_encode64(JWT Header) + “.” + Base64.urlsafe_encode64(JWT Body) @return [String]
Public Class Methods
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
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
# 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
# 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
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
String representation of jwt. @return [String]
# File lib/virgil/jwt/jwt.rb, line 97 def to_s @string_representation end
Private Instance Methods
# File lib/virgil/jwt/jwt.rb, line 125 def body_base64 Base64URL.encode(@body_content.to_json) end
# File lib/virgil/jwt/jwt.rb, line 121 def header_base64 Base64URL.encode(@header_content.to_json) end
# File lib/virgil/jwt/jwt.rb, line 129 def signature_base64 Base64URL.encode(@signature_data.to_s) end