|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Music app |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Pauli Järvinen <[email protected]> |
|
10
|
|
|
* @copyright Pauli Järvinen 2024 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Music\Utility; |
|
14
|
|
|
|
|
15
|
|
|
use OCA\Music\Db\Cache; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Service creating signature tokens for given URLs. This can be used to prove that an URL passed |
|
19
|
|
|
* by the client has been previously created by this back-end and the client is not trying to trick |
|
20
|
|
|
* the back-end to relay any other HTTP traffic. If we would allow making calls to just any URL, |
|
21
|
|
|
* then that would undermine the purpose of having the Content-Security-Policy in place. |
|
22
|
|
|
*/ |
|
23
|
|
|
class StreamTokenService { |
|
24
|
|
|
|
|
25
|
|
|
private Cache $cache; |
|
26
|
|
|
private ?string $secret; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(Cache $cache) { |
|
29
|
|
|
$this->cache = $cache; |
|
30
|
|
|
$this->secret = null; // lazy load |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function tokenForUrl(string $url) : string { |
|
34
|
|
|
$secret = $this->getPrivateSecret(); |
|
35
|
|
|
return self::createToken($url, $secret); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function urlTokenIsValid(string $url, string $token) : bool { |
|
39
|
|
|
$secret = $this->getPrivateSecret(); |
|
40
|
|
|
return self::tokenIsValid($token, $url, $secret); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private static function createToken(string $message, string $privateSecret) : string { |
|
44
|
|
|
$salt = \random_bytes(32); |
|
45
|
|
|
$hash = \hash('sha256', $salt . $message . $privateSecret, /*binary=*/true); |
|
46
|
|
|
return \base64_encode($salt . $hash); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
private static function tokenIsValid(string $token, string $message, string $privateSecret) : bool { |
|
50
|
|
|
$binToken = (string)\base64_decode($token); |
|
51
|
|
|
$salt = \substr($binToken, 0, 32); |
|
52
|
|
|
$validBinToken = $salt . \hash('sha256', $salt . $message . $privateSecret, /*binary=*/true); |
|
53
|
|
|
return ($binToken === $validBinToken); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function getPrivateSecret() : string { |
|
57
|
|
|
// Load the secret all the way from the DB only once per request and cache it for later |
|
58
|
|
|
// invocations to avoid flooding DB requests when parsing a large playlist file for Files. |
|
59
|
|
|
if ($this->secret === null) { |
|
60
|
|
|
$this->secret = $this->getPrivateSecretFromDb(); |
|
61
|
|
|
} |
|
62
|
|
|
return $this->secret; |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function getPrivateSecretFromDb() : string { |
|
66
|
|
|
$privateSecretBase64 = $this->cache->get('', 'radioStreamSecret'); |
|
67
|
|
|
|
|
68
|
|
|
if ($privateSecretBase64 === null) { |
|
69
|
|
|
$privateSecret = \random_bytes(32); |
|
70
|
|
|
$privateSecretBase64 = \base64_encode($privateSecret); |
|
71
|
|
|
$this->cache->set('', 'radioStreamSecret', $privateSecretBase64); |
|
72
|
|
|
} else { |
|
73
|
|
|
$privateSecret = \base64_decode($privateSecretBase64); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $privateSecret; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
} |