PHP Classes

Lightweight PHP API Key Authentication: Store and validate user API keys in files

Recommend this page to a friend!
  Info   View files Example   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 162 All time: 8,916 This week: 129Up
Version License PHP version Categories
apikeys 1.0.0Public Domain7PHP 5, User Management, Web services, S...
Description 

Author

This package can store and validate user API keys in files.

It can take a given API key eventually taken from a API call request parameters and checks it against a list of allowed API keys retrieved from a given file that stores allowed users API keys.

The class can also add or update an API key for a given user in a the same file used from validating API keys.

Picture of Ray Paseur
  Performance   Level  
Name: Ray Paseur is available for providing paid consulting. Contact Ray Paseur .
Classes: 8 packages by
Country: United States United States
Age: 73
All time rank: 2241311 in United States United States
Week rank: 93 Up10 in United States United States Up
Innovation award
Innovation award
Nominee: 5x

Winner: 1x

Example

<?php // demo/classes/demo_APIKey.php
/**
 * Demonstrate Classes to Generate / Validate an API Key PHP7+
 *
 * https://www.phpclasses.org/recommend/854-A-random-API-key-generator-to-server-as-access-restriction-to-AP.html
 */
error_reporting(E_ALL);

require_once(
'class_APIKey.php');
require_once(
'class_APIKeyValidation.php');
require_once(
'class_APIKeyException.php');

echo
'<pre>';


// Show how to use the class with Success to validate the key
$apikey = '3c98ea3c99806fde3aa810c9f0f56e0d'; // Came from URL or other request data
try {
   
$name = APIKeyValidation::validate($apikey);
    echo
PHP_EOL . "API Key $apikey belongs to $name";
}
catch(
APIKeyException $e) {
    echo
PHP_EOL . $e->getMessage();
    echo
PHP_EOL . $e->getCode() . " can be used as the HTTP code";
    echo
PHP_EOL;
}


// Show how to use the class with Failure to validate the key
$apikey = 'This-Bogus-API-Key';
try {
   
$name = APIKeyValidation::validate($apikey);
    echo
PHP_EOL . "API Key $apikey belongs to $name";
}
catch(
APIKeyException $e) {
    echo
PHP_EOL . $e->getMessage();
    echo
PHP_EOL . $e->getCode() . " can be used as the HTTP code";
    echo
PHP_EOL;
}


// Show how to generate a new API key
$new = APIKeyValidation::storeKey('New-User');
echo
PHP_EOL . "Look for '$new' in this list:";
echo
PHP_EOL;
readfile( APIKeyValidation::APIKEYFILE );


Details

readme for class_APIKey.php This set of classes is a response to a request at this URL: https://www.phpclasses.org/recommend/854-A-random-API-key-generator-to-server-as-access-restriction-to-AP.html There are 3 classes in the set: Abstract class APIKey This is the base class that the other classes extend. It has one method, generate(), which creates a unique API key. It also defines two other methods. Method validate() test a given API key for validity and returns the name of the person it belongs to, or throws APIKeyException. Method storeKey() takes the name of a person, calls generate() to get a new API key, and stores the name:key combination in a place that validate() can find. Class APIKeyValidation This is the concrete class that provides the two abstract methods called for by APIKey. When validate() cannot associate an API key with a user name it throws APIKeyException. Class APIKeyException Extends Exception and provides a message and an HTTP code. There are 2 demonstration files in hte set: Script demo_APIKey.php exercises the classes. Text File demo_APIKey.txt simulates a database or flat file of valid API keys. Todo: Add a method to invalidate an API key.

  Files folder image Files  
File Role Description
Plain text file class_APIKey.php Class Abstract Class
Plain text file class_APIKeyException.php Class Exception Handler
Plain text file class_APIKeyValidation.php Class Extends Abstract Class
Accessible without login Plain text file demo_APIKey.php Example Demonstration Script
Accessible without login Plain text file demo_APIKey.txt Data Test File for Demo
Accessible without login Plain text file readme_APIKey.txt Doc. Readme text file

 Version Control Unique User Downloads Download Rankings  
 0%
Total:162
This week:0
All time:8,916
This week:129Up