Download the PHP package dakujem/auth-middleware without Composer

On this page you can find all versions of the php package dakujem/auth-middleware. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package auth-middleware

PSR-15 Auth Middleware

PHP from Packagist Tests Coverage Status Join the chat at https://gitter.im/dakujem/auth-middleware

Modern and highly flexible PSR-15 authentication and authorization middleware.

💿 composer require dakujem/auth-middleware

📒 Changelog

Default Usage

To use this package, you create two middleware layers:

Use Dakujem\Middleware\AuthWizard for convenience:

The pair of middleware (MW) will look for a JWT in the Authorization header or token cookie.\ Then it will decode the JWT and inject the decoded payload to the token request attribute, accessible to the application.\ If the token is not present or is not valid, the execution pipeline will be terminated by the assertion middleware and a 401 Unauthorized response will be returned.

The token can be accessed via the request attribute:

You can choose to apply the assertion to selected routes only instead of every route:

Custom token inspection can be applied too:

💡\ For highly flexible options to instantiate the middleware, read the "Compose Your Own Middleware" chapter below.

The examples above use Slim PHP framework, but the same usage applies to any PSR-15 compatible middleware dispatcher.

Extracting & Decoding JWT

The above creates an instance of TokenMiddleware that uses the default JWT decoder and injects the decoded token to the token Request attribute accessible further in the app stack.

If the decoded token appears in the attribute, it is:

Authorization

The middleware above will only decode the token, if present, authentic and valid, but will NOT terminate the pipeline in any case❗

The authorization must be done by a separate middleware:

The above creates a middleware that will assert that the token attribute of the Request contains a decoded token.\ Otherwise, the pipeline will be terminated and 401 (Unauthorized) Response returned. An error message will be encoded as JSON into the response.

As you can see, the pair of middleware acts as a couple, but is decoupled for flexibility.

The middleware created by AuthWizard::assertTokens asserts the presence of the decoded token only.\ It is possible to create custom inspections, of course:

Using AuthWizard::inspectTokens, the pipeline can be terminated on any conditions, involving the token or not.\ Custom error messages or data can be passed to the Response.

If the token is not present, the middleware acts the same as the one created by assertTokens and the inspector is not called.

You are of course able to cast the token to a custom class, with methods like MyToken::grantsAdminAccess to tell if the token authorizes the user for admin access.

The cast can either be done in the decoder or in a separate middleware.

Compose Your Own Middleware

In the examples above, we are using the AuthWizard helper which provides sensible defaults.\ However, it is possible and encouraged to build your own middleware using the components provided by this package.

You have the flexibility to fine-tune the middleware for any use case.

I'm using aliased names instead of full interface names in this documentation for brevity.

Here are the full interface names:

Alias Full interface name
Request Psr\Http\Message\ServerRequestInterface
Response Psr\Http\Message\ResponseInterface
ResponseFactory Psr\Http\Message\ResponseFactoryInterface
Handler Psr\Http\Server\RequestHandlerInterface
Logger Psr\Log\LoggerInterface

TokenMiddleware

The TokenMiddleware is responsible for finding and decoding a token, then making it available to the rest of the app.

The TokenMiddleware is composed of

Any of these callable components can be replaced or extended.\ The default components offer customization too.

Here are the defaults provided by AuthWizard::decodeTokens:

Usage tips 💡:

AuthWizard, AuthFactory

AuthWizard is a friction reducer that helps quickly instantiate token-decoding and assertion middleware with sensible defaults.\ AuthFactory is a configurable factory with sensible defaults provided for convenience.\ AuthWizard internally instantiates AuthFactory and acts as a static facade for the factory.

Use AuthFactory::decodeTokens to create token-decoding middleware.\ Use AuthFactory::assertTokens to create middleware that asserts the presence of a decoded token.\ Use AuthFactory::inspectTokens to create middleware with custom authorization rules against the token.

GenericMiddleware

The GenericMiddleware is used for assertion of token presence and custom authorization by AuthWizard / AuthFactory.

It can also be used for convenient inline middleware implementation:

TokenManipulators

The TokenManipulators static class provides various request/response manipulators that can be used for token handling.\ They are used as components of the middleware.

FirebaseJwtDecoder

The FirebaseJwtDecoder class serves as the default implementation for JWT token decoding.\ It is used as a decoder for the TokenMiddleware.\ You can swap it for a different implementation.

You need to install Firebase JWT package in order to use this decoder.\ composer require firebase/php-jwt:"^5.5"

Logger

The TokenMiddleware accepts a PSR-3 Logger instance for debug purposes.

Tips

Multiple token-decoding and token-inspecting middleware can be stacked too!

Token decoding will usually be applied to the app-level middleware (every route), but the assertions can be composed and applied to groups or individual routes as needed.

Testing

Run unit tests using the following command:

$ composer test

Compatibility

dakujem/auth-middleware PHP
1.x 7.4 - 8.2
2.x 8+

In order to use the FirebaseJwtDecoder decoder, a correct version of firebase/php-jwt must be installed. The use of this decoder is not required though.

dakujem/auth-middleware firebase/php-jwt
1.0 - 1.2 ^5
1.2 ^6 when using a single secret+algorithm combination only
2 ^5.5, ^6 and above

Contributing

Ideas, feature requests and other contribution is welcome. Please send a PR or create an issue.

Security Issues

If you happen to find a security problem, create an issue without disclosing any relevant details, we'll get in touch and discuss the details privately.


All versions of auth-middleware with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
dakujem/generic-middleware Version ^1
psr/http-factory Version ^1.0
psr/http-server-middleware Version ^1.0
psr/log Version ^2 || ^3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package dakujem/auth-middleware contains the following files

Loading the files please wait ....