Passed
Pull Request — master (#226)
by
unknown
01:54
created

UrlBuilder::initiateAlgorithmForHooks()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 4
rs 10
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
    /** @deprecated This property will disappear after a while */
52
    private string $hashAlgoForHooks;
53
54
    public function __construct(string $secret, string $serverBaseUrl, string $hashingAlgorithm)
55
    {
56
        $this->securitySalt     = $secret;
57
        $this->bbbServerBaseUrl = $serverBaseUrl;
58
        $this->hashingAlgorithm = $hashingAlgorithm;
59
60
        $this->initiateAlgorithmForHooks();
0 ignored issues
show
Deprecated Code introduced by
The function BigBlueButton\Util\UrlBu...iateAlgorithmForHooks() 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

60
        /** @scrutinizer ignore-deprecated */ $this->initiateAlgorithmForHooks();

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...
61
    }
62
63
    /**
64
     * Sets the hashing algorithm.
65
     */
66
    public function setHashingAlgorithm(string $hashingAlgorithm): void
67
    {
68
        $this->hashingAlgorithm = $hashingAlgorithm;
69
    }
70
71
    public function getHashingAlgorithm(): string
72
    {
73
        return $this->hashingAlgorithm;
74
    }
75
76
    /**
77
     * Builds an API method URL that includes the url + params + its generated checksum.
78
     */
79
    public function buildUrl(string $method = '', string $params = '', bool $append = true): string
80
    {
81
        return $this->bbbServerBaseUrl . 'api/' . $method . ($append ? '?' . $this->buildQs($method, $params) : '');
82
    }
83
84
    /**
85
     * Builds a query string for an API method URL that includes the params + its generated checksum.
86
     */
87
    public function buildQs(string $method = '', string $params = ''): string
88
    {
89
        return $params . '&checksum=' . hash($this->hashingAlgorithm, $method . $params . $this->securitySalt);
90
    }
91
92
    // URL-Generators
93
    public function getCreateMeetingUrl(CreateMeetingParameters $createMeetingParams): string
94
    {
95
        return $this->buildUrl(ApiMethod::CREATE, $createMeetingParams->getHTTPQuery());
96
    }
97
98
    public function getJoinMeetingURL(JoinMeetingParameters $joinMeetingParams): string
99
    {
100
        return $this->buildUrl(ApiMethod::JOIN, $joinMeetingParams->getHTTPQuery());
101
    }
102
103
    public function getEndMeetingURL(EndMeetingParameters $endParams): string
104
    {
105
        return $this->buildUrl(ApiMethod::END, $endParams->getHTTPQuery());
106
    }
107
108
    public function getInsertDocumentUrl(InsertDocumentParameters $insertDocumentParameters): string
109
    {
110
        return $this->buildUrl(ApiMethod::INSERT_DOCUMENT, $insertDocumentParameters->getHTTPQuery());
111
    }
112
113
    public function getIsMeetingRunningUrl(IsMeetingRunningParameters $meetingParams): string
114
    {
115
        return $this->buildUrl(ApiMethod::IS_MEETING_RUNNING, $meetingParams->getHTTPQuery());
116
    }
117
118
    public function getMeetingsUrl(): string
119
    {
120
        return $this->buildUrl(ApiMethod::GET_MEETINGS);
121
    }
122
123
    public function getMeetingInfoUrl(GetMeetingInfoParameters $meetingParams): string
124
    {
125
        return $this->buildUrl(ApiMethod::GET_MEETING_INFO, $meetingParams->getHTTPQuery());
126
    }
127
128
    public function getRecordingsUrl(GetRecordingsParameters $recordingsParams): string
129
    {
130
        return $this->buildUrl(ApiMethod::GET_RECORDINGS, $recordingsParams->getHTTPQuery());
131
    }
132
133
    public function getPublishRecordingsUrl(PublishRecordingsParameters $recordingParams): string
134
    {
135
        return $this->buildUrl(ApiMethod::PUBLISH_RECORDINGS, $recordingParams->getHTTPQuery());
136
    }
137
138
    public function getDeleteRecordingsUrl(DeleteRecordingsParameters $recordingParams): string
139
    {
140
        return $this->buildUrl(ApiMethod::DELETE_RECORDINGS, $recordingParams->getHTTPQuery());
141
    }
142
143
    public function getUpdateRecordingsUrl(UpdateRecordingsParameters $recordingParams): string
144
    {
145
        return $this->buildUrl(ApiMethod::UPDATE_RECORDINGS, $recordingParams->getHTTPQuery());
146
    }
147
148
    public function getRecordingTextTracksUrl(GetRecordingTextTracksParameters $getRecordingTextTracksParameters): string
149
    {
150
        return $this->buildUrl(ApiMethod::GET_RECORDING_TEXT_TRACKS, $getRecordingTextTracksParameters->getHTTPQuery());
151
    }
152
153
    public function getPutRecordingTextTrackUrl(PutRecordingTextTrackParameters $putRecordingTextTrackParams): string
154
    {
155
        return $this->buildUrl(ApiMethod::PUT_RECORDING_TEXT_TRACK, $putRecordingTextTrackParams->getHTTPQuery());
156
    }
157
158
    /**
159
     * BBB-Server < 3.0 can only use SHA1 in the handling with hooks.
160
     * Please configure the HASH_ALGO_FOR_HOOKS environment variable in case SHA1 shall not be used.
161
     *
162
     * @see https://github.com/bigbluebutton/bbb-webhooks/issues/30
163
     */
164
    public function getHooksCreateUrl(HooksCreateParameters $hookCreateParams): string
165
    {
166
        // change hashing algorithm for hooks
167
        $this->setHashingAlgorithm($this->hashAlgoForHooks);
0 ignored issues
show
Deprecated Code introduced by
The property BigBlueButton\Util\UrlBuilder::$hashAlgoForHooks has been deprecated: This property will disappear after a while ( Ignorable by Annotation )

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

167
        $this->setHashingAlgorithm(/** @scrutinizer ignore-deprecated */ $this->hashAlgoForHooks);

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

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

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

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

187
        $this->setHashingAlgorithm(/** @scrutinizer ignore-deprecated */ $this->hashAlgoForHooks);

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

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

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

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

207
        $this->setHashingAlgorithm(/** @scrutinizer ignore-deprecated */ $this->hashAlgoForHooks);

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

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

Loading history...
208
209
        // build URL
210
        $url = $this->buildUrl(ApiMethod::HOOKS_DESTROY, $hooksDestroyParams->getHTTPQuery());
211
212
        // reset to 'normal' hashing algorithm
213
        $this->setHashingAlgorithm($this->getHashingAlgorithm());
214
215
        return $url;
216
    }
217
218
    /**
219
     *  Defines the algorithm to be used for hooks.
220
     *
221
     *  Background: BBB-Server below 3.0 are using SHA1-algorithm for hooks. The current planning for
222
     *              BBB-Server 3.0 (and on) is to align the hashing algorithm  for hooks with the rest
223
     *              of the system. Having this in mind the two situations need to be covered:
224
     *                 - BBB-Server <  3.0 ==> SHA1 is default for hooks (even rest is using other algorithm)
225
     *                 - BBB-Server >= 3.0 ==> same algorithm everywhere (according to planning).
226
     *
227
     *  This function will evolve in phases:
228
     *   - Phase 1: SHA1 as default                 (or superseded by environment-variable HASH_ALGO_FOR_HOOKS).
229
     *   - 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).
230
     *   - Phase 3: removal of this function, the class-property '$hashAlgoForHooks' and the use of env-variable HASH_ALGO_FOR_HOOKS.
231
     *
232
     * @deprecated This function will evolve in phases and will later disappear
233
     */
234
    private function initiateAlgorithmForHooks(): void
235
    {
236
        // in case this env-variable is not set, SHA1 shall be used as default (phase 1)
237
        $this->hashAlgoForHooks = getenv('HASH_ALGO_FOR_HOOKS') ?: HashingAlgorithm::SHA_1;
0 ignored issues
show
Deprecated Code introduced by
The property BigBlueButton\Util\UrlBuilder::$hashAlgoForHooks has been deprecated: This property will disappear after a while ( Ignorable by Annotation )

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

237
        /** @scrutinizer ignore-deprecated */ $this->hashAlgoForHooks = getenv('HASH_ALGO_FOR_HOOKS') ?: HashingAlgorithm::SHA_1;

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

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

Loading history...
238
239
        /* ---------------------------------- phase 2 ----------------------------------
240
         * // in case this env-variable is not set, the 'normal' algorithm shall be used as default (phase 2)
241
         * $this->hashAlgoForHooks = getenv('HASH_ALGO_FOR_HOOKS') ?: $this->getHashingAlgorithm();
242
         *
243
         * if (getenv('HASH_ALGO_FOR_HOOKS')) {
244
         *   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);
245
         * }
246
         * ---------------------------------- phase 2 ---------------------------------- */
247
    }
248
}
249