PHP code example of germania-kg / permanent-authentication

1. Go to this page and download the library: Download germania-kg/permanent-authentication 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/ */

    

germania-kg / permanent-authentication example snippets



use Germania\PermanentAuth\CreatePersistentLogin;
use RandomLib\Factory;
use RandomLib\Generator;

// Create Random generator
$factory = new RandomLib\Factory;
$random = $factory->getMediumStrengthGenerator();

// Setup expiration
$expire = new \DateTime;
date_add($expire, date_interval_create_from_date_string('10 days'));

// Setup hash function; this goes to database
$hasher = function( $token ) {	return password_hash( $token) };

// On error, throw Germania\PermanentAuth\Exceptions\StorageExceptionInterface
$client_storage = function( $selector, $token, $expire) { return setcookie( ... ); };

$server_storage = function($user_id, $selector, $token_hash, $expire) {
	$sql = 'INSERT INTO ...';
};


// Optional: PSR-3 Logger
$create = new CreatePersistentLogin( $random, $client_storage, $hasher, $server_storage, $expire);
$create = new CreatePersistentLogin( $random, $client_storage, $hasher, $server_storage, $expire, $logger);

// Action
$user_id = 99;
$created = $create( $user_id )

// Evaluate
if ($created):
	// Success!
endif;


use Germania\PermanentAuth\ClientAuthentication;

// Setup:
// 1. Retrieve the cookie value
$cookie_getter = function( $cookie_name ) {
	// return $_COOKIE[ $name ]
	return "foo:bar";
};

// 2: Split into selector and token part
$cookie_parser = function( $value ) {
	$parts = explode(":", $value);
	return (object) array(
		'selector' => $parts[0],
		'token'    => $parts[1]
	);
};

$auth = new ClientAuthentication( $cookie_getter, $cookie_parser, "persistent");

// Invoke
$selector_token = $auth();

// Evaluate
if ($selector_token):
	// Check if selector and token are valid on server-side
endif;



use Germania\PermanentAuth\AuthUserInterface;

class AppUser implements AuthUserInterface
{
	public $id;

    /**
     * Returns the User ID.
     * @return mixed
     */
    public function getId() {
    	return $this->id;
    }

    /**
     * Sets the User ID.
     * @param mixed $id
     */
    public function setId( $id )
    {
		$this->id = $id;
	}
}


use Germania\PermanentAuth\Middleware;
use Slim\App;

$app = new App;

$user = new AppUser;

$middleware = new Middleware( $user, ... );
$app->add( $middleware );



use Germania\PermanentAuth\ClientStorage;


use Germania\PermanentAuth\PdoStorage;


use Germania\PermanentAuth\PdoValidator;


use Germania\PermanentAuth\PdoDelete;