Giải mã($jwt php)

Tùy chọn, cài đặt gói

composer require paragonie/sodium_compat
2 từ trình soạn thảo nếu php của bạn < 7. 2 hoặc chưa cài đặt libsodium

composer require paragonie/sodium_compat

Thí dụ

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$key = 'example_key';
$payload = [
    'iss' => '//example.org',
    'aud' => '//example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * //tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
$jwt = JWT::encode[$payload, $key, 'HS256'];
$decoded = JWT::decode[$jwt, new Key[$key, 'HS256']];

print_r[$decoded];

/*
 NOTE: This will now be an object instead of an associative array. To get
 an associative array, you will need to cast it as such:
*/

$decoded_array = [array] $decoded;

/**
 * You can add a leeway to account for when there is a clock skew times between
 * the signing and verifying servers. It is recommended that this leeway should
 * not be bigger than a few minutes.
 *
 * Source: //self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
 */
JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode[$jwt, new Key[$key, 'HS256']];

Ví dụ với RS256 [openssl]

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$privateKey =  'example.org',
    'aud' => 'example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

$jwt = JWT::encode[$payload, $privateKey, 'RS256'];
echo "Encode:\n" . print_r[$jwt, true] . "\n";

// Get public key from the private key, or pull from from a file.
$publicKey = openssl_pkey_get_details[$privateKey]['key'];

$decoded = JWT::decode[$jwt, new Key[$publicKey, 'RS256']];
echo "Decode:\n" . print_r[[array] $decoded, true] . "\n";

Ví dụ với EdDSA [chữ ký libsodium và Ed25519]

use Firebase\JWT\JWT;
use Firebase\JWT\Key;

// Public and private keys are expected to be Base64 encoded. The last
// non-empty line is used so that keys can be generated with
// sodium_crypto_sign_keypair[]. The secret keys generated by other tools may
// need to be adjusted to match the input expected by libsodium.

$keyPair = sodium_crypto_sign_keypair[];

$privateKey = base64_encode[sodium_crypto_sign_secretkey[$keyPair]];

$publicKey = base64_encode[sodium_crypto_sign_publickey[$keyPair]];

$payload = [
    'iss' => 'example.org',
    'aud' => 'example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

$jwt = JWT::encode[$payload, $privateKey, 'EdDSA'];
echo "Encode:\n" . print_r[$jwt, true] . "\n";

$decoded = JWT::decode[$jwt, new Key[$publicKey, 'EdDSA']];
echo "Decode:\n" . print_r[[array] $decoded, true] . "\n";

Sử dụng JWK

use Firebase\JWT\JWK;
use Firebase\JWT\JWT;

// Set of keys. The "keys" key is required. For example, the JSON response to
// this endpoint: //www.gstatic.com/iap/verify/public_key-jwk
$jwks = ['keys' => []];

// JWK::parseKeySet[$jwks] returns an associative array of **kid** to Firebase\JWT\Key
// objects. Pass this as the second parameter to JWT::decode.
JWT::decode[$payload, JWK::parseKeySet[$jwks]];

Sử dụng bộ khóa được lưu trong bộ nhớ cache

Lớp

composer require paragonie/sodium_compat
3 có thể được sử dụng để tìm nạp và lưu trữ JWKS [Bộ khóa Web JSON] từ một URI công khai. Điều này có những ưu điểm sau

  1. Các kết quả được lưu trữ cho hiệu suất
  2. Nếu một phím không được nhận dạng được yêu cầu, bộ đệm sẽ được làm mới để phù hợp với việc xoay phím
  3. Nếu giới hạn tốc độ được bật, URI JWKS sẽ không thực hiện quá 10 yêu cầu mỗi giây

________số 8

Điều khoản khác

Xử lý ngoại lệ

Khi một cuộc gọi đến

composer require paragonie/sodium_compat
4 không hợp lệ, nó sẽ đưa ra một trong các ngoại lệ sau

composer require paragonie/sodium_compat
0

Tất cả các ngoại lệ trong không gian tên

composer require paragonie/sodium_compat
5 mở rộng
composer require paragonie/sodium_compat
6 và có thể được đơn giản hóa như thế này

composer require paragonie/sodium_compat
3

Truyền tới mảng

Giá trị trả về của

composer require paragonie/sodium_compat
4 là đối tượng PHP chung chung
composer require paragonie/sodium_compat
8. Thay vào đó, nếu bạn muốn xử lý bằng mảng, bạn có thể thực hiện như sau

composer require paragonie/sodium_compat
0

bài kiểm tra

Chạy thử nghiệm bằng phpunit

composer require paragonie/sodium_compat
1

Dòng mới trong khóa riêng

Nếu khóa riêng của bạn chứa

composer require paragonie/sodium_compat
9 ký tự, hãy nhớ đặt nó trong dấu ngoặc kép
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$key = 'example_key';
$payload = [
    'iss' => '//example.org',
    'aud' => '//example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * //tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
$jwt = JWT::encode[$payload, $key, 'HS256'];
$decoded = JWT::decode[$jwt, new Key[$key, 'HS256']];

print_r[$decoded];

/*
 NOTE: This will now be an object instead of an associative array. To get
 an associative array, you will need to cast it as such:
*/

$decoded_array = [array] $decoded;

/**
 * You can add a leeway to account for when there is a clock skew times between
 * the signing and verifying servers. It is recommended that this leeway should
 * not be bigger than a few minutes.
 *
 * Source: //self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
 */
JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode[$jwt, new Key[$key, 'HS256']];
0 chứ không phải dấu ngoặc đơn
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

$key = 'example_key';
$payload = [
    'iss' => '//example.org',
    'aud' => '//example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

/**
 * IMPORTANT:
 * You must specify supported algorithms for your application. See
 * //tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
 * for a list of spec-compliant algorithms.
 */
$jwt = JWT::encode[$payload, $key, 'HS256'];
$decoded = JWT::decode[$jwt, new Key[$key, 'HS256']];

print_r[$decoded];

/*
 NOTE: This will now be an object instead of an associative array. To get
 an associative array, you will need to cast it as such:
*/

$decoded_array = [array] $decoded;

/**
 * You can add a leeway to account for when there is a clock skew times between
 * the signing and verifying servers. It is recommended that this leeway should
 * not be bigger than a few minutes.
 *
 * Source: //self-issued.info/docs/draft-ietf-oauth-json-web-token.html#nbfDef
 */
JWT::$leeway = 60; // $leeway in seconds
$decoded = JWT::decode[$jwt, new Key[$key, 'HS256']];
1 để diễn giải chính xác các ký tự thoát

Chủ Đề