示例#1
0
 public static function generate_jwt($user)
 {
     $issuedAt = time();
     $tokenId = base64_encode(Random::key(32));
     $serverName = Config::get('serverName');
     /*
      * Create the token as an array
      */
     $data = ['iat' => $issuedAt, 'jti' => $tokenId, 'iss' => $serverName, 'exp' => $issuedAt + 1800, 'data' => ['userId' => $user->id, 'userName' => $user->username]];
     /*
      * Extract the key, which is coming from the config file.
      *
      * Generated with base64_encode(openssl_random_pseudo_bytes(64));
      */
     $secretKey = base64_decode(Config::get('jwt')['key']);
     /*
      * Extract the algorithm from the config file too
      */
     $algorithm = Config::get('jwt')['algorithm'];
     /*
      * Encode the array to a JWT string.
      * Second parameter is the key to encode the token.
      *
      * The output string can be validated at http://jwt.io/
      */
     $jwt = JWT::encode($data, $secretKey, $algorithm);
     return $jwt;
 }