Passed
Push — master ( f2daf3...fc29ab )
by Tim
03:13
created

TicketFactory::createServiceTicket()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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
namespace SimpleSAML\Module\casserver\Cas\Ticket;
25
26
class TicketFactory
27
{
28
    private $serviceTicketExpireTime;
29
    private $proxyGrantingTicketExpireTime;
30
    private $proxyTicketExpireTime;
31
32
    public function __construct(\SimpleSAML\Configuration $config)
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
    {
34
        $this->serviceTicketExpireTime = $config->getValue('service_ticket_expire_time', 5);
35
        $this->proxyGrantingTicketExpireTime = $config->getValue('proxy_granting_ticket_expire_time', 3600);
36
        $this->proxyTicketExpireTime = $config->getValue('proxy_ticket_expire_time', 5);
37
    }
38
39
    /**
40
     * @param $sessionId string
41
     * @param $expiresAt int
42
     * @return array
43
     */
44
    public function createSessionTicket($sessionId, $expiresAt)
45
    {
46
        return [
47
            'id' => $sessionId,
48
            'validBefore' => $expiresAt,
49
            'renewId' => SimpleSAML\Utils\Random::generateID()
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\casser...SimpleSAML\Utils\Random was not found. Did you mean SimpleSAML\Utils\Random? If so, make sure to prefix the type with \.
Loading history...
50
        ];
51
    }
52
53
    /**
54
     * @param $content array
55
     * @return array
56
     */
57
    public function createServiceTicket(array $content)
58
    {
59
        $id = str_replace('_', 'ST-', SimpleSAML\Utils\Random::generateID());
60
        $expiresAt = time() + $this->serviceTicketExpireTime;
61
62
        return array_merge(['id' => $id, 'validBefore' => $expiresAt], $content);
63
    }
64
65
    /**
66
     * @param $content array
67
     * @return array
68
     */
69
    public function createProxyGrantingTicket(array $content)
70
    {
71
        $id = str_replace('_', 'PGT-', SimpleSAML\Utils\Random::generateID());
72
        $iou = str_replace('_', 'PGTIOU-', SimpleSAML\Utils\Random::generateID());
73
74
        $expireAt = time() + $this->proxyGrantingTicketExpireTime;
75
76
        return array_merge(['id' => $id, 'iou' => $iou, 'validBefore' => $expireAt], $content);
77
    }
78
79
    /**
80
     * @param $content array
81
     * @return array
82
     */
83
    public function createProxyTicket(array $content)
84
    {
85
        $id = str_replace('_', 'PT-', SimpleSAML\Utils\Random::generateID());
86
        $expiresAt = time() + $this->proxyTicketExpireTime;
87
88
        return array_merge(['id' => $id, 'validBefore' => $expiresAt], $content);
89
    }
90
91
    /**
92
     * @param $ticket array
93
     * @return int|false
94
     */
95
    public function isSessionTicket(array $ticket)
96
    {
97
        return preg_match('/^[a-zA-Z0-9]+$/D', $ticket['id']);
98
    }
99
100
    /**
101
     * @param $ticket array
102
     * @return int|false
103
     */
104
    public function isServiceTicket(array $ticket)
105
    {
106
        return preg_match('/^ST-?[a-zA-Z0-9]+$/D', $ticket['id']);
107
    }
108
109
    /**
110
     * @param $ticket array
111
     * @return int|false
112
     */
113
    public function isProxyGrantingTicket(array $ticket)
114
    {
115
        return preg_match('/^PGT-?[a-zA-Z0-9]+$/D', $ticket['id']);
116
    }
117
118
    /**
119
     * @param $ticket array
120
     * @return int|false
121
     */
122
    public function isProxyTicket(array $ticket)
123
    {
124
        return preg_match('/^PT-?[a-zA-Z0-9]+$/D', $ticket['id']);
125
    }
126
127
    /**
128
     * @param $ticket array
129
     * @return bool
130
     */
131
    public function isExpired(array $ticket)
132
    {
133
        return !array_key_exists('validBefore', $ticket) || $ticket['validBefore'] < time();
134
    }
135
}
136