Sha256: c1a00213318b12a283b7dfa961e6732a99c55aa596e28416953de64657ba68e5

Contents?: true

Size: 1016 Bytes

Versions: 1

Compression:

Stored size: 1016 Bytes

Contents

module ActiveRecord::Jwt
  module Decoder
    extend ActiveSupport::Concern

    module ClassMethods
      def find_authenticated_jwt(jwt)
        decoded_jwt = decode_jwt(jwt)
        self.find_by(ActiveRecord::Jwt::Decoder.configuration.sub => decoded_jwt[:payload]['sub'])
      end

      def decode_jwt(jwt)
        payload, header = JWT.decode(jwt, ActiveRecord::Jwt::Decoder.configuration.key, true, ActiveRecord::Jwt::Decoder.configuration.options)
        payload_valid?(payload)
        { payload: payload, header: header }
      rescue JWT::DecodeError => e
        raise ActiveRecord::Jwt::InvalidError.new(e)
      end

      private
      def payload_valid?(payload)
        raise ActiveRecord::Jwt::InvalidError.new if ActiveRecord::Jwt::Decoder.configuration.class && payload['class'] != self.name.to_s
      end
    end

    module_function
    def self.configure(&block)
      yield(configuration)
    end

    def configuration
      @_configuration ||= DecoderConfiguration.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-jwt-0.0.1 lib/activerecord-jwt/decoder.rb