ngphp/jwt

NGPHP/JWT 是一个简单的 PHP 库,用于编码和解码 JSON Web Tokens (JWT)。该库是 NGPHP 框架的一部分,由 Vedat Yıldırım 开发。它提供了生成、验证和解码 JWT 令牌的方法,符合当前规范。

1.0.0 2024-06-13 13:37 UTC

This package is auto-updated.

Last update: 2024-09-05 12:03:16 UTC


README

NGPHP/JWT 是一个简单的 PHP 库,用于编码和解码 JSON Web Tokens (JWT)。该库是 NGPHP 框架的一部分,由 Vedat Yıldırım 开发。它提供了生成、验证和解码 JWT 令牌的方法,符合当前规范。

安装

您可以通过 Composer 安装此包: https://packagist.org.cn/packages/ngphp/jwt

composer install ngphp/jwt

示例

文件:vendor/ngphp/src/example.php 网址: https:///jwt/example.php

用法

生成 JWT

// without composer: require 'JWT.php';

// with composer
require 'vendor/autoload.php';

use ngphp\JWT;

// Initialize the JWT handler with a secret key
$secret = 'your_secret_key';
$jwt = new JWT($secret);

// Example payload
$payload = [
    'user_id' => 1,
    'username' => 'john.doe',
    'email' => 'john.doe@example.com',
    'role' => 'admin',
    'iat' => time(),
    'exp' => time() + 3600 // 1 hour expiration
];

// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n";

验证 JWT

// Example headers containing the JWT
$headers = [
    'Authorization' => 'Bearer ' . $token
];

// Authenticate the user using the token from the headers
try {
    if ($jwt->authenticate($headers)) {
        $user = JWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

解码 JWT

// Decode the token directly
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

示例

以下是一个更详细的示例,演示了生成、存储、验证和解码 JWT。

require 'vendor/autoload.php';

use leanphp\jwt\JWT;

// Initialize the JWT handler with a secret key
$secret = 'your_secret_key';
$jwt = new JWT($secret);

// Example payload
$payload = [
    'user_id' => 1,
    'username' => 'john.doe',
    'email' => 'john.doe@example.com',
    'role' => 'admin',
    'iat' => time(),
    'exp' => time() + 3600 // 1 hour expiration
];

// Generate a JWT
$token = $jwt->generate($payload);
echo "Generated Token: " . $token . "\n\n";

// Simulate storing the token in a database
echo "Storing the token in the database...\n\n";
// Example of database storage logic (not implemented here)
// storeTokenInDatabase($user_id, $token);

// Example headers containing the JWT
$headers = [
    'Authorization' => 'Bearer ' . $token
];

// Authenticate the user using the token from the headers
echo "Authenticating User...\n";
try {
    if ($jwt->authenticate($headers)) {
        $user = JWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
        echo "\n";
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

// Decode the token directly
echo "Decoding the Token...\n";
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
    echo "\n";
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

// Example output separation
echo "\n--------------------------\n";
echo "Example 1: Token Generation\n";
echo "Generated Token: " . $token . "\n";

echo "\n--------------------------\n";
echo "Example 2: Token Authentication\n";
try {
    if ($jwt->authenticate($headers)) {
        $user = JWT::getUser();
        echo "Authenticated User: \n";
        print_r($user);
        echo "\n";
    }
} catch (\Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}

echo "\n--------------------------\n";
echo "Example 3: Token Decoding\n";
try {
    $decoded = $jwt->decode($token);
    echo "Decoded Token: \n";
    print_r($decoded);
    echo "\n";
} catch (\Exception $e) {
    echo "Decoding failed: " . $e->getMessage() . "\n";
}

许可证 本项目采用 MIT 许可证。有关更多详细信息,请参阅 LICENSE 文件。

致谢 该库是 LeanPHP 框架的一部分,由 Vedat Yıldırım 开发。

贡献 如果您发现任何问题或有改进建议,请随时在 GitHub 上提交问题或拉取请求。

README.md 提供了 JWT 库的概述,包括安装说明、用法示例以及每个方法的详细解释。它还包括一个完整的示例,演示了如何生成、验证和解码 JWT 令牌。仓库名称 php-jwt 简洁且搜索引擎优化友好,使用户在搜索 PHP 中的 JWT 解决方案时更容易找到您的项目。