Sha256: 1701d01915210734c5616dcb32cf6681611be4e08ee1daba35c1ff40a75a2180

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'g5_authenticatable_api/services/token_info'

module G5AuthenticatableApi
  module Services
    # Validates an access token against the G5 Auth server
    class TokenValidator < TokenInfo
      attr_reader :error

      def validate!
        token_data unless skip_validation?
      rescue StandardError => @error
        raise error
      end

      def valid?
        validate!
        true
      rescue StandardError
        false
      end

      def auth_response_header
        return unless error

        auth_header = String.new('Bearer')

        if access_token
          auth_header << " error=\"#{error_code}\""

          if error_description.present?
            auth_header << ",error_description=\"#{error_description}\""
          end
        end

        auth_header
      end

      private

      def error_code
        error_code = error.code if error.respond_to?(:code)
        error_code || 'invalid_request'
      end

      def error_description
        return unless error.respond_to?(:description)
        error.description
      end

      def skip_validation?
        @warden.try(:user) && !G5AuthenticatableApi.strict_token_validation
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
g5_authenticatable_api-1.0.0 lib/g5_authenticatable_api/services/token_validator.rb
g5_authenticatable_api-1.0.0.pre.1 lib/g5_authenticatable_api/services/token_validator.rb