|
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
|
|
|
public function __construct(UserServiceContract $service) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->service = $service; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/* This class is used on api authN to fetch the user based on the jwt.*/ |
|
32
|
|
|
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
|
|
|
$jwt->user_id = $jwt->sub; |
|
39
|
|
|
|
|
40
|
|
|
return $this->upsertUser($jwt); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getUserByUserInfo($userInfo) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->upsertUser($userInfo['profile']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function upsertUser($profile) |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset($profile->user_id)) |
|
51
|
|
|
throw new Exception("Missing token information: Auth0 user id is not set"); |
|
52
|
|
|
|
|
53
|
|
|
$identifier = explode('|', $profile->user_id); |
|
54
|
|
|
$identityProvider = $identifier[0]; |
|
55
|
|
|
$id = $identifier[1]; |
|
56
|
|
|
|
|
57
|
|
|
$user = $this->service->find($id); |
|
58
|
|
|
|
|
59
|
|
|
if ($user === null || !$this->userEqualsProfile($user, $profile)) { |
|
60
|
|
|
try { |
|
61
|
|
|
|
|
62
|
|
|
if ($user === null) |
|
63
|
|
|
$user = new User(); |
|
64
|
|
|
|
|
65
|
|
|
$user->_id = new ObjectId($id); |
|
|
|
|
|
|
66
|
|
|
$user->provider = $identityProvider; |
|
|
|
|
|
|
67
|
|
|
$user->email = $profile->email; |
|
|
|
|
|
|
68
|
|
|
$user->username = $profile->nickname; |
|
|
|
|
|
|
69
|
|
|
$user->name = $profile->name; |
|
|
|
|
|
|
70
|
|
|
$user->avatar = $profile->picture; |
|
|
|
|
|
|
71
|
|
|
$user->save(); |
|
72
|
|
|
} catch (\Exception $exception) { |
|
73
|
|
|
throw new UnauthorizedException('Profile data is not set in the token '); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $user; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function userEqualsProfile($user, $profile) |
|
81
|
|
|
{ |
|
82
|
|
|
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
|
|
|
//Get the user info of the user logged in (probably in session) |
|
88
|
|
|
$user = \App::make('auth0')->getUser(); |
|
89
|
|
|
|
|
90
|
|
|
if ($user === null) { |
|
|
|
|
|
|
91
|
|
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// build the user |
|
95
|
|
|
$user = $this->getUserByUserInfo($user); |
|
96
|
|
|
|
|
97
|
|
|
// it is not the same user as logged in, it is not valid |
|
98
|
|
|
if ($user && $user->auth0id == $identifier) { |
|
|
|
|
|
|
99
|
|
|
return $user; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|