Debug::decode()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 1
dl 0
loc 21
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\saml2debug\Controller;
6
7
use SimpleSAML\Configuration;
8
use SimpleSAML\Error;
9
use SimpleSAML\Session;
10
use SimpleSAML\XHTML\Template;
11
use Symfony\Component\HttpFoundation\Request;
12
13
/**
14
 * Controller class for the saml2debug module.
15
 *
16
 * This class serves the different views available in the module.
17
 *
18
 * @package simplesamlphp/simplesamlphp-module-saml2debug
19
 */
20
class Debug
21
{
22
    /** @var \SimpleSAML\Configuration */
23
    protected Configuration $config;
24
25
    /** @var \SimpleSAML\Session */
26
    protected Session $session;
27
28
29
    /**
30
     * Controller constructor.
31
     *
32
     * It initializes the global configuration and session for the controllers implemented here.
33
     *
34
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
35
     * @param \SimpleSAML\Session $session The session to use by the controllers.
36
     *
37
     * @throws \Exception
38
     */
39
    public function __construct(
40
        Configuration $config,
41
        Session $session,
42
    ) {
43
        $this->config = $config;
44
        $this->session = $session;
45
    }
46
47
48
    /**
49
     * Show SAML2 debugging info.
50
     *
51
     * @param \Symfony\Component\HttpFoundation\Request $request
52
     * @return \SimpleSAML\XHTML\Template
53
     * @throws \Exception
54
     */
55
    public function decode(Request $request): Template
56
    {
57
        $decoded = '';
58
        $encoded = '';
59
60
        if ($request->request->has('encoded')) {
61
            if (!$request->request->has('binding')) {
62
                throw new Error\BadRequest('Missing binding');
63
            }
64
65
            $decoded = $this->parseEncodedMessage(
66
                $request->request->get('encoded'),
67
                $request->request->get('binding'),
68
            );
69
        }
70
71
        $t = new Template($this->config, 'saml2debug:decode.twig');
72
        $t->data['encoded'] = $encoded;
73
        $t->data['decoded'] = $decoded;
74
75
        return $t;
76
    }
77
78
79
    /**
80
     * Show SAML2 debugging info.
81
     *
82
     * @param \Symfony\Component\HttpFoundation\Request $request
83
     * @return \SimpleSAML\XHTML\Template
84
     * @throws \Exception
85
     */
86
    public function encode(Request $request): Template
87
    {
88
        $decoded = '';
89
        $encoded = 'fZJNT%2BMwEIbvSPwHy%2Fd8tMvHympSdUGISuwS0cCBm%2BtMUwfbk%2FU4zfLvSVMq2Euv45n3fd7xzOb%2FrGE78KTR' .
90
                   'ZXwSp5yBU1hpV2f8ubyLfvJ5fn42I2lNKxZd2Lon%2BNsBBTZMOhLjQ8Y77wRK0iSctEAiKLFa%2FH4Q0zgVrceACg1ny9' .
91
                   'uMy7rCdaM2%2Bs0BWrtppK2UAdeoVjW2ruq1bevGImcvR6zpHmtJ1MHSUZAuDKU0vY7Si2h6VU5%2BiMuJuLx65az4dPql' .
92
                   '3SHBKaz1oYnEfVkWUfG4KkeBna7A%2Fxm6M14j1gZihZazBRH4MODcoKPOgl%2BB32kFz08PGd%2BG0JJIkr7v46%2BhRC' .
93
                   'aEpod17DCRivYZCkmkd4N28B3wfNyrGKP5bws9DS6PKDz%2FMpsl36Tyz%2F%2Fax1jeFmi0emcLY7C%2F8SDD0Z7dobcy' .
94
                   'nHbbV3QVbcZW0TlqQemNhoqzJD%2B4%2Fn8Yw7l8AA%3D%3D';
95
96
        if ($request->request->has('decoded')) {
97
            $encoded = $this->parseDecodedMessage($request->request->get('decoded'));
98
        }
99
100
        $t = new Template($this->config, 'saml2debug:encode.twig');
101
        $t->data['encoded'] = $encoded;
102
        $t->data['decoded'] = $decoded;
103
104
        return $t;
105
    }
106
107
108
    /**
109
     * @param string $raw
110
     * @return string
111
     */
112
    private function getValue(string $raw): string
113
    {
114
        $val = $raw;
115
116
        $url = parse_url($raw, PHP_URL_QUERY);
117
        if (!empty($url)) {
118
            $val = $url;
119
        }
120
121
        $arr = [];
122
        parse_str($val, $arr);
123
124
        if (array_key_exists('SAMLResponse', $arr)) {
125
            return $arr['SAMLResponse'];
126
        }
127
        if (array_key_exists('SAMLRequest', $arr)) {
128
            return $arr['SAMLRequest'];
129
        }
130
        if (array_key_exists('LogoutRequest', $arr)) {
131
            return $arr['LogoutRequest'];
132
        }
133
        if (array_key_exists('LogoutResponse', $arr)) {
134
            return $arr['LogoutResponse'];
135
        }
136
137
        return rawurldecode(stripslashes($val));
138
    }
139
140
141
    /**
142
     * @param string $raw
143
     * @return string
144
     */
145
    private function parseDecodedMessage(string $raw): string
146
    {
147
        $message = $this->getValue($raw);
148
149
        $base64decoded = base64_decode($message);
150
        $gzinflated = gzinflate($base64decoded);
151
        if ($gzinflated !== false) {
152
            $base64decoded = $gzinflated;
153
        }
154
155
        return $base64decoded;
156
    }
157
158
159
    /**
160
     * @param string $message
161
     * @param string $binding
162
     * @return string
163
     */
164
    private function parseEncodedMessage(string $message, string $binding): string
165
    {
166
        if ($binding === 'redirect') {
167
            return urlencode(base64_encode(gzdeflate(stripslashes($message))));
168
        } else {
169
            return base64_encode(stripslashes($message));
170
        }
171
    }
172
}
173