Class: Virgil::Jwt::Jwt
- Inherits:
-
AccessToken
- Object
- AccessToken
- Virgil::Jwt::Jwt
- Defined in:
- lib/virgil/jwt/jwt.rb
Overview
Implements [AccessToken] in terms of Virgil JWT.
Instance Attribute Summary collapse
-
#body_content ⇒ JwtBodyContent
readonly
Gets a jwt body.
-
#header_content ⇒ JwtHeaderContent
readonly
Gets a jwt header.
-
#signature_data ⇒ Bytes
readonly
Gets a digital signature of jwt.
-
#unsigned_data ⇒ String
readonly
String representation of jwt without signature.
Class Method Summary collapse
-
.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).
Instance Method Summary collapse
-
#expired? ⇒ TrueClass
Whether or not token is expired.
-
#initialize(header_content:, body_content:, signature_data:) ⇒ Jwt
constructor
Initializes a new instance of the [Jwt] class using specified header, body and signature.
-
#to_s ⇒ String
String representation of jwt.
Methods inherited from AccessToken
Constructor Details
#initialize(header_content:, body_content:, signature_data:) ⇒ Jwt
Initializes a new instance of the [Jwt] class using specified header, body and signature.
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_content ⇒ JwtBodyContent (readonly)
Gets a jwt body
43 44 45 |
# File 'lib/virgil/jwt/jwt.rb', line 43 def body_content @body_content end |
#header_content ⇒ JwtHeaderContent (readonly)
Gets a jwt header
47 48 49 |
# File 'lib/virgil/jwt/jwt.rb', line 47 def header_content @header_content end |
#signature_data ⇒ Bytes (readonly)
Gets a digital signature of jwt
51 52 53 |
# File 'lib/virgil/jwt/jwt.rb', line 51 def signature_data @signature_data end |
#unsigned_data ⇒ String (readonly)
String representation of jwt without signature. It equals to: Base64.urlsafe_encode64(JWT Header) + “.” + Base64.urlsafe_encode64(JWT Body)
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).
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.
103 104 105 |
# File 'lib/virgil/jwt/jwt.rb', line 103 def expired? Time.now.utc >= @body_content.expires_at end |
#to_s ⇒ String
String representation of jwt.
97 98 99 |
# File 'lib/virgil/jwt/jwt.rb', line 97 def to_s @string_representation end |