1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: arthur |
5
|
|
|
* Date: 13.10.18 |
6
|
|
|
* Time: 19:08. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Modules\Auth0\Drivers; |
10
|
|
|
|
11
|
|
|
use Foundation\Exceptions\NotImplementedException; |
12
|
|
|
use Illuminate\Validation\UnauthorizedException; |
13
|
|
|
use Modules\Auth0\Abstracts\Auth0IdentityProviderTransformer; |
14
|
|
|
use Modules\Auth0\Abstracts\Auth0ProfileValidator; |
15
|
|
|
use Modules\Auth0\Transformers\Auth0DatabaseProfileTransformer; |
16
|
|
|
use Modules\Auth0\Transformers\Auth0FacebookProfileTransformer; |
17
|
|
|
use Modules\Auth0\Transformers\Auth0GoogleProfileTransformer; |
18
|
|
|
use Modules\Auth0\Validators\Auth0DatabaseProfileValidator; |
19
|
|
|
use Modules\Auth0\Validators\Auth0FacebookProfileValidator; |
20
|
|
|
use Modules\Auth0\Validators\Auth0GoogleProfileValidator; |
21
|
|
|
use Modules\User\Entities\User; |
22
|
|
|
|
23
|
|
|
class Auth0UserProfileStorageDriver |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \stdClass |
27
|
|
|
*/ |
28
|
|
|
protected $profile; |
29
|
|
|
/** |
30
|
|
|
* @var User |
31
|
|
|
*/ |
32
|
|
|
protected $user; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $identityProvider; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $requiredProfileAttributes = [ |
42
|
|
|
'email', |
43
|
|
|
'name', |
44
|
|
|
'avatar', |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var Auth0IdentityProviderTransformer |
49
|
|
|
*/ |
50
|
|
|
protected $transformer; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var Auth0ProfileValidator |
54
|
|
|
*/ |
55
|
|
|
protected $validator; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Auth0UserStorageDriver constructor. |
59
|
|
|
* |
60
|
|
|
* @param $profile |
61
|
|
|
* @param $user |
62
|
|
|
* @param $identityProvider |
63
|
|
|
*/ |
64
|
|
|
public function __construct(User $user, \stdClass $profile, string $identityProvider) |
65
|
|
|
{ |
66
|
|
|
$this->profile = $profile; |
67
|
|
|
$this->user = $user; |
68
|
|
|
$this->identityProvider = $identityProvider; |
69
|
|
|
$this->boot(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws NotImplementedException | UnauthorizedException |
74
|
|
|
*/ |
75
|
|
|
protected function boot() |
76
|
|
|
{ |
77
|
|
|
$this->loadServices(); |
78
|
|
|
|
79
|
|
|
if (! $this->validator->validate($this->profile)) { |
80
|
|
|
throw new UnauthorizedException('Invalid profile data found in the access token'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @throws NotImplementedException |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function loadServices() |
90
|
|
|
{ |
91
|
|
|
switch ($this->identityProvider) { |
92
|
|
|
case 'auth0': |
93
|
|
|
$this->transformer = new Auth0DatabaseProfileTransformer(); |
94
|
|
|
$this->validator = new Auth0DatabaseProfileValidator(); |
95
|
|
|
break; |
96
|
|
|
case 'facebook': |
97
|
|
|
$this->transformer = new Auth0FacebookProfileTransformer(); |
98
|
|
|
$this->validator = new Auth0FacebookProfileValidator(); |
99
|
|
|
break; |
100
|
|
|
case 'google-oauth2': |
101
|
|
|
$this->transformer = new Auth0GoogleProfileTransformer(); |
102
|
|
|
$this->validator = new Auth0GoogleProfileValidator(); |
103
|
|
|
break; |
104
|
|
|
default: |
105
|
|
|
throw new NotImplementedException('Unsupported identity provider'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function run() |
110
|
|
|
{ |
111
|
|
|
$profile = $this->transformer->transformProfile($this->profile); |
112
|
|
|
|
113
|
|
|
if ($this->profileHasChanged()) { |
114
|
|
|
$profile['provider'] = $this->identityProvider; |
115
|
|
|
$this->user->fill($profile); |
116
|
|
|
$this->user->save(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $this->user; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function profileHasChanged(): bool |
123
|
|
|
{ |
124
|
|
|
$user = $this->user->toArray(); |
125
|
|
|
$profile = (array) $this->profile; |
126
|
|
|
|
127
|
|
|
return ! array_is_subset_of($profile, $user); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|