Skip to content

Commit b55171b

Browse files
Support multiple checksum algorithms (#147)
* Add custom hash algorithm * Adjust tests * Fix cs * Update src/BigBlueButton.php Co-authored-by: Felix Jacobi <felix@jacobi-hamburg.net> * Refactor to be compliant with upstream * Fix cs * Remove setHashingAlgorithm, add hashing algorithm as constructor arg. * Revert changes to construct param type hints * Fix cs * Fix test --------- Co-authored-by: Felix Jacobi <felix@jacobi-hamburg.net>
1 parent e231d4b commit b55171b

6 files changed

Lines changed: 79 additions & 11 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"ext-curl": "*",
6363
"ext-simplexml": "*",
6464
"ext-mbstring": "*",
65-
"ext-json": "*"
65+
"ext-json": "*",
66+
"marc-mabe/php-enum": "^4.7"
6667
},
6768
"suggest": {
6869
"psr/http-client-implementation": "To use the PsrHttpClientTransport.",

src/BigBlueButton.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace BigBlueButton;
2121

2222
use BigBlueButton\Core\ApiMethod;
23+
use BigBlueButton\Enum\HashingAlgorithm;
2324
use BigBlueButton\Exceptions\ConfigException;
2425
use BigBlueButton\Exceptions\NetworkException;
2526
use BigBlueButton\Exceptions\ParsingException;
@@ -80,6 +81,11 @@ class BigBlueButton
8081
*/
8182
protected $bbbServerBaseUrl;
8283

84+
/**
85+
* @var string
86+
*/
87+
protected $hashingAlgorithm;
88+
8389
/**
8490
* @var UrlBuilder
8591
*/
@@ -107,17 +113,19 @@ class BigBlueButton
107113
*
108114
* @throws ConfigException
109115
*/
110-
public function __construct(string $baseUrl = null, string $secret = null, TransportInterface $transport = null)
116+
public function __construct(string $baseUrl = null, string $secret = null, TransportInterface $transport = null, string $hashingAlgorithm = HashingAlgorithm::SHA_1)
111117
{
112118
// Keeping backward compatibility with older deployed versions
113119
$this->securitySecret = $secret ?: getenv('BBB_SECURITY_SALT') ?: getenv('BBB_SECRET');
114120
$this->bbbServerBaseUrl = $baseUrl ?: getenv('BBB_SERVER_BASE_URL');
115121

122+
$this->hashingAlgorithm = $hashingAlgorithm;
123+
116124
if (empty($this->bbbServerBaseUrl)) {
117125
throw new ConfigException('Base url required');
118126
}
119127

120-
$this->urlBuilder = new UrlBuilder($this->securitySecret, $this->bbbServerBaseUrl);
128+
$this->urlBuilder = new UrlBuilder($this->securitySecret, $this->bbbServerBaseUrl, $this->hashingAlgorithm);
121129
$this->transport = $transport ?? CurlTransport::createWithDefaultOptions();
122130
}
123131

src/Enum/HashingAlgorithm.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* BigBlueButton open source conferencing system - https://www.bigbluebutton.org/.
5+
*
6+
* Copyright (c) 2016-2023 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 <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace BigBlueButton\Enum;
22+
23+
use MabeEnum\Enum;
24+
25+
class HashingAlgorithm extends Enum
26+
{
27+
public const SHA_1 = 'sha1';
28+
public const SHA_256 = 'sha256';
29+
public const SHA_512 = 'sha512';
30+
public const SHA_384 = 'sha384';
31+
}

src/Util/UrlBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ final class UrlBuilder
3535
*/
3636
private $bbbServerBaseUrl;
3737

38-
public function __construct(string $secret, string $serverBaseUrl)
38+
/**
39+
* @var string
40+
*/
41+
private $hashingAlgorithm;
42+
43+
public function __construct(string $secret, string $serverBaseUrl, string $hashingAlgorithm)
3944
{
4045
$this->securitySalt = $secret;
4146
$this->bbbServerBaseUrl = $serverBaseUrl;
47+
$this->hashingAlgorithm = $hashingAlgorithm;
4248
}
4349

4450
/**
@@ -61,6 +67,6 @@ public function buildQs(string $method = '', string $params = ''): string
6167
$checksumParam = 'checksum=';
6268
}
6369

64-
return $params.$checksumParam.sha1($method.$params.$this->securitySalt);
70+
return $params.$checksumParam.hash($this->hashingAlgorithm, $method.$params.$this->securitySalt);
6571
}
6672
}

tests/unit/BigBlueButtonTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace BigBlueButton;
2121

2222
use BigBlueButton\Core\ApiMethod;
23+
use BigBlueButton\Enum\HashingAlgorithm;
2324
use BigBlueButton\Exceptions\ConfigException;
2425
use BigBlueButton\Exceptions\NetworkException;
2526
use BigBlueButton\Exceptions\ParsingException;
@@ -349,13 +350,23 @@ public function testUpdateRecordingsUrl()
349350

350351
public function testBuildUrl(): void
351352
{
352-
$bigBlueButton = new BigBlueButton('https://bbb.example/bigbluebutton/', 'S3cr3t');
353+
// Test with default hash algorithm (sha1)
354+
$bigBlueButton = new BigBlueButton('https://bbb.example/bigbluebutton/', 'S3cr3t', null, HashingAlgorithm::SHA_1);
353355

354356
$this->assertSame(
355357
'https://bbb.example/bigbluebutton/api/foo?foo=bar&baz=bazinga&checksum=694ad46bc5a79a572bab6c8b9a939527c39ac7f6',
356358
$bigBlueButton->buildUrl('foo', 'foo=bar&baz=bazinga'),
357359
'URL is not ok'
358360
);
361+
362+
// Test with different hash algorithm (sha256)
363+
$bigBlueButton = new BigBlueButton('https://bbb.example/bigbluebutton/', 'S3cr3t', null, HashingAlgorithm::SHA_256);
364+
365+
$this->assertSame(
366+
'https://bbb.example/bigbluebutton/api/foo?foo=bar&baz=bazinga&checksum=0ce0d779a8220be9824c7eab055b36b59ac504ba899a76d7c528b8473960025e',
367+
$bigBlueButton->buildUrl('foo', 'foo=bar&baz=bazinga'),
368+
'URL is not ok'
369+
);
359370
}
360371

361372
public function testGetInsertDocument(): void

tests/unit/Util/UrlBuilderTest.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,30 @@ final class UrlBuilderTest extends TestCase
3030
{
3131
public function testBuildUrl(): void
3232
{
33-
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/');
33+
// Test with sha1 hash algorithm
34+
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/', 'sha1');
3435

3536
// echo sha1('getMeetings' . 'foo=bar&baz=bazinga' . 'AFFE');
3637
$this->assertSame(
3738
'https://bbb.example/bigbluebutton/api/getMeetings?foo=bar&baz=bazinga&checksum=8c313ec566a91bb9a409b51a0f515f53216a43ae',
3839
$urlBuilder->buildUrl('getMeetings', 'foo=bar&baz=bazinga'),
3940
'signed URL is OK'
4041
);
42+
43+
// Test with sha256 hash algorithm
44+
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/', 'sha256');
45+
46+
// echo hash('sha256', 'getMeetings' . 'foo=bar&baz=bazinga' . 'AFFE');
47+
$this->assertSame(
48+
'https://bbb.example/bigbluebutton/api/getMeetings?foo=bar&baz=bazinga&checksum=e93a022a742425259bf3acec803ad8b4e428e7653b66bfecfa60d935a04bcc3b',
49+
$urlBuilder->buildUrl('getMeetings', 'foo=bar&baz=bazinga'),
50+
'signed URL is OK'
51+
);
4152
}
4253

4354
public function testBuildUrlWithEmptyParams(): void
4455
{
45-
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/');
56+
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/', 'sha1');
4657

4758
// echo sha1('getMeetings' . '' . 'AFFE');
4859
$this->assertSame(
@@ -54,7 +65,7 @@ public function testBuildUrlWithEmptyParams(): void
5465

5566
public function testBuildUrlWithoutAppend(): void
5667
{
57-
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/');
68+
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/', 'sha1');
5869

5970
$this->assertSame(
6071
'https://bbb.example/bigbluebutton/api/getMeetings',
@@ -65,7 +76,7 @@ public function testBuildUrlWithoutAppend(): void
6576

6677
public function testBuildQs(): void
6778
{
68-
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/');
79+
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/', 'sha1');
6980

7081
// echo sha1('getMeetings' . 'foo=bar&baz=bazinga' . 'AFFE');
7182
$this->assertSame(
@@ -77,7 +88,7 @@ public function testBuildQs(): void
7788

7889
public function testBuildQsWithEmptyParams(): void
7990
{
80-
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/');
91+
$urlBuilder = new UrlBuilder('AFFE', 'https://bbb.example/bigbluebutton/', 'sha1');
8192

8293
// echo sha1('getMeetings' . '' . 'AFFE');
8394
$this->assertSame(

0 commit comments

Comments
 (0)