Passed
Push — master ( 4484cd...f53a20 )
by Tim
02:08
created

Debug::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
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\Module;
10
use SimpleSAML\Session;
11
use SimpleSAML\XHTML\Template;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
/**
16
 * Controller class for the saml2debug module.
17
 *
18
 * This class serves the different views available in the module.
19
 *
20
 * @package simplesamlphp/simplesamlphp-module-saml2debug
21
 */
22
class Debug
23
{
24
    /** @var \SimpleSAML\Configuration */
25
    protected Configuration $config;
26
27
    /** @var \SimpleSAML\Session */
28
    protected Session $session;
29
30
31
    /**
32
     * Controller constructor.
33
     *
34
     * It initializes the global configuration and session for the controllers implemented here.
35
     *
36
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
37
     * @param \SimpleSAML\Session $session The session to use by the controllers.
38
     *
39
     * @throws \Exception
40
     */
41
    public function __construct(
42
        Configuration $config,
43
        Session $session
44
    ) {
45
        $this->config = $config;
46
        $this->session = $session;
47
    }
48
49
50
    /**
51
     * Show SAML2 debugging info.
52
     *
53
     * @param \Symfony\Component\HttpFoundation\Request $request
54
     * @return \SimpleSAML\XHTML\Template
55
     * @throws \Exception
56
     */
57
    public function decode(Request $request): Template
58
    {
59
        $decoded = '';
60
        $encoded = '';
61
62
        if ($request->query->has('encoded')) {
63
            if (!$request->query->has('binding')) {
64
                throw new Error\BadRequest('Missing binding');
65
            }
66
            $decoded = $this->parseEncodedMessage($request->query->get('encoded'), $request->query->get('binding'));
67
        }
68
69
        $t = new Template($this->config, 'saml2debug:decode.twig');
70
        $t->data['encoded'] = $encoded;
71
        $t->data['decoded'] = $decoded;
72
73
        return $t;
74
    }
75
76
77
    /**
78
     * Show SAML2 debugging info.
79
     *
80
     * @param \Symfony\Component\HttpFoundation\Request $request
81
     * @return \SimpleSAML\XHTML\Template
82
     * @throws \Exception
83
     */
84
    public function encode(Request $request): Template
85
    {
86
        $decoded = '';
87
        $encoded = 'fZJNT%2BMwEIbvSPwHy%2Fd8tMvHympSdUGISuwS0cCBm%2BtMUwfbk%2FU4zfLvSVMq2Euv45n3fd7xzOb%2FrGE78KTRZXwSp5yBU1hp' .
88
                   'V2f8ubyLfvJ5fn42I2lNKxZd2Lon%2BNsBBTZMOhLjQ8Y77wRK0iSctEAiKLFa%2FH4Q0zgVrceACg1ny9uMy7rCdaM2%2Bs0BWrtppK2U' .
89
                   'AdeoVjW2ruq1bevGImcvR6zpHmtJ1MHSUZAuDKU0vY7Si2h6VU5%2BiMuJuLx65az4dPql3SHBKaz1oYnEfVkWUfG4KkeBna7A%2Fxm6M1' .
90
                   '4j1gZihZazBRH4MODcoKPOgl%2BB32kFz08PGd%2BG0JJIkr7v46%2BhRCaEpod17DCRivYZCkmkd4N28B3wfNyrGKP5bws9DS6PKDz%2F' .
91
                   'Mpsl36Tyz%2F%2Fax1jeFmi0emcLY7C%2F8SDD0Z7dobcynHbbV3QVbcZW0TlqQemNhoqzJD%2B4%2Fn8Yw7l8AA%3D%3D';
92
93
        if ($request->query->has('decoded')) {
94
            $encoded = $this->parseDecodeMessage($request->query->get('decoded'));
0 ignored issues
show
Bug introduced by
The method parseDecodeMessage() does not exist on SimpleSAML\Module\saml2debug\Controller\Debug. Did you maybe mean parseDecodedMessage()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
            /** @scrutinizer ignore-call */ 
95
            $encoded = $this->parseDecodeMessage($request->query->get('decoded'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
95
        }
96
97
        $t = new Template($this->config, 'saml2debug:encode.twig');
98
        $t->data['encoded'] = $encoded;
99
        $t->data['decoded'] = $decoded;
100
101
        return $t;
102
    }
103
104
105
    /**
106
     * @param string $raw
107
     * @return string
108
     */
109
    private function getValue(string $raw): string
110
    {
111
        $val = $raw;
112
113
        $url = parse_url($raw, PHP_URL_QUERY);
114
        if (!empty($url)) {
115
            $val = $url;
116
        }
117
118
        $arr = [];
119
        parse_str($val, $arr);
120
121
        if (array_key_exists('SAMLResponse', $arr)) {
122
            return $arr['SAMLResponse'];
123
        }
124
        if (array_key_exists('SAMLRequest', $arr)) {
125
            return $arr['SAMLRequest'];
126
        }
127
        if (array_key_exists('LogoutRequest', $arr)) {
128
            return $arr['LogoutRequest'];
129
        }
130
        if (array_key_exists('LogoutResponse', $arr)) {
131
            return $arr['LogoutResponse'];
132
        }
133
134
        return rawurldecode(stripslashes($val));
135
    }
136
137
138
    /**
139
     * @param string $raw
140
     * @return string
141
     */
142
    private function parseDecodedMessage(string $raw): string
0 ignored issues
show
Unused Code introduced by
The method parseDecodedMessage() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
143
    {
144
        $message = $this->getValue($raw);
145
146
        $base64decoded = base64_decode($message);
147
        $gzinflated = gzinflate($base64decoded);
148
        if ($gzinflated !== false) {
149
            $base64decoded = $gzinflated;
150
        }
151
        $decoded = htmlspecialchars($base64decoded);
152
        return $decoded;
153
    }
154
155
156
    /**
157
     * @param string $message
158
     * @param string $binding
159
     * @return string
160
     */
161
    private function parseEncodedMessage(string $message, string $binding): string
162
    {
163
        if ($binding === 'redirect') {
164
            return urlencode(base64_encode(gzdeflate(stripslashes($message))));
165
        } else {
166
            return base64_encode(stripslashes($message));
167
        }
168
    }
169
}
170