class JWTea::Kettle
Public Class Methods
new(secret:, store:, algorithm: nil, expires_in: nil)
click to toggle source
# File lib/jwtea/kettle.rb, line 9 def initialize(secret:, store:, algorithm: nil, expires_in: nil) @secret = secret @store = store @algorithm = algorithm || ::JWTea.configuration.default_algorithm @expires_in = (expires_in || ::JWTea.configuration.default_expires_in).to_i end
Public Instance Methods
brew(data)
click to toggle source
# File lib/jwtea/kettle.rb, line 16 def brew(data) exp = @expires_in.seconds.from_now.to_i token = ::JWTea::Token.build(data, exp, @secret, @algorithm) @store.save(token.jti, token.exp, @expires_in) token end
decode(encoded_token)
click to toggle source
# File lib/jwtea/kettle.rb, line 38 def decode(encoded_token) token = pour(encoded_token) token.data end
encode(data)
click to toggle source
# File lib/jwtea/kettle.rb, line 33 def encode(data) token = brew(data) token.encoded end
inspect()
click to toggle source
Prevent sentitive data from being accidentally logged to console
# File lib/jwtea/kettle.rb, line 54 def inspect "#<#{self.class} expires_in: #{@expires_in}, store: #{@store}>" end
pour(encoded_token)
click to toggle source
# File lib/jwtea/kettle.rb, line 23 def pour(encoded_token) with_token(encoded_token) do |token| raise ::JWTea::InvalidToken.new('token revoked') unless token_exists?(token) token end rescue ::JWT::DecodeError => e raise ::JWTea::InvalidToken.new(e.message) end
revoke(encoded_token)
click to toggle source
# File lib/jwtea/kettle.rb, line 43 def revoke(encoded_token) with_token(encoded_token) { |token| @store.delete(token.jti) } end
to_h()
click to toggle source
Prevent sentitive data from being accidentally rendered to json
# File lib/jwtea/kettle.rb, line 59 def to_h { expires_in: @expires_in }.freeze end
valid?(encoded_token)
click to toggle source
# File lib/jwtea/kettle.rb, line 47 def valid?(encoded_token) with_token(encoded_token) { |token| token_exists?(token) } rescue JWT::DecodeError false end
Private Instance Methods
token_exists?(token)
click to toggle source
# File lib/jwtea/kettle.rb, line 65 def token_exists?(token) @store.exists?(token.jti, token.exp) end
with_token(encoded_token) { |token| ... }
click to toggle source
# File lib/jwtea/kettle.rb, line 69 def with_token(encoded_token) token = ::JWTea::Token.load(encoded_token, @secret, @algorithm) raise ::JWTea::MissingJtiError unless token.jti raise ::JWTea::MissingExpError unless token.exp yield(token) end