Download the PHP package kynx/api-key-generator without Composer

On this page you can find all versions of the php package kynx/api-key-generator. 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 api-key-generator

kynx/api-key-generator

Build Status

Generate and parse well-formed API keys.

Introduction

API keys are a common way to authenticate users accessing an API. While there isn't a standard saying how they should be constructed, there are some best practices.

An API key should:

This library helps with this, providing an ApiKeyGenerator for generating and parsing keys, and an ApiKey object for working with the keys themselves. It does not provide help with storing and verifying the keys - tha part is up to you.

Installation

Usage

Generating a key

This will output something like:

See the examples directory for code showing how to modify the key strength, change the characters used, etc.

Parsing a key

Since the key is well-formed, this will output:

Now make a random change to the key. The parse() method will return null and you will see theInvalid key! message.

Note: Just because a key is well-formed does not mean the user can be authenticated. It just means it looks like it was generated by you. You must still verify the key against your persistent storage.

Handling old keys

What happens if you change the prefix for your key, or want to increase the secret strength (see below)? You will want to issue all new keys with the updated settings, but will still need to parse old keys.

This is what the KeyGeneratorChain is for:

New keys will always be generated by the $primary KeyGenerator. When parsing, the $primary will be tried first. If it returns null, each of the $fallback KeyGenerators will be tried in turn, with the first successful parse result returned, or null if none match. See parse-chain for a more complete example.

API Key Structure

You will notice in the examples above that the generated key is composed of three parts separated by underscores. They are:

Prefix

The prefix is always what you passed as the first argument passed to the KeyGenerator constructor. It is there to make your key easy to recognise, both to end users and to secret scanners that find leaked keys in the wild. In the examples we've used a company identifier (xyz) plus a string indicating whether it's for use in our production or sandbox environments. If you've ever integrated with Stripe you will be familiar with this pattern, but you are free to use whatever format makes sense.

Identifier

The identifier is a random string that can be used to look up the secret in a database. Store this un-hashed in a case-sensitive column (for example, VARBINARY in MySQL) and put a unique constraint on it. The generated identifiers are not guaranteed to be unique, so when inserting into the database your should be prepared to catch the constraint violation, generate a new key and re-try. See store-and-authenticate for an example.

Secret

The secret part provides the security. This must be hashed (using PHP's password_hash()) before storing. See the store-and-authenticate example code if you are unsure how to do this.

Checksum

Finally the checksum is a crc32b hash of the rest of the key. It is there to quickly filter out garbage hitting your API, without needing to bother your persistence layer. Just because it matches does not mean the user can be authenticated! You still need to check the identifier and secret against what you stored when the key was generated.

Defaults

By default the identifier is 8 characters long. With the default characters this gives pow(59, 8) - 1 combinations. Even with millions of keys in storage the risk of collisions is tiny. Given that API keys are only really suitable for organisation-level access control, this should be plenty. But if needed you can increase it.

By default the secret is 32 characters long. You can improve the security of your API by increasing this. See the generate-secure example.

By default the generated part of the key is composed of the characters [a-zA-Z0-9_]. You should ensure your prefix is too: this makes it easy to copy it from your key management console. Try double-clicking on xyz-sandbox_Pu!Lo&jP_N22/Oh5hz48h4.QM_e07f9ca3 to see what happens when other characters are present. If you want more entropy, make your secret longer.

Upgrading

1.x -> 2.x

In 1.x the default secret key length was 16 characters. Much as I hate BC breaks, this was too low for a library advocating best practice. In 2.x the default is 32 and the minimum is 24. I think it is better to fix this mistake early.

To upgrade without breaking existing keys you will need to use the KeyGeneratorChain along with a BC generator for parsing the old keys:

The BcKeyGenerator cannot be used for generating new keys, only for parsing old ones. It and the associated BcApiKey are deprecated will be dropped in version 3.x.

Further reading


All versions of api-key-generator with dependencies

PHP Build Version
Package Version
Requires php Version ~8.2.0 || ~8.3.0 || ~8.4.0
ircmaxell/random-lib Version ^1.2
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 kynx/api-key-generator contains the following files

Loading the files please wait ....