Passed
Pull Request — master (#226)
by
unknown
02:25
created

UrlBuilder::getHashingAlgorithmForHooks()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * BigBlueButton open source conferencing system - https://www.bigbluebutton.org/.
5
 *
6
 * Copyright (c) 2016-2024 BigBlueButton Inc. and by respective authors (see below).
7
 *
8
 * This program is free software; you can redistribute it and/or modify it under the
9
 * terms of the GNU Lesser General Public License as published by the Free Software
10
 * Foundation; either version 3.0 of the License, or (at your option) any later
11
 * version.
12
 *
13
 * BigBlueButton is distributed in the hope that it will be useful, but WITHOUT ANY
14
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License along
18
 * with BigBlueButton; if not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace BigBlueButton\Util;
22
23
use BigBlueButton\Core\ApiMethod;
24
use BigBlueButton\Enum\HashingAlgorithm;
25
use BigBlueButton\Parameters\CreateMeetingParameters;
26
use BigBlueButton\Parameters\DeleteRecordingsParameters;
27
use BigBlueButton\Parameters\EndMeetingParameters;
28
use BigBlueButton\Parameters\GetMeetingInfoParameters;
29
use BigBlueButton\Parameters\GetRecordingsParameters;
30
use BigBlueButton\Parameters\GetRecordingTextTracksParameters;
31
use BigBlueButton\Parameters\HooksCreateParameters;
32
use BigBlueButton\Parameters\HooksDestroyParameters;
33
use BigBlueButton\Parameters\InsertDocumentParameters;
34
use BigBlueButton\Parameters\IsMeetingRunningParameters;
35
use BigBlueButton\Parameters\JoinMeetingParameters;
36
use BigBlueButton\Parameters\PublishRecordingsParameters;
37
use BigBlueButton\Parameters\PutRecordingTextTrackParameters;
38
use BigBlueButton\Parameters\UpdateRecordingsParameters;
39
40
/**
41
 * Class UrlBuilder.
42
 */
43
class UrlBuilder
44
{
45
    protected string $hashingAlgorithm;
46
47
    private string $securitySalt;
48
49
    private string $bbbServerBaseUrl;
50
51
    public function __construct(string $secret, string $serverBaseUrl, string $hashingAlgorithm)
52
    {
53
        $this->securitySalt     = $secret;
54
        $this->bbbServerBaseUrl = $serverBaseUrl;
55
        $this->hashingAlgorithm = $hashingAlgorithm;
56
    }
57
58
    /**
59
     * Sets the hashing algorithm.
60
     */
61
    public function setHashingAlgorithm(string $hashingAlgorithm): void
62
    {
63
        $this->hashingAlgorithm = $hashingAlgorithm;
64
    }
65
66
    public function getHashingAlgorithm(): string
67
    {
68
        return $this->hashingAlgorithm;
69
    }
70
71
    /**
72
     * Builds an API method URL that includes the url + params + its generated checksum.
73
     */
74
    public function buildUrl(string $method = '', string $params = '', bool $append = true): string
75
    {
76
        return $this->bbbServerBaseUrl . 'api/' . $method . ($append ? '?' . $this->buildQs($method, $params) : '');
77
    }
78
79
    /**
80
     * Builds a query string for an API method URL that includes the params + its generated checksum.
81
     */
82
    public function buildQs(string $method = '', string $params = ''): string
83
    {
84
        return $params . '&checksum=' . hash($this->hashingAlgorithm, $method . $params . $this->securitySalt);
85
    }
86
87
    // URL-Generators
88
    public function getCreateMeetingUrl(CreateMeetingParameters $createMeetingParams): string
89
    {
90
        return $this->buildUrl(ApiMethod::CREATE, $createMeetingParams->getHTTPQuery());
91
    }
92
93
    public function getJoinMeetingURL(JoinMeetingParameters $joinMeetingParams): string
94
    {
95
        return $this->buildUrl(ApiMethod::JOIN, $joinMeetingParams->getHTTPQuery());
96
    }
97
98
    public function getEndMeetingURL(EndMeetingParameters $endParams): string
99
    {
100
        return $this->buildUrl(ApiMethod::END, $endParams->getHTTPQuery());
101
    }
102
103
    public function getInsertDocumentUrl(InsertDocumentParameters $insertDocumentParameters): string
104
    {
105
        return $this->buildUrl(ApiMethod::INSERT_DOCUMENT, $insertDocumentParameters->getHTTPQuery());
106
    }
107
108
    public function getIsMeetingRunningUrl(IsMeetingRunningParameters $meetingParams): string
109
    {
110
        return $this->buildUrl(ApiMethod::IS_MEETING_RUNNING, $meetingParams->getHTTPQuery());
111
    }
112
113
    public function getMeetingsUrl(): string
114
    {
115
        return $this->buildUrl(ApiMethod::GET_MEETINGS);
116
    }
117
118
    public function getMeetingInfoUrl(GetMeetingInfoParameters $meetingParams): string
119
    {
120
        return $this->buildUrl(ApiMethod::GET_MEETING_INFO, $meetingParams->getHTTPQuery());
121
    }
122
123
    public function getRecordingsUrl(GetRecordingsParameters $recordingsParams): string
124
    {
125
        return $this->buildUrl(ApiMethod::GET_RECORDINGS, $recordingsParams->getHTTPQuery());
126
    }
127
128
    public function getPublishRecordingsUrl(PublishRecordingsParameters $recordingParams): string
129
    {
130
        return $this->buildUrl(ApiMethod::PUBLISH_RECORDINGS, $recordingParams->getHTTPQuery());
131
    }
132
133
    public function getDeleteRecordingsUrl(DeleteRecordingsParameters $recordingParams): string
134
    {
135
        return $this->buildUrl(ApiMethod::DELETE_RECORDINGS, $recordingParams->getHTTPQuery());
136
    }
137
138
    public function getUpdateRecordingsUrl(UpdateRecordingsParameters $recordingParams): string
139
    {
140
        return $this->buildUrl(ApiMethod::UPDATE_RECORDINGS, $recordingParams->getHTTPQuery());
141
    }
142
143
    public function getRecordingTextTracksUrl(GetRecordingTextTracksParameters $getRecordingTextTracksParameters): string
144
    {
145
        return $this->buildUrl(ApiMethod::GET_RECORDING_TEXT_TRACKS, $getRecordingTextTracksParameters->getHTTPQuery());
146
    }
147
148
    public function getPutRecordingTextTrackUrl(PutRecordingTextTrackParameters $putRecordingTextTrackParams): string
149
    {
150
        return $this->buildUrl(ApiMethod::PUT_RECORDING_TEXT_TRACK, $putRecordingTextTrackParams->getHTTPQuery());
151
    }
152
153
    /**
154
     * BBB-Server < 3.0 can only use SHA1 in the handling with hooks.
155
     * Please configure the HASH_ALGO_FOR_HOOKS environment variable in case SHA1 shall not be used.
156
     *
157
     * @see https://github.com/bigbluebutton/bbb-webhooks/issues/30
158
     */
159
    public function getHooksCreateUrl(HooksCreateParameters $hookCreateParams): string
160
    {
161
        // store current hashing algorithm
162
        $hashingAlgorithm = $this->getHashingAlgorithm();
163
164
        // change hashing algorithm for hooks
165
        $this->setHashingAlgorithm($this->getHashingAlgorithmForHooks());
0 ignored issues
show
Deprecated Code introduced by
The function BigBlueButton\Util\UrlBu...hingAlgorithmForHooks() has been deprecated: This function will evolve in phases and will later disappear ( Ignorable by Annotation )

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

165
        $this->setHashingAlgorithm(/** @scrutinizer ignore-deprecated */ $this->getHashingAlgorithmForHooks());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
166
167
        // build URL
168
        $url = $this->buildUrl(ApiMethod::HOOKS_CREATE, $hookCreateParams->getHTTPQuery());
169
170
        // reset to 'normal' hashing algorithm
171
        $this->setHashingAlgorithm($hashingAlgorithm);
172
173
        return $url;
174
    }
175
176
    /**
177
     * BBB-Server < 3.0 can only use SHA1 in the handling with hooks.
178
     * Please configure the HASH_ALGO_FOR_HOOKS environment variable in case SHA1 shall not be used.
179
     *
180
     * @see https://github.com/bigbluebutton/bbb-webhooks/issues/30
181
     */
182
    public function getHooksListUrl(): string
183
    {
184
        // store current hashing algorithm
185
        $hashingAlgorithm = $this->getHashingAlgorithm();
186
187
        // change hashing algorithm for hooks
188
        $this->setHashingAlgorithm($this->getHashingAlgorithmForHooks());
0 ignored issues
show
Deprecated Code introduced by
The function BigBlueButton\Util\UrlBu...hingAlgorithmForHooks() has been deprecated: This function will evolve in phases and will later disappear ( Ignorable by Annotation )

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

188
        $this->setHashingAlgorithm(/** @scrutinizer ignore-deprecated */ $this->getHashingAlgorithmForHooks());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
189
190
        // build URL
191
        $url = $this->buildUrl(ApiMethod::HOOKS_LIST);
192
193
        // reset to 'normal' hashing algorithm
194
        $this->setHashingAlgorithm($hashingAlgorithm);
195
196
        return $url;
197
    }
198
199
    /**
200
     * BBB-Server < 3.0 can only use SHA1 in the handling with hooks.
201
     * Please configure the HASH_ALGO_FOR_HOOKS environment variable in case SHA1 shall not be used.
202
     *
203
     * @see https://github.com/bigbluebutton/bbb-webhooks/issues/30
204
     */
205
    public function getHooksDestroyUrl(HooksDestroyParameters $hooksDestroyParams): string
206
    {
207
        // store current hashing algorithm
208
        $hashingAlgorithm = $this->getHashingAlgorithm();
209
210
        // change hashing algorithm for hooks
211
        $this->setHashingAlgorithm($this->getHashingAlgorithmForHooks());
0 ignored issues
show
Deprecated Code introduced by
The function BigBlueButton\Util\UrlBu...hingAlgorithmForHooks() has been deprecated: This function will evolve in phases and will later disappear ( Ignorable by Annotation )

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

211
        $this->setHashingAlgorithm(/** @scrutinizer ignore-deprecated */ $this->getHashingAlgorithmForHooks());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
212
213
        // build URL
214
        $url = $this->buildUrl(ApiMethod::HOOKS_DESTROY, $hooksDestroyParams->getHTTPQuery());
215
216
        // reset to 'normal' hashing algorithm
217
        $this->setHashingAlgorithm($hashingAlgorithm);
218
219
        return $url;
220
    }
221
222
    /**
223
     *  This function defines the algorithm to be used for hooks.
224
     *
225
     *  This function will evolve in phases:
226
     *    - Phase 1: SHA1 as default                 (or superseded by environment-variable HASH_ALGO_FOR_HOOKS).
227
     *    - Phase 2: same algo everywhere as default (or superseded by environment-variable HASH_ALGO_FOR_HOOKS and which will trigger in this case a deprecation-warning).
228
     *    - Phase 3: removal of this function, adaptation of the other hook-functions in this class and remove the use of env-variable HASH_ALGO_FOR_HOOKS.
229
     *
230
     *  Background:
231
     *  BB-Server below 3.0 are using SHA1-algorithm for hooks only, but allow higher algorithms for
232
     *  other APIs. This is creating issues since the algorithm of choice is used in the urlBuilder-class
233
     *  for the hashing of the checksum. This is resulting in denied requests for hooks if the algorithm
234
     *  of choice is not SHA1.
235
     *  The current planning for BBB-Server 3.0 (and on) is to align the hashing algorithm for hooks with
236
     *  the rest of the system. Having this in mind two situations need to be covered:
237
     *    - BBB-Server <  3.0 ==> SHA1 is default for hooks (even rest is using other algorithm)
238
     *    - BBB-Server >= 3.0 ==> same algorithm everywhere (according to planning).
239
     *
240
     * @deprecated This function will evolve in phases and will later disappear
241
     */
242
    private function getHashingAlgorithmForHooks(): string
243
    {
244
        // ---------------------------------- phase 1 ----------------------------------
245
        // in case this env-variable is not set, SHA1 shall be used as default (phase 1)
246
        return getenv('HASH_ALGO_FOR_HOOKS') ?: HashingAlgorithm::SHA_1;
247
        // ---------------------------------- phase 1 ----------------------------------
248
249
        /* ---------------------------------- phase 2 ----------------------------------
250
         * if (getenv('HASH_ALGO_FOR_HOOKS')) {
251
         *   trigger_error('The environment variable HASH_ALGO_FOR_HOOKS will be removed soon. This will require you to run a BBB-Server 3.0 or higher!', E_USER_DEPRECATED);
252
         * }
253
         *
254
         * // in case this env-variable is not set, the 'normal' algorithm shall be used as default (phase 2)
255
         * return getenv('HASH_ALGO_FOR_HOOKS') ?: $this->getHashingAlgorithm();
256
         *
257
         * ---------------------------------- phase 2 ---------------------------------- */
258
    }
259
}
260