-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.php
70 lines (56 loc) · 1.92 KB
/
Token.php
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
<?php
class Token{
/**
* Sign - Static method to generate token
*
* @param array $payload
* @param string $key - The signature key
* @param int $expire - (optional) Max age of token in seconds. Leave it blank for no expiration.
*
* @return string token
*/
static function Sign($payload, $key, $expire = null){
// Header
$headers = ['algo'=>'HS256', 'type'=>'JWT', 'expire' => time()+$expire];
if($expire){
$headers['expire'] = time()+$expire;
}
$headers_encoded = base64_encode(json_encode($headers));
// Payload
$payload['time'] = time();
$payload_encoded = base64_encode(json_encode($payload));
// Signature
$signature = hash_hmac('SHA256',$headers_encoded.$payload_encoded,$key);
$signature_encoded = base64_encode($signature);
// Token
$token = $headers_encoded . '.' . $payload_encoded .'.'. $signature_encoded;
return $token;
}
/**
* Verify - Static method verify token
*
* @param string $token
* @param string $key - The signature key
*
* @return boolean false if token is invalid or expired
* @return array payload
*/
static function Verify($token, $key){
// Break token parts
$token_parts = explode('.', $token);
// Verigy Signature
$signature = base64_encode(hash_hmac('SHA256',$token_parts[0].$token_parts[1],$key));
if($signature != $token_parts[2]){
return false;
}
// Decode headers & payload
$headers = json_decode(base64_decode($token_parts[0]), true);
$payload = json_decode(base64_decode($token_parts[1]), true);
// Verify validity
if(isset($headers['expire']) && $headers['expire'] < time()){
return false;
}
// If token successfully verified
return $payload;
}
}