-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.php
82 lines (74 loc) · 2.87 KB
/
jwt.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
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class JWT{
public static function create(array $data, $secret_key, $expire = 3600){
$header = json_encode(['alg' => 'HS256', 'typ' => 'JWT']);
$time = time();
$values = array(
'jti' => base64_encode(openssl_random_pseudo_bytes(32)),
'iat' => $time,
'exp' => $time + $expire,
'data' => $data
);
$payload = json_encode($values);
$base64url_header = self::base64url_encode($header);
$base64url_payload = self::base64url_encode($payload);
$signature = hash_hmac('sha256', $base64url_header . "." . $base64url_payload, $secret_key, true);
$base64url_signature = self::base64url_encode($signature);
$jwt = $base64url_header . "." . $base64url_payload . "." . $base64url_signature;
return $jwt;
}
public static function verify_signature($jwt, $secret_key){
$jwt_values = explode('.', $jwt);
if(count($jwt_values)<3) return false;
$received_signature = $jwt_values[2];
$received_header_payload = $jwt_values[0] . "." . $jwt_values[1];
$resulted_signature = self::base64url_encode(hash_hmac('sha256', $received_header_payload, $secret_key, true));
if($resulted_signature == $received_signature)
return true;
else
return false;
}
public static function check_exp($jwt, $secret_key){
$payload = self::get_payload($jwt, $secret_key);
if($payload!=NULL)
if(time()>$payload['exp'])
return true;
else
return false;
else
return true;
}
public static function verify($jwt, $secret_key){
if(self::verify_signature($jwt, $secret_key))
if(!self::check_exp($jwt, $secret_key))
return 0;
else
return 2;
else
return 1;
}
public static function get_data($jwt, $secret_key){
if(self::verify_signature($jwt, $secret_key)){
$jwt_values = explode('.', $jwt);
$payload = json_decode(self::base64url_decode($jwt_values[1]),true);
return $payload['data'];
}else{
return NULL;
}
}
public static function get_payload($jwt, $secret_key){
if(self::verify_signature($jwt, $secret_key)){
$jwt_values = explode('.', $jwt);
$payload = json_decode(self::base64url_decode($jwt_values[1]),true);
return $payload;
}else{
return NULL;
}
}
public static function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
public static function base64url_decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
}