PHP code example of phant / auth

1. Go to this page and download the library: Download phant/auth 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/ */

    

phant / auth example snippets


use Phant\Auth\Domain\Service\AccessToken as ServiceAccessToken;
use Phant\Auth\Domain\Service\RequestAccess as ServiceRequestAccess;
use Phant\Auth\Domain\Entity\SslKey;
use App\RepositoryRequestAccess;


// Config

$sslKey = new SslKey('private key', 'public key');
$repositoryRequestAccess = new RepositoryRequestAccess();


// Build services

$serviceRequestAccess = new ServiceRequestAccess(
	$repositoryRequestAccess,
	$sslKey
);

$serviceAccessToken = return new ServiceAccessToken(
	$sslKey,
	$serviceRequestAccess
)

use Phant\Auth\Domain\Service\RequestAccessFromApiKey as ServiceRequestAccessFromApiKey;
use App\RepositoryApplication;


// Config

$repositoryApplication = new RepositoryApplication();


// Build services

$serviceRequestAccessFromApiKey = new ServiceRequestAccessFromApiKey(
	$serviceRequestAccess,
	$serviceAccessToken,
	$repositoryApplication
);


// Obtain API key from application

/* @todo */
$apiKey = 'XXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';


// Request access token

$accessToken = $serviceRequestAccessFromApiKey->getAccessToken($apiKey);

use Phant\Auth\Domain\Service\RequestAccessFromOtp as ServiceRequestAccessFromOtp;
use Phant\Auth\Domain\Entity\Application;
use Phant\Auth\Domain\Entity\User;
use App\OtpSender;


// Config

$repositoryApplication = new RepositoryApplication();
$otpSender = new OtpSender();


// Build services

$serviceRequestAccessFromOtp = new ServiceRequestAccessFromOtp(
	$serviceRequestAccess,
	$serviceAccessToken,
	$otpSender
);


// Request access token

$user = new User(
	'[email protected]',
	'John',
	'DOE'
);

$application = new Application(
	'eb7c9c44-32c2-4e88-8410-4ebafb18fdf7',
	'My app',
	'https://domain.ext/image.ext'
);

$requestAccessToken = $serviceRequestAccessFromOtp->generate($user, $application);


// Obtain Otp from user

/* @todo */
$otp = '123456';


// Verify Otp

$isValid = $serviceRequestAccessFromOtp->verify($otp);

if ( ! $isValid) {
	$numberOfAttemptsRemaining = $serviceRequestAccessFromOtp->numberOfAttemptsRemaining($requestAccessToken);
}


// Get access token

$accessToken = $serviceRequestAccessFromOtp->getAccessToken($requestAccessToken);

use Phant\Auth\Domain\Service\RequestAccessFromThirdParty as ServiceRequestAccessFromThirdParty;
use Phant\Auth\Domain\Entity\Application;
use Phant\Auth\Domain\Entity\User;
use App\RepositoryRequestAccess;


// Config

$repositoryApplication = new RepositoryApplication();
$otpSender = new OtpSender();


// Build services

$serviceRequestAccessFromThirdParty = new ServiceRequestAccessFromThirdParty(
	$serviceRequestAccess,
	$serviceAccessToken
);


// Request access token

$application = new Application(
	'eb7c9c44-32c2-4e88-8410-4ebafb18fdf7',
	'My app',
	'https://domain.ext/image.ext'
);

$requestAccessToken = $serviceRequestAccessFromThirdParty->generate(
	$application,
	'https://domain.ext/callback/url'
);


// Request third party auth with requestAccessToken

/* @todo */


// Obtain authentication status

/* @todo */
$isAuthorized = true;


// Obtain user data

/* @todo */
$user = new User(
	'[email protected]',
	'John',
	'DOE'
);


// Set auth status

$serviceRequestAccessFromThirdParty->setStatus($requestAccessToken, $user, $isAuthorized);


// Get access token

$accessToken = $serviceRequestAccessFromThirdParty->getAccessToken($requestAccessToken);

use Phant\Auth\Domain\Service\AccessToken as ServiceAccessToken;
use Phant\Auth\Domain\Service\RequestAccess as ServiceRequestAccess;
use Phant\Auth\FixtDomainure\DataStructure\SslKey;
use App\RepositoryRequestAccess;


// Config

$sslKey = new SslKey('private key', 'public key');
$repositoryRequestAccess = new RepositoryRequestAccess();


// Build services

$serviceRequestAccess = new ServiceRequestAccess(
	$repositoryRequestAccess,
	$sslKey
);

$serviceAccessToken = return new ServiceAccessToken(
	$sslKey,
	$serviceRequestAccess
)


// An access token

$accessToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhcHAiOnsiaWQiOiJjMjI4MDI1OC03OGU5LTQ4ZmQtOTA1Zi0yYzhlMDIzYWNiOWMiLCJuYW1lIjoiRmxhc2hwb2ludCIsImxvZ28iOiJodHRwczpcL1wvdmlhLnBsYWNlaG9sZGVyLmNvbVwvNDAweDIwMD90ZXh0PUZsYXNocG9pbnQiLCJhcGlfa2V5IjoiZnc5TEFJcFkuclA2b2d5VlNRdEx1OWRWMXBqOTR2WG56ekVPNXNISldHeHdhNWMxZzZMa3owNlo5dGNuc21GNFNieVRqeURTaCJ9LCJ1c2VyIjp7ImVtYWlsX2FkZHJlc3MiOiJqb2huLmRvZUBkb21haW4uZXh0IiwibGFzdG5hbWUiOiJET0UiLCJmaXJzdG5hbWUiOiJKb2huIiwicm9sZSI6bnVsbH0sImlhdCI6MTY2MzY4NDM3MCwiZXhwIjoxNjYzNjk1MTcwfQ.a-wJ_T1ENG58zCw2X7oP2oZrziZRP_m0rOOkUkC2axAsx7O72ebGjQja-iry-lFvd1PF48BxejQw69LPUQKrx1Tb9oQ_8VqMhU97nR8Jd5v2jlWIA7CP2H9voQLE5ybHpqFO2IzgPf2MurzwXQ0tlSeiRbQzHLzMBbWhcQLU4aI';

use Phant\DataStructure\Token\Jwt;
use Phant\Error\NotCompliant;

$publicKey = $serviceAccessToken->getPublicKey();

try {
	$payLoad = (new Jwt($accessToken))->decode($publicKey);
} catch (NotCompliant $e) {
	
}

use Phant\Auth\Domain\Entity\Application;

$application = new Application(
	'eb7c9c44-32c2-4e88-8410-4ebafb18fdf7',
	'My app',
	'https://domain.ext/image.ext'
);

$isValid = $serviceAccessToken->check($accessToken, $application);

use Phant\Auth\Domain\Entity\Application;

$application = new Application(
	'eb7c9c44-32c2-4e88-8410-4ebafb18fdf7',
	'My app',
	'https://domain.ext/image.ext'
);

$payload = $serviceAccessToken->getPayload($accessToken);