1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* simpleSAMLphp-casserver is a CAS 1.0 and 2.0 compliant CAS server in the form of a simpleSAMLphp module |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2013 Bjorn R. Jensen |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU Lesser General Public |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16
|
|
|
* Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Lesser General Public |
19
|
|
|
* License along with this library; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
declare(strict_types=1); |
25
|
|
|
|
26
|
|
|
namespace SimpleSAML\Module\casserver\Cas\Factories; |
27
|
|
|
|
28
|
|
|
use SimpleSAML\Configuration; |
29
|
|
|
use SimpleSAML\XML\Utils\Random; |
30
|
|
|
|
31
|
|
|
class TicketFactory |
32
|
|
|
{ |
33
|
|
|
/** @var int $serviceTicketExpireTime */ |
34
|
|
|
private int $serviceTicketExpireTime; |
35
|
|
|
|
36
|
|
|
/** @var int $proxyGrantingTicketExpireTime */ |
37
|
|
|
private int $proxyGrantingTicketExpireTime; |
38
|
|
|
|
39
|
|
|
/** @var int $proxyTicketExpireTime */ |
40
|
|
|
private int $proxyTicketExpireTime; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \SimpleSAML\Configuration $config |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Configuration $config) |
47
|
|
|
{ |
48
|
|
|
$this->serviceTicketExpireTime = $config->getOptionalValue('service_ticket_expire_time', 5); |
49
|
|
|
$this->proxyGrantingTicketExpireTime = $config->getOptionalValue('proxy_granting_ticket_expire_time', 3600); |
50
|
|
|
$this->proxyTicketExpireTime = $config->getOptionalValue('proxy_ticket_expire_time', 5); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $sessionId |
56
|
|
|
* @param int $expiresAt |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function createSessionTicket(string $sessionId, int $expiresAt): array |
60
|
|
|
{ |
61
|
|
|
$randomUtils = new Random(); |
62
|
|
|
return [ |
63
|
|
|
'id' => $sessionId, |
64
|
|
|
'validBefore' => $expiresAt, |
65
|
|
|
'renewId' => $randomUtils->generateID(), |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $content |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function createServiceTicket(array $content): array |
75
|
|
|
{ |
76
|
|
|
$randomUtils = new Random(); |
77
|
|
|
$id = str_replace('_', 'ST-', $randomUtils->generateID()); |
78
|
|
|
$expiresAt = time() + $this->serviceTicketExpireTime; |
79
|
|
|
|
80
|
|
|
return array_merge(['id' => $id, 'validBefore' => $expiresAt], $content); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $content |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
public function createProxyGrantingTicket(array $content): array |
89
|
|
|
{ |
90
|
|
|
$randomUtils = new Random(); |
91
|
|
|
$id = str_replace('_', 'PGT-', $randomUtils->generateID()); |
92
|
|
|
$iou = str_replace('_', 'PGTIOU-', $randomUtils->generateID()); |
93
|
|
|
|
94
|
|
|
$expireAt = time() + $this->proxyGrantingTicketExpireTime; |
95
|
|
|
|
96
|
|
|
return array_merge(['id' => $id, 'iou' => $iou, 'validBefore' => $expireAt], $content); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $content |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function createProxyTicket(array $content): array |
105
|
|
|
{ |
106
|
|
|
$randomUtils = new Random(); |
107
|
|
|
$id = str_replace('_', 'PT-', $randomUtils->generateID()); |
108
|
|
|
$expiresAt = time() + $this->proxyTicketExpireTime; |
109
|
|
|
|
110
|
|
|
return array_merge(['id' => $id, 'validBefore' => $expiresAt], $content); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $ticket |
116
|
|
|
* @return int|false |
117
|
|
|
*/ |
118
|
|
|
public function isSessionTicket(array $ticket) |
119
|
|
|
{ |
120
|
|
|
return preg_match('/^[a-zA-Z0-9]+$/D', $ticket['id']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $ticket |
126
|
|
|
* @return int|false |
127
|
|
|
*/ |
128
|
|
|
public function isServiceTicket(array $ticket) |
129
|
|
|
{ |
130
|
|
|
return preg_match('/^ST-?[a-zA-Z0-9]+$/D', $ticket['id']); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param array $ticket |
136
|
|
|
* @return int|false |
137
|
|
|
*/ |
138
|
|
|
public function isProxyGrantingTicket(array $ticket) |
139
|
|
|
{ |
140
|
|
|
return preg_match('/^PGT-?[a-zA-Z0-9]+$/D', $ticket['id']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $ticket |
146
|
|
|
* @return int|false |
147
|
|
|
*/ |
148
|
|
|
public function isProxyTicket(array $ticket) |
149
|
|
|
{ |
150
|
|
|
return preg_match('/^PT-?[a-zA-Z0-9]+$/D', $ticket['id']); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array $ticket |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function isExpired(array $ticket): bool |
159
|
|
|
{ |
160
|
|
|
return !array_key_exists('validBefore', $ticket) || $ticket['validBefore'] < time(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|