Ticket.php
6.91 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?php
/**
* Implements CAS tickets for the plugin's CAS server.
*
* @version 1.1.0
* @since 1.1.0
*/
namespace Cassava\CAS;
use Cassava\Exception\TicketException;
use Cassava\Options;
use Cassava\Plugin;
/**
* Class that implements CAS tickets.
*
* @version 1.1.0
* @since 1.1.0
*/
class Ticket {
/**
* Service Ticket
*/
const TYPE_ST = 'ST';
/**
* Proxy Ticket
*/
const TYPE_PT = 'PT';
/**
* Proxy-Granting Ticket
*/
const TYPE_PGT = 'PGT';
/**
* Proxy-Granting Ticket IOU
*/
const TYPE_PGTIOU = 'PGTIOU';
/**
* Ticket-Granting Cookie
*/
const TYPE_TGC = 'TGC';
/**
* Login Ticket
*/
const TYPE_LT = 'LT';
/**
* Ticket type.
* @var string
*/
public $type;
/**
* Authenticated WordPress user who owns the ticket.
* @var WP_User
*/
public $user;
/**
* URL for the service that requested authentication.
* @var string
*/
public $service;
/**
* Expiration timestamp, in seconds.
* @var float
*/
public $expires;
/**
* CAS ticket constructor.
*
* @param string $type Ticket type.
* @param WP_User $user Authenticated WordPress user who owns the ticket.
* @param string $service URL for the service that requested authentication.
* @param double $expires Expiration timestamp, in seconds.
* Freshly generated tickets should not provide this value.
*
* @todo "Remember-Me" tickets should have an expiration date of up to 3 months.
*/
public function __construct( $type, $user, $service, $expires = 0.0 ) {
$this->type = $type;
$this->user = $user;
$this->service = esc_url_raw( $service );
$this->expires = $expires;
/**
* Freshly generated tickets have no expiration timestamp:
*/
if ( ! $expires ) {
$expiration = Options::get( 'expiration', 30 );
/**
* This filter allows developers to override the default ticket expiration period.
*
* @param int $expiration Ticket expiration period (in seconds).
* @param string $type Type of ticket to set.
* @param WP_User $user Authenticated user associated with the ticket.
*/
$expiration = \apply_filters( 'cas_server_ticket_expiration', $expiration, $type, $user );
$this->expires = microtime( true ) + $expiration;
$this->markUnused();
}
}
/**
* Magic method that returns the ticket as a string.
*
* @return string Ticket as string.
*/
public function __toString() {
return $this->encodeTicket();
}
/**
* Create a new ticket instance from a ticket string.
*
* @param string $string Ticket string.
* @return Ticket Ticket object.
*
* @throws \Cassava\Exception\TicketException
*
* @uses \__()
* @uses \get_user_by()
* @uses \is_wp_error()
*/
public static function fromString( $string ) {
$components = static::decodeTicket( $string );
if ( $components['expires'] < time() ) {
throw new TicketException( __( 'Expired ticket.', 'wp-cas-server' ) );
}
$user = \get_user_by( 'login', $components['login'] );
if ( ! $user || \is_wp_error( $user ) ) {
throw new TicketException( __( 'No user matches ticket.', 'wp-cas-server' ) );
}
$ticket = new static( $components['type'], $user, $components['service'], $components['expires'] );
if ( $ticket->generateSignature() !== $components['signature'] ) {
throw new TicketException( __( 'Corrupted ticket.', 'wp-cas-server' ) );
}
if ( $ticket->isUsed() ) {
throw new TicketException( __( 'Unknown or used ticket.', 'wp-cas-server' ) );
}
return $ticket;
}
/**
* Serialize ticket components into a string.
*
* @return string Ticket string.
*/
protected function encodeTicket() {
$components = array(
$this->user->user_login, // 1. User login
urlencode( $this->service ), // 2. Service URL
$this->expires, // 3. Expiration timestamp
$this->generateSignature(), // 4. Cryptographic signature
);
return $this->type . '-' . base64_encode( implode( '|', $components ) );
}
/**
* Extracts components from a base64 encoded ticket string.
*
* @param string $ticket Ticket string (minus the prefix).
* @return array Ticket components.
*
* @throws \Cassava\Exception\TicketException
*
* @uses \__()
*/
protected static function decodeTicket( $ticket ) {
if ( strpos( $ticket, '-' ) === false ) {
$ticket = static::TYPE_ST . '-' . $ticket;
}
list( $type, $content ) = explode( '-', $ticket, 2 );
$values = explode( '|', base64_decode( $content ) );
if (count( $values ) < 4) {
throw new TicketException( __( 'Malformed ticket.', 'wp-cas-server' ) );
}
$keys = array( 'login', 'service', 'expires', 'signature' );
$components = array_combine( $keys, $values );
$components['type'] = $type;
$components['service'] = urldecode( $components['service'] );
return $components;
}
/**
* Generate security key for a ticket.
*
* @return string Generated security key.
*
* @uses \wp_hash()
*/
protected function generateKey() {
$keyComponents = array(
$this->user->user_login,
substr( $this->user->user_pass, 8, 4 ),
$this->expires,
);
return \wp_hash( implode( '|', $keyComponents ) );
}
/**
* Create a ticket signature by concatenating components and signing them with a key.
*
* @return string Generated signature hash.
*/
public function generateSignature() {
$signatureComponents = array(
$this->user->login,
$this->service,
$this->expires,
);
return hash_hmac( 'sha1', implode( '|', $signatureComponents ), $this->generateKey() );
}
/**
* Validates a ticket string against a list of expected types.
*
* @param string $ticket Ticket string to validate.
* @param array $types List of allowed type prefixes.
*
* @throws \Cassava\Exception\TicketException
*/
public static function validateAllowedTypes( $ticket, $types = array() ) {
list( $type ) = explode( '-', $ticket, 2 );
if ( ! in_array( $type, $types ) ) {
throw new TicketException( __( 'Ticket type cannot be validated.', 'wp-cas-server' ) );
}
}
/**
* Remember a fresh ticket using WordPress's Transients API.
*
* @uses \set_transient()
*/
protected function markUnused() {
$key = $this->generateKey();
\set_transient( Plugin::TRANSIENT_PREFIX . $key, (string) $this, $this->expires );
}
/**
* Remember a ticket as having been used using WordPress's Transients API.
*
* @uses \delete_transient()
*
* @todo "Remember-Me" tickets should not be invalidated.
*/
public function markUsed() {
$key = $this->generateKey();
\delete_transient( Plugin::TRANSIENT_PREFIX . $key );
}
/**
* Checks whether a ticket has been used using WordPress's Transients API.
*
* @return boolean Whether the ticket has been used.
*
* @uses \get_transient()
*/
public function isUsed() {
if ( Options::get( 'allow_ticket_reuse' ) ) {
return false;
}
$key = $this->generateKey();
return ! \get_transient( Plugin::TRANSIENT_PREFIX . $key );
}
}