Class: JayDoubleuTee::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/jay_doubleu_tee/decoder.rb

Constant Summary collapse

InvalidTokenError =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(given) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/jay_doubleu_tee/decoder.rb', line 14

def call(given)
  return Failure(InvalidTokenError.new("Unauthorized. Token missing")) if given.nil? || given.empty?
  # Set password to nil and validation to false otherwise this won't work
  token = extract_token(given)
  res = JWT.decode token, signature, verify?, { algorithm: algorithm }
  Success(res[0]) # hides information about chosen algorithm and returns only payload
rescue JWT::DecodeError
  Failure(InvalidTokenError.new("Unauthorized. Token invalid"))
end