-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_issuer.php
More file actions
92 lines (78 loc) · 2.64 KB
/
token_issuer.php
File metadata and controls
92 lines (78 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
//require_once('vendor/autoload.php');
require __DIR__ . '/vendor/autoload.php';
require_once("config.php");
/*
* Application setup, database connection, data sanitization and user
* validation routines are here.
*/
use \Firebase\JWT\JWT;
$credentialsAreValid = 0;
if($_POST['email']=="shishir.raven@gmail.com" && $_POST['password']=="123456")
{
$credentialsAreValid = 1;
$username_id = "1";
$username ="shishir";
}
/* For testing purposes I am setting this up. */
if ($credentialsAreValid)
{
$tokenId = base64_encode(mcrypt_create_iv(32));
$issuedAt = time();
$notBefore = $issuedAt + 1; //Adding 10 seconds
$expire = $notBefore + 60; // Adding 60 seconds
$serverName = $config['server_name']; // Retrieve the server name from config file
/*
* Create the token as an array
*/
$data = [
'iat' => $issuedAt, // Issued at: time when the token was generated
'jti' => $tokenId, // Json Token Id: an unique identifier for the token
'iss' => $serverName, // Issuer
'nbf' => $notBefore, // Not before
'exp' => $expire, // Expire
'data' => [ // Data related to the signer user
'userId' => $username_id, // userid from the users table
'userName' => $username, // User name
]
];
/*
* More code here...
*/
/*
* Code here...
*/
/*
* Extract the key, which is coming from the config file.
*
* Best suggestion is the key to be a binary string and
* store it in encoded in a config file.
*
* Can be generated with base64_encode(openssl_random_pseudo_bytes(64));
*
* keep it secure! You'll need the exact key to verify the
* token later.
*/
$secretKey = base64_decode($config['jwtKey']);
/*
echo $secretKey;
exit;*/
/*
* 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, //Data to be encoded in the JWT
$secretKey, // The signing key
'HS512' // Algorithm used to sign the token, see https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#section-3
);
$unencodedArray = ['jwt' => $jwt];
echo json_encode($unencodedArray);
} // if ($credentialsAreValid)
else
{
$unencodedArray = ['jwt' => "❌ error"];
echo json_encode($unencodedArray);
}