-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToken.php
138 lines (98 loc) · 4.32 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php namespace rogoss\OAuth;
require_once __DIR__ . "/TokenBase.php";
require_once __DIR__ . "/iTokenUser.php";
class Token extends TokenBase implements iTokenUser{
/** @ignore */
public function validateString(string $sField, $mValue) : string {
$sValue = trim("". $mValue);
if(empty($sValue))
throw new TokenException("value of '{$sField}' is empty", TokenException::VALUE_EMPTY);
return $sValue;
}
/** @ignore */
public function validateHash(string $sField, $mValue) : string {
$sValue = trim("". $mValue);
if(!preg_match("/^[0-9a-f]+$/i", $sValue))
throw new TokenException("value of '{$sField}' is not a hash", TokenException::VALUE_NOT_HASH);
return $sValue;
}
/** @ignore */
public function validateInt(string $sField, $mValue) : int {
if(!is_numeric($mValue))
throw new TokenException("value of '{$sField}' is not numeric", TokenException::VALUE_NOT_NUMMERIC);
return intval($mValue);
}
public static function blank() : iTokenUser {
return new static();
}
public static function withSecret(string $sSecret) : iTokenUser {
return new static($sSecret);
}
public function parseToken(string $sValue, bool $bBase64 = false) : iTokenUser {
if($bBase64)
$sValue = base64_decode($sValue);
if(empty($aJSON = json_decode($sValue, true)))
throw new TokenException("given Value is not JSON", TokenException::VALUE_NOT_JSON);
if(!isset($aJSON['s']))
throw new TokenException("Missing field 's' ", TokenException::INVALID_TOKEN_STRUCT);
$sSignature = $aJSON['s'];
unset($aJSON['s']);
if(!isset($aJSON['m']))
throw new TokenException("Missing field 'm' ", TokenException::INVALID_TOKEN_STRUCT);
$sTokenMode = $aJSON['m'];
unset($aJSON['m']);
$aModes = str_split($sTokenMode);
$iModeCnt = count($aModes);
for($a = 0; $a < $iModeCnt; $a++) {
$sMode = $aModes[$a];
switch($sMode) {
case "a":
$sField = "a";
$sProp = "sAgentToken";
$sValidator = "validateHash";
break;
case "t":
$sField = "ttl";
$sProp = "iTTL";
$sValidator = "validateInt";
break;
case "c":
$sField = "c";
$sProp = "sContent";
$sValidator = "validateString";
break;
break;
default:
throw new TokenException("unknown tokenmode '{$sTokenMode}'", TokenException::INVALID_TOKEN_STRUCT);
}
if(!isset($aJSON[$sField]))
throw new TokenException("token is missing field '{$sField}'", TokenException::INVALID_TOKEN_STRUCT);
if(empty($aJSON[$sField]))
throw new TokenException("token field '{$sField}' is empty", TokenException::VALUE_EMPTY);
$mValue = $aJSON[$sField];
unset($aJSON[$sField]);
$this->$sProp = $this->$sValidator($sField, $mValue);
}
if(count($aJSON) > 0)
throw new TokenException("token fields do not match with given mode (".implode(", ", array_keys($aJSON)). ")", TokenException::INVALID_TOKEN_STRUCT);
if(strcmp($sSignature, $this->getSignature()))
throw new TokenException("token signature missmatch", TokenException::INVALID_TOKEN_STRUCT);
return $this;
}
public function requiresAgent($sAgent): iTokenUser {
if(strcmp($this->hashAgent($sAgent), $this->sAgentToken))
throw new TokenException("token agent missmatch", TokenException::AGENT_MISSMATCH);
return $this;
}
public function requiresNoneExpired(): iTokenUser {
if($this->iTTL > 0 and (time() - $this->iTTL) >= 0)
throw new TokenException("token expired", TokenException::TOKEN_EXPIRED);
return $this;
}
public function getContent(): string {
return $this->sContent;
}
private function __construct($sSecret = "") {
parent::__construct($sSecret);
}
}