Sha256: d93e30d9685eb2a738002428e8e9fe16cd185ee0ebb48cda6e96c8a30e226bfe

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

module MaticJWT
  module Grape
    module Middleware
      class Auth
        def initialize(app, options)
          @app = app
          @secret_reader = options[:secret]
        end

        def call(env)
          @env = env

          validate_request
          decode_payload
          authenticate!
          continue!
        end

        private

        def validate_request
          raise JWT::VerificationError, 'Authorization token is invalid' unless request.valid?
        end

        def decode_payload
          @env['auth_payload'] = jwt_authenticator.payload&.first
        end

        def authenticate!
          jwt_authenticator.authenticate_with_secret!(secret)
        end

        def continue!
          @app.call(@env)
        end

        def jwt_authenticator
          ::MaticJWT::Authenticator.new(request.auth_token)
        end

        def secret
          @secret_reader.call(jwt_authenticator.client_name)
        end

        def request
          @request ||= ::MaticJWT::Grape::Middleware::Request.new(@env)
        end
      end
    end
  end
end

Grape::Middleware::Auth::Strategies.add(:jwt_auth, ::MaticJWT::Grape::Middleware::Auth, ->(options) { [options] })

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
matic-jwt-1.2.1 lib/matic-jwt/grape/middleware/auth.rb
matic-jwt-1.2.0 lib/matic-jwt/grape/middleware/auth.rb
matic-jwt-1.1.0 lib/matic-jwt/grape/middleware/auth.rb