|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Usox\HyperSonic\Authentication; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Usox\HyperSonic\Authentication\Exception\AbstractAuthenticationException; |
|
9
|
|
|
use Usox\HyperSonic\Authentication\Exception\AuthenticationParamsMissingException; |
|
10
|
|
|
use Usox\HyperSonic\Authentication\Exception\UsernameMissingException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Manages the authentication of subsonic api requests |
|
14
|
|
|
*/ |
|
15
|
|
|
final class AuthenticationManager implements AuthenticationManagerInterface |
|
16
|
|
|
{ |
|
17
|
5 |
|
public function __construct( |
|
18
|
|
|
private readonly AuthenticationProviderInterface $authenticationProvider, |
|
19
|
|
|
) { |
|
20
|
5 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Will throw an exception on auth fault, otherwise nothing will happen |
|
24
|
|
|
* |
|
25
|
|
|
* @throws AbstractAuthenticationException |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function auth( |
|
28
|
|
|
ServerRequestInterface $request, |
|
29
|
|
|
): void { |
|
30
|
4 |
|
$queryParams = $request->getQueryParams(); |
|
31
|
|
|
|
|
32
|
4 |
|
$userName = $queryParams['u'] ?? null; |
|
33
|
|
|
|
|
34
|
4 |
|
if ($userName === null) { |
|
35
|
1 |
|
throw new UsernameMissingException(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
$token = $queryParams['t'] ?? null; |
|
39
|
3 |
|
$salt = $queryParams['s'] ?? null; |
|
40
|
|
|
|
|
41
|
|
|
// token auth? phew, thank god |
|
42
|
3 |
|
if ($token !== null && $salt !== null) { |
|
43
|
1 |
|
$this->authenticationProvider->authByToken( |
|
44
|
1 |
|
(string) $userName, |
|
45
|
1 |
|
(string) $token, |
|
46
|
1 |
|
(string) $salt, |
|
47
|
1 |
|
); |
|
48
|
|
|
|
|
49
|
1 |
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @var null|string $password */ |
|
53
|
2 |
|
$password = $queryParams['p'] ?? null; |
|
54
|
|
|
|
|
55
|
|
|
// meh, plaintext or hex-encoded password |
|
56
|
2 |
|
if ($password !== null) { |
|
57
|
1 |
|
if (str_starts_with($password, 'enc:')) { |
|
58
|
1 |
|
$password = hex2bin(str_replace('enc:', '', $password)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
$this->authenticationProvider->authByPassword( |
|
62
|
1 |
|
(string) $userName, |
|
63
|
1 |
|
(string) $password, |
|
64
|
1 |
|
); |
|
65
|
|
|
|
|
66
|
1 |
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// no auth method applies? throw exception |
|
70
|
1 |
|
throw new AuthenticationParamsMissingException(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|