|
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
|
|
|
|
|
12
|
|
|
use Modules\User\Contracts\UserServiceContract; |
|
13
|
|
|
use Modules\User\Entities\User; |
|
14
|
|
|
|
|
15
|
|
|
class Auth0UserRepository extends \Auth0\Login\Repository\Auth0UserRepository |
|
16
|
|
|
{ |
|
17
|
|
|
protected $service; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Auth0UserRepository constructor. |
|
21
|
|
|
* @param $service |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(UserServiceContract $service) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->service = $service; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/* This class is used on api authN to fetch the user based on the jwt.*/ |
|
30
|
|
|
public function getUserByDecodedJWT($jwt) { |
|
31
|
|
|
/* |
|
32
|
|
|
* The `sub` claim in the token represents the subject of the token |
|
33
|
|
|
* and it is always the `user_id` |
|
34
|
|
|
*/ |
|
35
|
|
|
$jwt->user_id = $jwt->sub; |
|
36
|
|
|
|
|
37
|
|
|
return $this->upsertUser($jwt); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getUserByUserInfo($userInfo) { |
|
41
|
|
|
return $this->upsertUser($userInfo['profile']); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
protected function upsertUser($profile) { |
|
45
|
|
|
|
|
46
|
|
|
// Note: Requires configured database access |
|
47
|
|
|
$user = $this->service->getUserByAuth0Id($profile->user_id); |
|
48
|
|
|
|
|
49
|
|
|
if ($user === null) { |
|
50
|
|
|
// If not, create one |
|
51
|
|
|
$user = new User(); |
|
52
|
|
|
$user->email = $profile->email; // you should ask for the email scope |
|
|
|
|
|
|
53
|
|
|
$user->auth0_id = $profile->user_id; |
|
|
|
|
|
|
54
|
|
|
$user->name = $profile->name; // you should ask for the name scope |
|
|
|
|
|
|
55
|
|
|
$user->save(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $user; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getUserByIdentifier($identifier) { |
|
62
|
|
|
//Get the user info of the user logged in (probably in session) |
|
63
|
|
|
$user = \App::make('auth0')->getUser(); |
|
64
|
|
|
|
|
65
|
|
|
if ($user === null) return null; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
// build the user |
|
68
|
|
|
$user = $this->getUserByUserInfo($user); |
|
69
|
|
|
|
|
70
|
|
|
// it is not the same user as logged in, it is not valid |
|
71
|
|
|
if ($user && $user->auth0id == $identifier) { |
|
|
|
|
|
|
72
|
|
|
return $user; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: