1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: arthur |
5
|
|
|
* Date: 04.10.18 |
6
|
|
|
* Time: 17:25. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Foundation\Repositories; |
10
|
|
|
|
11
|
|
|
use Foundation\Exceptions\Exception; |
12
|
|
|
use Illuminate\Validation\UnauthorizedException; |
13
|
|
|
use Modules\User\Contracts\UserServiceContract; |
14
|
|
|
use Modules\User\Entities\User; |
15
|
|
|
use MongoDB\BSON\ObjectId; |
16
|
|
|
|
17
|
|
|
class Auth0UserRepository extends \Auth0\Login\Repository\Auth0UserRepository |
18
|
|
|
{ |
19
|
|
|
protected $service; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Auth0UserRepository constructor. |
23
|
|
|
* |
24
|
|
|
* @param $service |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct(UserServiceContract $service) |
27
|
|
|
{ |
28
|
3 |
|
$this->service = $service; |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/* This class is used on api authN to fetch the user based on the jwt.*/ |
32
|
2 |
|
public function getUserByDecodedJWT($jwt) |
33
|
|
|
{ |
34
|
|
|
/* |
35
|
|
|
* The `sub` claim in the token represents the subject of the token |
36
|
|
|
* and it is always the `user_id` |
37
|
|
|
*/ |
38
|
2 |
|
$jwt->user_id = $jwt->sub; |
39
|
|
|
|
40
|
2 |
|
return $this->upsertUser($jwt); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getUserByUserInfo($userInfo) |
44
|
|
|
{ |
45
|
|
|
return $this->upsertUser($userInfo['profile']); |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
protected function upsertUser($profile) |
49
|
|
|
{ |
50
|
2 |
|
if (!isset($profile->user_id)) { |
51
|
|
|
throw new Exception('Missing token information: Auth0 user id is not set'); |
52
|
|
|
} |
53
|
2 |
|
$identifier = explode('|', $profile->user_id); |
54
|
2 |
|
$identityProvider = $identifier[0]; |
55
|
2 |
|
$id = $identifier[1]; |
56
|
|
|
|
57
|
2 |
|
$user = $this->service->find($id); |
58
|
|
|
|
59
|
2 |
|
if ($user === null || !$this->userEqualsProfile($user, $profile)) { |
60
|
|
|
try { |
61
|
1 |
|
if ($user === null) { |
62
|
1 |
|
$user = new User(); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$user->_id = new ObjectId($id); |
|
|
|
|
66
|
1 |
|
$user->provider = $identityProvider; |
|
|
|
|
67
|
1 |
|
$user->email = $profile->email; |
|
|
|
|
68
|
1 |
|
$user->username = $profile->nickname; |
|
|
|
|
69
|
1 |
|
$user->name = $profile->name; |
|
|
|
|
70
|
1 |
|
$user->avatar = $profile->picture; |
|
|
|
|
71
|
1 |
|
$user->save(); |
72
|
|
|
} catch (\Exception $exception) { |
73
|
|
|
throw new UnauthorizedException('Profile data is not set in the token '); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
return $user; |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
private function userEqualsProfile($user, $profile) |
81
|
|
|
{ |
82
|
1 |
|
return $user->username === $profile->nickname && $user->email === $profile->email && $user->name === $profile->name && $user->avatar === $profile->picture; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getUserByIdentifier($identifier) |
86
|
|
|
{ |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|