Skip to content

Commit

Permalink
Fix version comparison (#723)
Browse files Browse the repository at this point in the history
* Fix version comparison for stable27

Signed-off-by: nabim777 <nabinalemagar019@gmail.com>

* mini patch updated

Signed-off-by: nabim777 <nabinalemagar019@gmail.com>

---------

Signed-off-by: nabim777 <nabinalemagar019@gmail.com>
  • Loading branch information
nabim777 committed Oct 31, 2024
1 parent f78780a commit 2a96d49
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/Service/OauthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public function __construct(ClientMapper $clientMapper,
*/
public function hashOrEncryptSecretBasedOnNextcloudVersion(string $secret, string $nextcloudVersion): string {
switch (true) {
case version_compare($nextcloudVersion, '30.0.0') >= 0:
case version_compare($nextcloudVersion, '29.0.7') >= 0 && version_compare($nextcloudVersion, '30.0.0') < 0:
case version_compare($nextcloudVersion, '28.0.10') >= 0 && version_compare($nextcloudVersion, '29.0.0') < 0:
case version_compare($nextcloudVersion, '27.1.11.8') >= 0 && version_compare($nextcloudVersion, '28.0.0') < 0:
case version_compare($nextcloudVersion, '30.0.0.0') >= 0:
case version_compare($nextcloudVersion, '29.0.7.0') >= 0 && version_compare($nextcloudVersion, '30.0.0.0') < 0:
case version_compare($nextcloudVersion, '28.0.10.0') >= 0 && version_compare($nextcloudVersion, '29.0.0.0') < 0:
case version_compare($nextcloudVersion, '27.1.11.9') >= 0 && version_compare($nextcloudVersion, '28.0.0.0') < 0:
$encryptedSecret = bin2hex($this->crypto->calculateHMAC($secret));
break;
case version_compare($nextcloudVersion, '27.0.0') === 0:
case version_compare($nextcloudVersion, '27.0.0.0') >= 0 && version_compare($nextcloudVersion, '27.0.1.0') < 0:
$encryptedSecret = $secret;
break;
default:
Expand Down
73 changes: 61 additions & 12 deletions tests/lib/Service/OauthSeviceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,56 @@ protected function getOauthServiceMock(
public function gethashOrEncryptSecretBasedOnNextcloudVersion(): array {
return [
[
"30.0.0",
"30.0.0.14",
"calculateHMAC"
],
[
"29.0.7",
"30.0.0.1",
"calculateHMAC"
],
[
"29.1.0",
"29.0.7.0",
"calculateHMAC"
],
[
"29.0.6",
"29.1.0.0",
"calculateHMAC"
],
[
"29.0.6.0",
"encrypt"
],
[
"28.0.10",
"28.0.10.0",
"calculateHMAC"
],
[
"28.2.0",
"28.2.0.0",
"calculateHMAC"
],
[
"28.0.0.0",
"encrypt"
],
[
"28.0.10.1",
"calculateHMAC"
],
[
"28.0.0",
"28.0.0.1",
"encrypt"
],
[
"29.0.0",
"29.0.0.0",
"encrypt"
],
[
"29.0.0.1",
"encrypt"
],
[
"27.1.11.8",
"calculateHMAC"
"encrypt"
],
[
"27.1.12.0",
Expand All @@ -101,6 +117,34 @@ public function gethashOrEncryptSecretBasedOnNextcloudVersion(): array {
[
"27.1.1.0",
"encrypt"
],
[
"27.0.1.0",
"encrypt"
],
[
"27.0.1.1",
"encrypt"
],
[
"27.0.0.0",
null
],
[
"27.0.0.1",
null
],
[
"27.0.0.99",
null
],
[
"27.1.11.9",
"calculateHMAC"
],
[
"29.0.7.1",
"calculateHMAC"
]
];
}
Expand All @@ -109,15 +153,20 @@ public function gethashOrEncryptSecretBasedOnNextcloudVersion(): array {
/**
* @dataProvider gethashOrEncryptSecretBasedOnNextcloudVersion
* @param string $nextcloudVersion
* @param string $hashOrEncryptFunction
* @param string|null $hashOrEncryptFunction
*
* @return void
*
*/
public function testGetHashedOrEncryptedClientSecretBasedOnNextcloudVersions(string $nextcloudVersion, string $hashOrEncryptFunction) {
public function testGetHashedOrEncryptedClientSecretBasedOnNextcloudVersions(string $nextcloudVersion, ?string $hashOrEncryptFunction) {
$iCryptoMock = $this->getMockBuilder(ICrypto::class)->getMock();
$oAuthService = $this->getOauthServiceMock(null, null, $iCryptoMock);
$iCryptoMock->expects($this->once())->method($hashOrEncryptFunction);
if ($hashOrEncryptFunction !== null) {
$iCryptoMock->expects($this->once())->method($hashOrEncryptFunction);
} else {
$iCryptoMock->expects($this->never())->method('calculateHMAC');
$iCryptoMock->expects($this->never())->method('encrypt');
}
$oAuthService->hashOrEncryptSecretBasedOnNextcloudVersion("client_secret", $nextcloudVersion);
}
}

0 comments on commit 2a96d49

Please sign in to comment.