Download the PHP package apigopro/slim-jwt-auth without Composer

On this page you can find all versions of the php package apigopro/slim-jwt-auth. 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 slim-jwt-auth

apigopro/slim-jwt-auth

A PSR-15 JWT authentication middleware for Slim Framework 4, requiring PHP 8.5.

It's a spiritual successor to tuupola/slim-jwt-auth (unmaintained since 2022) — same array-based options, same path/ignore/before/after/error callback shape — but built directly on firebase/php-jwt 7.x, since tuupola's package depended on firebase/php-jwt's old (pre-6.0) API that no longer exists.

Install

This package is published on Packagist.

Basic usage

Requests are authenticated by reading Authorization: Bearer <token> (configurable), decoding it with firebase/php-jwt, and — on success — attaching the decoded claims to the request as an attribute (token by default) before your route handler runs. Anything that fails verification (bad signature, expired, wrong audience if you check it yourself, etc.) short-circuits with a JSON 401.

Options

Option Default Notes
secret (required) String, or ['kid' => secret] map for key rotation, or ['kid' => [secret, algorithm]] for per-key algorithms.
algorithm 'HS256' Used for secret values that don't specify their own algorithm.
header 'Authorization' Header to read the token from.
regexp /Bearer\s+(.*)$/i Pattern used to pull the token out of the header.
cookie 'token' Cookie name used as a fallback if the header is absent. Set to false to disable.
attribute 'token' Request attribute the decoded claims are stored under. Set to false to skip attaching.
path ['/'] Path prefixes that require authentication. '/api' matches /api, /api/x, /api/x/y, etc. Supports * wildcards, e.g. '/api/*/public'.
ignore [] Path prefixes exempted from authentication, checked before path.
before null function($request, array $claims): ?ServerRequestInterface — run after successful auth, before the route handler. Return a modified request to replace it.
after null function($response, array $claims): ?ResponseInterface — run after the route handler. Return a modified response to replace it.
error null function($response, array $arguments): ?ResponseInterface — called on auth failure. $arguments['message'] has the reason. Return a response to override the default JSON 401/400.
secure true Require HTTPS (or a relaxed host) unless false.
relaxed ['localhost', '127.0.0.1'] Hostnames allowed over plain HTTP even when secure is true.
leeway 0 Seconds of clock skew tolerance for exp/nbf/iat checks (maps to Firebase\JWT\JWT::$leeway).
rules (auto) Advanced: pass your own array of SlimJwtAuth\RuleInterface implementations to fully control which requests get authenticated, replacing the default path/method rules.
response_factory (auto-detects slim/psr7) Any PSR-17 ResponseFactoryInterface. Only needed if you're not using slim/psr7.

Key rotation / multiple keys

The middleware picks the right key by matching the token's kid header against the array keys — this is the same mechanism firebase/php-jwt uses natively, just wired up for you.

Role-based access control (RBAC)

RbacMiddleware runs after JwtAuthMiddleware (usually attached per-route or per-group) and checks roles/permissions found in the decoded claims that JwtAuthMiddleware already attached to the request.

Slim runs middleware innermost-first for a route, so attach JwtAuthMiddleware at the app level (or earlier in the stack) and RbacMiddleware on the specific route/group that needs it.

Roles claim

By default, roles come from a roles claim in the JWT, which can be:

Change the claim name with 'roles_claim' => 'scope', or supply 'roles_extractor' => fn($claims) => [...] for anything more custom (nested claims, an external lookup, etc.) — it receives the full decoded claims object and must return a plain array of role strings.

Permissions

For finer-grained checks than "has this role", map roles to permissions and require permissions instead of (or alongside) roles:

Static constructors

For anything the static constructors don't cover, use the array-options form directly — it accepts everything below.

RBAC options

Option Default Notes
attribute 'token' Same attribute name JwtAuthMiddleware writes decoded claims to.
roles_claim 'roles' Claim to read roles from.
roles_extractor null function($claims): string[] — overrides roles_claim entirely.
roles [] Required roles. Empty means "no role requirement".
permissions [] Required permissions. Empty means "no permission requirement".
role_permissions [] ['role' => ['permission', ...]] map. '*' grants all permissions for that role.
mode 'any' 'any' (OR) or 'all' (AND), applied independently to roles and permissions.
error null function($response, array $arguments): ?ResponseInterface. $arguments has message, request, required_roles, actual_roles, required_permissions, actual_permissions.
response_factory (auto-detects slim/psr7) Same as JwtAuthMiddleware.

Status codes

Migrating from tuupola/slim-jwt-auth

Most configs port over unchanged. Concrete differences:

Testing

The test suite covers: valid tokens, missing/expired/mis-signed tokens, path/ignore prefix matching, kid-based key rotation, custom error callbacks, cookie fallback, the secure/relaxed HTTPS enforcement, before/after hooks, and RBAC role/permission checks (any/all modes, wildcard permissions, custom role extraction, and the 401-vs-403 distinction).

License

MIT.


All versions of slim-jwt-auth with dependencies

PHP Build Version
Package Version
Requires php Version ^8.5
firebase/php-jwt Version ^7.0
psr/http-message Version ^1.1 || ^2.0
psr/http-server-middleware Version ^1.0
psr/http-server-handler Version ^1.0
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 apigopro/slim-jwt-auth contains the following files

Loading the files please wait ...