-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSOUser.php
50 lines (43 loc) · 1.4 KB
/
SSOUser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace App\Values;
use Illuminate\Http\Request;
use Laravel\Socialite\Contracts\User as SocialiteUser;
use Webmozart\Assert\Assert;
final class SSOUser
{
private function __construct(
public string $provider,
public string $id,
public string $email,
public string $name,
public ?string $avatar,
) {
self::assertValidProvider($provider);
}
public static function fromSocialite(SocialiteUser $socialiteUser, string $provider): self
{
return new self(
provider: $provider,
id: $socialiteUser->getId(),
email: $socialiteUser->getEmail(),
name: $socialiteUser->getName(),
avatar: $socialiteUser->getAvatar(),
);
}
public static function fromProxyAuthRequest(Request $request): self
{
$identifier = $request->header(config('koel.proxy_auth.user_header'));
$email = filter_var($identifier, FILTER_VALIDATE_EMAIL) ?: "[email protected]";
return new self(
provider: 'Reverse Proxy',
id: $identifier,
email: $email,
name: $request->header(config('koel.proxy_auth.preferred_name_header')) ?: $identifier,
avatar: null,
);
}
public static function assertValidProvider(string $provider): void
{
Assert::oneOf($provider, ['Google', 'Reverse Proxy']);
}
}