class Slots::JWT::Slokens
Attributes
authentication_model_values[R]
exp[R]
extra_payload[R]
iat[R]
session[R]
token[R]
Public Class Methods
decode(token)
click to toggle source
# File lib/slots/jwt/slokens.rb, line 24 def self.decode(token) self.new(decode: true, token: token) end
encode(authentication_record, session = '', extra_payload)
click to toggle source
# File lib/slots/jwt/slokens.rb, line 27 def self.encode(authentication_record, session = '', extra_payload) self.new(encode: true, authentication_record: authentication_record, session: session, extra_payload: extra_payload) end
new(decode: false, encode: false, token: nil, authentication_record: nil, extra_payload: nil, session: nil)
click to toggle source
# File lib/slots/jwt/slokens.rb, line 8 def initialize(decode: false, encode: false, token: nil, authentication_record: nil, extra_payload: nil, session: nil) if decode @token = token decode() elsif encode @authentication_model_values = authentication_record.as_json @extra_payload = extra_payload.as_json @session = session update_iat update_exp encode() @valid = true else raise 'must encode or decode' end end
Public Instance Methods
expired?()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 31 def expired? @expired end
payload()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 56 def payload { authentication_model_key => @authentication_model_values, 'exp' => @exp, 'iat' => @iat, 'session' => @session, 'extra_payload' => @extra_payload, } end
update_token(authentication_record, extra_payload)
click to toggle source
# File lib/slots/jwt/slokens.rb, line 51 def update_token(authentication_record, extra_payload) update_exp update_token_data(authentication_record, extra_payload) end
update_token_data(authentication_record, extra_payload)
click to toggle source
# File lib/slots/jwt/slokens.rb, line 44 def update_token_data(authentication_record, extra_payload) @authentication_model_values = authentication_record.as_json @extra_payload = extra_payload.as_json update_iat encode end
valid!()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 39 def valid! raise InvalidToken, "Invalid Token" unless valid? self end
valid?()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 35 def valid? @valid end
Private Instance Methods
authentication_model_key()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 67 def authentication_model_key Slots::JWT.configuration.authentication_model.name.underscore end
decode()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 89 def decode begin set_payload ::JWT.decode @token, secret, true, verify_iat: true, algorithm: 'HS256' rescue ::JWT::ExpiredSignature @expired = true rescue ::JWT::InvalidIatError, ::JWT::VerificationError, ::JWT::DecodeError, NoMethodError, JSON::ParserError, Slots::JWT::InvalidSecret @valid = false else @valid = payload.slice(*default_expected_keys).compact.length == default_expected_keys.length end end
default_expected_keys()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 71 def default_expected_keys ['exp', 'iat', 'session', authentication_model_key] end
encode()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 83 def encode @token = ::JWT.encode self.payload, secret, 'HS256' @expired = false @valid = true end
secret()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 74 def secret Slots::JWT.configuration.secret(@iat) end
set_payload()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 102 def set_payload encoded64 = @token.split('.')[1] || '' string_payload = Base64.decode64(encoded64) local_payload = JSON.parse(string_payload) raise JSON::ParserError unless local_payload.is_a?(Hash) @exp = local_payload['exp']&.to_i @iat = local_payload['iat']&.to_i @session = local_payload['session'] @authentication_model_values = local_payload[authentication_model_key] @extra_payload = local_payload['extra_payload'] end
update_exp()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 80 def update_exp @exp = Slots::JWT.configuration.token_lifetime.from_now.to_i end
update_iat()
click to toggle source
# File lib/slots/jwt/slokens.rb, line 77 def update_iat @iat = Time.now.to_i end