1
1
<?php
2
2
3
3
/**
4
- * JSON Web Token implementation
5
- *
6
- * Minimum implementation used by Realtime auth, based on this spec:
7
- * http://self-issued.info/docs/draft-jones-json-web-token-01.html.
4
+ * JSON Web Token implementation, based on this spec:
5
+ * http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06
8
6
*
9
7
* @author Neuman Vong <[email protected] >
8
+ * @author Anant Narayanan <[email protected] >
10
9
*/
11
10
class JWT
12
11
{
13
12
/**
14
- * @param string $jwt The JWT
15
- * @param string|null $key The secret key
16
- * @param bool $verify Don't skip verification process
13
+ * Decodes a JWT string into a PHP object.
14
+ *
15
+ * @access public
16
+ * @param string $jwt The JWT
17
+ * @param string|null $key The secret key
18
+ * @param bool $verify Don't skip verification process
17
19
*
18
- * @return object The JWT's payload as a PHP object
20
+ * @return object The JWT's payload as a PHP object
21
+ * @uses jsonDecode
22
+ * @uses urlsafeB64Decode
19
23
*/
20
24
public static function decode ($ jwt , $ key = null , $ verify = true )
21
25
{
@@ -24,12 +28,10 @@ public static function decode($jwt, $key = null, $verify = true)
24
28
throw new UnexpectedValueException ('Wrong number of segments ' );
25
29
}
26
30
list ($ headb64 , $ payloadb64 , $ cryptob64 ) = $ tks ;
27
- if (null === ($ header = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ headb64 )))
28
- ) {
31
+ if (null === ($ header = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ headb64 )))) {
29
32
throw new UnexpectedValueException ('Invalid segment encoding ' );
30
33
}
31
- if (null === $ payload = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ payloadb64 ))
32
- ) {
34
+ if (null === $ payload = JWT ::jsonDecode (JWT ::urlsafeB64Decode ($ payloadb64 ))) {
33
35
throw new UnexpectedValueException ('Invalid segment encoding ' );
34
36
}
35
37
$ sig = JWT ::urlsafeB64Decode ($ cryptob64 );
@@ -45,11 +47,16 @@ public static function decode($jwt, $key = null, $verify = true)
45
47
}
46
48
47
49
/**
48
- * @param object|array $payload PHP object or array
49
- * @param string $key The secret key
50
- * @param string $algo The signing algorithm
50
+ * Converts and signs a PHP object or array into a JWT string.
51
+ *
52
+ * @access public
53
+ * @param object|array $payload PHP object or array
54
+ * @param string $key The secret key
55
+ * @param string $algo The signing algorithm
51
56
*
52
- * @return string A JWT
57
+ * @return string A signed JWT
58
+ * @uses jsonEncode
59
+ * @uses urlsafeB64Encode
53
60
*/
54
61
public static function encode ($ payload , $ key , $ algo = 'HS256 ' )
55
62
{
@@ -67,11 +74,14 @@ public static function encode($payload, $key, $algo = 'HS256')
67
74
}
68
75
69
76
/**
70
- * @param string $msg The message to sign
71
- * @param string $key The secret key
72
- * @param string $method The signing algorithm
77
+ * Sign a string with a given key and algorithm.
73
78
*
74
- * @return string An encrypted message
79
+ * @access public
80
+ * @param string $msg The message to sign
81
+ * @param string $key The secret key
82
+ * @param string $method The signing algorithm
83
+ *
84
+ * @return string An encrypted message
75
85
*/
76
86
public static function sign ($ msg , $ key , $ method = 'HS256 ' )
77
87
{
@@ -87,43 +97,50 @@ public static function sign($msg, $key, $method = 'HS256')
87
97
}
88
98
89
99
/**
90
- * @param string $input JSON string
100
+ * Decode a JSON string into a PHP object.
101
+ *
102
+ * @access public
103
+ * @param string $input JSON string
91
104
*
92
- * @return object Object representation of JSON string
105
+ * @return object Object representation of JSON string
93
106
*/
94
107
public static function jsonDecode ($ input )
95
108
{
96
109
$ obj = json_decode ($ input );
97
110
if (function_exists ('json_last_error ' ) && $ errno = json_last_error ()) {
98
111
JWT ::handleJsonError ($ errno );
99
- }
100
- else if ($ obj === null && $ input !== 'null ' ) {
112
+ } else if ($ obj === null && $ input !== 'null ' ) {
101
113
throw new DomainException ('Null result with non-null input ' );
102
114
}
103
115
return $ obj ;
104
116
}
105
117
106
118
/**
107
- * @param object|array $input A PHP object or array
119
+ * Encode a PHP object into a JSON string.
120
+ *
121
+ * @access public
122
+ * @param object|array $input A PHP object or array
108
123
*
109
- * @return string JSON representation of the PHP object or array
124
+ * @return string JSON representation of the PHP object or array
110
125
*/
111
126
public static function jsonEncode ($ input )
112
127
{
113
128
$ json = json_encode ($ input );
114
129
if (function_exists ('json_last_error ' ) && $ errno = json_last_error ()) {
115
130
JWT ::handleJsonError ($ errno );
116
- }
117
- else if ($ json === 'null ' && $ input !== null ) {
131
+ } else if ($ json === 'null ' && $ input !== null ) {
118
132
throw new DomainException ('Null result with non-null input ' );
119
133
}
120
134
return $ json ;
121
135
}
122
136
123
137
/**
124
- * @param string $input A base64 encoded string
138
+ * Decode a string with URL-safe Base64.
125
139
*
126
- * @return string A decoded string
140
+ * @access public
141
+ * @param string $input A Base64 encoded string
142
+ *
143
+ * @return string A decoded string
127
144
*/
128
145
public static function urlsafeB64Decode ($ input )
129
146
{
@@ -136,19 +153,23 @@ public static function urlsafeB64Decode($input)
136
153
}
137
154
138
155
/**
139
- * @param string $input Anything really
156
+ * Encode a string with URL-safe Base64.
157
+ *
158
+ * @access public
159
+ * @param string $input The string you want encoded
140
160
*
141
- * @return string The base64 encode of what you passed in
161
+ * @return string The base64 encode of what you passed in
142
162
*/
143
163
public static function urlsafeB64Encode ($ input )
144
164
{
145
165
return str_replace ('= ' , '' , strtr (base64_encode ($ input ), '+/ ' , '-_ ' ));
146
166
}
147
167
148
168
/**
149
- * @param int $errno An error number from json_last_error()
169
+ * @access private
170
+ * @param int $errno An error number from json_last_error()
150
171
*
151
- * @return void
172
+ * @return void
152
173
*/
153
174
private static function handleJsonError ($ errno )
154
175
{
0 commit comments