Sha256: 221bdf125f442885c03dd23e5f41fcdaefc91432c892caa2c64e3726a895c5c2

Contents?: true

Size: 881 Bytes

Versions: 5

Compression:

Stored size: 881 Bytes

Contents

# frozen_string_literal: true

require 'jwt/error'

module Warden
  module JWTAuth
    # Decodes a JWT into a hash payload into a JWT token
    class TokenDecoder
      include JWTAuth::Import['decoding_secret', 'rotation_secret', 'algorithm']

      # Decodes the payload from a JWT as a hash
      #
      # @see JWT.decode for all the exceptions than can be raised when given
      # token is invalid
      #
      # @param token [String] a JWT
      # @return [Hash] payload decoded from the JWT
      def call(token)
        decode(token, decoding_secret)
      rescue JWT::VerificationError
        decode(token, rotation_secret)
      end

      private

      def decode(token, secret)
        JWT.decode(token,
                   secret,
                   true,
                   algorithm: algorithm,
                   verify_jti: true)[0]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
warden-jwt_auth-0.11.0 lib/warden/jwt_auth/token_decoder.rb
warden-jwt_auth-0.10.1 lib/warden/jwt_auth/token_decoder.rb
warden-jwt_auth-0.10.0 lib/warden/jwt_auth/token_decoder.rb
warden-jwt_auth-0.9.0 lib/warden/jwt_auth/token_decoder.rb
warden-jwt_auth-0.8.0 lib/warden/jwt_auth/token_decoder.rb