PHP code example of kba-team / micro-auth-lib

1. Go to this page and download the library: Download kba-team/micro-auth-lib library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kba-team / micro-auth-lib example snippets



kbATeam\MicroAuthLib\AuthResult;
use kbATeam\MicroAuthLib\Checksum;
use kbATeam\MicroAuthLib\Exceptions\InvalidParameterException;
use kbATeam\MicroAuthLib\Request;
use kbATeam\MicroAuthLib\Response;

//shared secret of client and server
Checksum::setSecret('shared secret');
try {
    //read and validate the GET request
    $request = Request::read($_GET);
    //get the authentication result from apache2 in REMOTE_USER
    $authResult = AuthResult::read($_SERVER);
} catch (InvalidParameterException $exception) {
    header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
    exit(500); //This is just a quick example. Please don't do this in your code.
}

//build client response and redirect there
$response = new Response($authResult->getAuthName(), $request->getId());
header('Location: ' . $response->getLocation($request->getReferer()), true, 302);


kbATeam\MicroAuthLib\Checksum;
use kbATeam\MicroAuthLib\Exceptions\InvalidParameterException;
use kbATeam\MicroAuthLib\Request;
use kbATeam\MicroAuthLib\Response;
use kbATeam\MicroAuthLib\Url;

//Insert the shared secret for kba-auth here.
Checksum::setSecret('shared secret');

if (isset($_COOKIE['micro-auth-id'])) {
    //Get the ID from the cookie and delete the cookie.
    $kbaAuthId = (int)$_COOKIE['micro-auth-id'];
    setcookie('micro-auth-id', null, -1);
    //Read the parameters from the GET request generated by kba-auth.
    try {
        $response = Response::read($_GET);
    } catch (InvalidParameterException $exception) {
        echo $exception->getMessage();
        die(); //This is just a quick example. Please don't do this in your code.
    }
    //Compare the ID from the request and the cookie for extra security.
    if ($response->getId() === $kbaAuthId) {
        echo 'Hello ' . $response->getAuthName() . '!';
        die(); //This is just a quick example. Please don't do this in your code.
    }
}

//Generate a random ID and save it to a cookie.
$kbaAuthId = rand(1000, 9999);
setcookie('micro-auth-id', $kbaAuthId);
//Generate a new request for kba-auth and add the ID and the referer.
$referer = new Url('https://myapp.test/test.php');
$request = new Request($referer, $kbaAuthId);
//Redirect the browser to the kba-auth service.
$kbaAuth = new Url('https://auth.service.test/');
header('Location: ' . $request->getLocation($kbaAuth), true, 302);