Sha256: cf6c04c60cba47036f5c0d21ba6c0982707d9288d9963ec49a30ff7166a190a1

Contents?: true

Size: 1.19 KB

Versions: 25

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Doorkeeper
  module OAuth
    class InvalidTokenResponse < ErrorResponse
      attr_reader :reason

      def self.from_access_token(access_token, attributes = {})
        reason = if access_token&.revoked?
                   :revoked
                 elsif access_token&.expired?
                   :expired
                 else
                   :unknown
                 end

        new(attributes.merge(reason: reason))
      end

      def initialize(attributes = {})
        super(attributes.merge(name: :invalid_token, state: :unauthorized))
        @reason = attributes[:reason] || :unknown
      end

      def status
        :unauthorized
      end

      def description
        @description ||=
          I18n.translate(
            @reason,
            scope: %i[doorkeeper errors messages invalid_token],
          )
      end

      protected

      def exception_class
        errors_mapping.fetch(reason)
      end

      private

      def errors_mapping
        {
          expired: Doorkeeper::Errors::TokenExpired,
          revoked: Doorkeeper::Errors::TokenRevoked,
          unknown: Doorkeeper::Errors::TokenUnknown,
        }
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
doorkeeper-5.5.0.rc2 lib/doorkeeper/oauth/invalid_token_response.rb
doorkeeper-5.5.0.rc1 lib/doorkeeper/oauth/invalid_token_response.rb
doorkeeper-5.4.0 lib/doorkeeper/oauth/invalid_token_response.rb
doorkeeper-5.4.0.rc2 lib/doorkeeper/oauth/invalid_token_response.rb
doorkeeper-5.4.0.rc1 lib/doorkeeper/oauth/invalid_token_response.rb