PHP code example of monkeyscloud / monkeyslegion-auth
1. Go to this page and download the library: Download monkeyscloud/monkeyslegion-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/ */
monkeyscloud / monkeyslegion-auth example snippets
use MonkeysLegion\Auth\Guard\JwtGuard;
use MonkeysLegion\Auth\Guard\AuthManager;
use MonkeysLegion\Auth\Service\JwtService;
$jwt = new JwtService('your-secret-key-at-least-32-chars');
$guard = new JwtGuard($jwt, $userProvider);
$manager = new AuthManager(defaultGuard: 'jwt');
$manager->register('jwt', $guard);
// Authenticate a request
$user = $manager->guard()->authenticate($request);
use MonkeysLegion\Auth\Guard\SessionGuard;
$guard = new SessionGuard($session, $userProvider);
// Login (regenerates session ID to prevent fixation)
$guard->login($user);
// Authenticate from session
$user = $guard->authenticate($request);
// Logout
$guard->logout();
use MonkeysLegion\Auth\Guard\WebAuthnGuard;
use MonkeysLegion\Auth\Event\PasskeyAuthenticated;
use MonkeysLegion\Auth\Attribute\Passkey;
// 1. Register the guard
$manager->register('webauthn', new WebAuthnGuard($userProvider));
// 2. In your controller: verify the assertion, then set the request attribute
$credential = $webAuthnService->verifyAuthentication($assertionResponse);
$request = $request->withAttribute('webauthn.user_handle', $credential->userHandle);
// 3. The guard resolves the user from the verified attribute
$user = $manager->guard('webauthn')->authenticate($request);
// 4. Dispatch audit event
$dispatcher->dispatch(new PasskeyAuthenticated(
userId: $user->getAuthIdentifier(),
credentialId: base64_encode($credential->credentialId),
ipAddress: $serverParams['REMOTE_ADDR'] ?? null,
));
use MonkeysLegion\Auth\Attribute\Authenticated;
use MonkeysLegion\Auth\Attribute\RequiresRole;
use MonkeysLegion\Auth\Attribute\RequiresPermission;
use MonkeysLegion\Auth\Attribute\RateLimit;
#[Authenticated(guard: 'jwt')]
#[RequiresRole(['admin', 'editor'], mode: 'any')]
#[RateLimit(maxAttempts: 60, decaySeconds: 60)]
class ArticleController
{
#[RequiresPermission('articles.publish')]
public function publish(int $id): Response
{
// Only authenticated users with admin/editor role
// and articles.publish permission can reach here
}
}
use MonkeysLegion\Auth\Service\PasswordHasher;
use MonkeysLegion\Auth\DTO\PasswordPolicy;
$hasher = new PasswordHasher(
policy: new PasswordPolicy(
minLength: 12,
true
$hasher->needsRehash($hash); // false
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.