· 9 years ago · Nov 16, 2016, 11:32 AM
1$tokenId = base64_encode(mcrypt_create_iv(32));
2 $issuedAt = time();
3 $notBefore = $issuedAt + 10; //Adding 10 seconds
4 $expire = $notBefore + 60; // Adding 60 seconds
5 $serverName = $config->get('serverName'); // Retrieve the server name from config file
6
7 /*
8 * Create the token as an array
9 */
10 $data = [
11 'iat' => $issuedAt, // Issued at: time when the token was generated
12 'jti' => $tokenId, // Json Token Id: an unique identifier for the token
13 'iss' => $serverName, // Issuer
14 'nbf' => $notBefore, // Not before
15 'exp' => $expire, // Expire
16 'data' => [ // Data related to the signer user
17 'userId' => $rs['id'], // userid from the users table
18 'userName' => $username, // User name
19 ]
20 ];
21
22//-------------------------------------------
23
24$secretKey = base64_decode($config->get('jwtKey'));
25
26 /*
27 * Encode the array to a JWT string.
28 * Second parameter is the key to encode the token.
29 *
30 * The output string can be validated at http://jwt.io/
31 */
32 $jwt = JWT::encode(
33 $data, //Data to be encoded in the JWT
34 $secretKey, // The signing key
35 'HS512' // Algorithm used to sign the token
36 );
37
38 $unencodedArray = ['jwt' => $jwt];
39 echo json_encode($unencodedArray);