geggleto/securejwt

0.0.1 2016-05-27 15:52 UTC

This package is auto-updated.

Last update: 2024-08-29 03:29:53 UTC


README

预需求

Libsodium 已在您的环境中安装和配置。ParagonIE 的朋友们有一个非常好的文档可以帮助您。 在此阅读

安装

composer require geggleto/securejwt

用法

  1. 生成安全密钥 [提供了一个脚本 scripts/generateSecretKey.php]

  2. 加密令牌

    $config = new \Lcobucci\JWT\Builder(); // This object helps to simplify the creation of the dependencies
    // instead of using "?:" on constructors.

    $token = $config->setIssuer('http://example.com') // Configures the issuer (iss claim)
        ->setAudience('http://example.org') // Configures the audience (aud claim)
        ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item
        ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim)
        ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim)
        ->setExpiration(time() + 3600) // Configures the expiration time of the token (exp claim)
        ->set('uid', 1) // Configures a new claim, called "uid"
        ->getToken(); // Retrieves the generated token

    $secureJwt = new \SecureJwt\SecureJwt('./sec/encryption.key');

    $securedToken = $secureJwt->encryptToken((string)$token); //<--- This is the encrypted token
  1. 解密令牌
    $tokenString = $secureJwt->decryptToken($securedToken);

    $newToken = (new \Lcobucci\JWT\Parser())->parse($tokenString);