PHP code example of nimbly / proof
1. Go to this page and download the library: Download nimbly/proof 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/ */
nimbly / proof example snippets
$proof = new Proof(
new KeypairSigner(
Proof::ALGO_SHA256,
\openssl_get_publickey($public_key_contents),
\openssl_get_privatekey($private_key_contents)
)
);
$token = new Token([
"iss" => "customer-data-service",
"sub" => $user->id,
"iat" => \time(),
"exp" => \strtotime("+1 hour")
]);
$jwt = $proof->encode($token);
$token = $proof->decode($jwt);
$token = new Token([
"iss" => "customer-data-service",
"sub" => $user->id,
"custom_claim_foo" => "bar",
"exp" => \strtotime("+1 hour")
])
$token->setClaim("nbf", \strtotime("+1 week"));
$jwt = $proof->encode($token);
$token = $proof->decode($jwt);
$subject = $token->getClaim("sub");
if( $token->hasClaim("sub") ){
// Load User from DB
}
$claims = $token->toArray();
$hmacSigner = new HmacSigner(
Proof::ALGO_SHA256,
$secretsManager->getSecret("jwt_signing_key")
);
$keypairSigner = new KeypairSigner(
Proof::ALGO_SHA256,
\openssl_get_publickey($secretsManager->getSecret("public_key")),
\openssl_get_privatekey($secretsManager->getSecret("private_key"))
);
$proof = new Proof(
signer: $signer,
keyMap: [
"1234" => $signer,
"5678" => $signer2
]
);
$proof->encode($token, "5678");
new Nimbly\Proof\Middleware\ValidateJwtMiddleware(
proof: $proof,
header: "X-Custom-Header",
scheme: null
);
class AuthorizeUserMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$token = $request->getAttribute(Nimbly\Proof\Token::class);
if( empty($token) || $token->hasClaim("sub") === false ){
throw new UnauthorizedHttpException("Bearer", "Please login to continue.");
}
$user = App\Models\User::find($token->getClaim("sub"));
if( empty($user) ){
throw new UnauthorizedHttpException("Bearer", "Please login to continue.");
}
$request = $request->withAttribute(App\Models\User::class, $user);
return $handler->handle($request);
}
}