Download the PHP package vaclavvanik/oauth2-token without Composer

On this page you can find all versions of the php package vaclavvanik/oauth2-token. 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 oauth2-token

oauth2-token

CI Latest Stable Version Total Downloads

A small contract for obtaining an OAuth 2.0 access token, plus a caching decorator that hands back the token it already has until it is about to expire - so an API client is not doing a token round-trip before every request.

Why

Plenty of APIs hand out a short-lived bearer token in exchange for a client id and secret (the client_credentials grant, or something close to it). The token fetch is trivial; the annoying part is not doing it every time. This package is that annoying part, done once:

No HTTP client is pulled in - the transport lives in the concrete TokenProvider you provide; the only runtime dependencies are the psr/http-client and psr/http-message interface packages. Tested on PHP 7.3 - 8.5.

Install

Usage

Implement the contract

fromValidatedArray() needs the token lifetime to be knowable - either an expires_in in the response, or a JWT access_token with an exp claim - otherwise it throws. If your provider returns an opaque token with a lifetime you only know from its docs, supply it yourself:

Wrap it with caching

Use the token

getToken() returns a cached token when one is still valid and only calls the wrapped TokenProvider when the cache misses or the stored token is within the refresh delta of expiry. Tokens are cached per client id + secret + scope set.

There is no lock around the fetch: if several processes hit a cold or expired cache at the same moment, each fetches its own token. Fine for most providers; if yours rate-limits the token endpoint hard, pre-warm the cache or add locking in a custom Repository.

Forcing a refresh

The cache cannot see a token that was revoked before its expiry - the API just starts rejecting it. RFC 6750 says that is a 401 with error="invalid_token", but check what your API actually does (some use 403). Catch it, forget() the token, and retry once with a fresh one:

forget() is part of RefreshableTokenProvider (what the factory returns), not the base TokenProvider interface - type-hint RefreshableTokenProvider where you need it.

Refresh delta

By default a token is treated as expired 60 seconds before its real expiry, leaving room for clock skew and the request itself. Override it when building the decorator:

Storage

Repository Use it for
Cache\MemoryRepository a single long-running process; nothing persists
Cache\JsonFileRepository sharing a token across CLI runs / workers; writes atomically, drops expired entries on save, and treats a corrupt file as an empty cache the next save rebuilds (a file it cannot read at all - permissions, a directory - still throws)
your own Cache\Repository Redis, APCu, PSR-6/PSR-16, ... - four methods: has, load, save, delete

Cache entries are keyed by Cache\Key - an md5 of the client id, secret and the TokenRequest scope string. Scopes are a set, so ['read', 'write'] and ['write', 'read'] share one entry. The secret is used only to derive the hash: this package never exposes it through its API and never puts it in an exception or a log line.

Exceptions

getToken() fails in one of two ways, and callers are meant to tell them apart:

Everything else this package throws implements Exception\Exception:

Run check - coding standards and php-unit

Install dependencies:

Run check:

Changelog

Please see CHANGELOG for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.


All versions of oauth2-token with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3 || ^8.0
ext-json Version *
psr/http-client Version ^1.0
psr/http-message Version ^1.0 || ^2.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 vaclavvanik/oauth2-token contains the following files

Loading the files please wait ...