|
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
|
|
|
|
|
12
|
|
|
use Foundation\Exceptions\NotImplementedException; |
|
13
|
|
|
use Illuminate\Validation\UnauthorizedException; |
|
14
|
|
|
use Modules\Auth0\Abstracts\Auth0ChangeDetectorGuard; |
|
|
|
|
|
|
15
|
|
|
use Modules\Auth0\Abstracts\Auth0IdentityProviderTransformer; |
|
16
|
|
|
use Modules\Auth0\Abstracts\Auth0ProfileValidator; |
|
17
|
|
|
use Modules\Auth0\Guards\Auth0DatabaseProfileChangeDetector; |
|
|
|
|
|
|
18
|
|
|
use Modules\Auth0\Guards\Auth0FacebookProfileChangeDetector; |
|
|
|
|
|
|
19
|
|
|
use Modules\Auth0\Guards\Auth0GoogleProfileChangeDetector; |
|
|
|
|
|
|
20
|
|
|
use Modules\Auth0\Transformers\Auth0DatabaseProfileTransformer; |
|
21
|
|
|
use Modules\Auth0\Transformers\Auth0FacebookProfileTransformer; |
|
22
|
|
|
use Modules\Auth0\Transformers\Auth0GoogleProfileTransformer; |
|
23
|
|
|
use Modules\Auth0\Validators\Auth0DatabaseProfileValidator; |
|
24
|
|
|
use Modules\Auth0\Validators\Auth0FacebookProfileValidator; |
|
25
|
|
|
use Modules\Auth0\Validators\Auth0GoogleProfileValidator; |
|
26
|
|
|
use Modules\User\Entities\User; |
|
27
|
|
|
|
|
28
|
|
|
class Auth0UserProfileStorageDriver |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var \stdClass |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $profile; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var User |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $user; |
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $identityProvider; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $requiredProfileAttributes = [ |
|
47
|
|
|
"email", |
|
48
|
|
|
"name", |
|
49
|
|
|
"avatar" |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var Auth0IdentityProviderTransformer |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $transformer; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var Auth0ProfileValidator |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $validator; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Auth0UserStorageDriver constructor. |
|
64
|
|
|
* @param $profile |
|
65
|
|
|
* @param $user |
|
66
|
|
|
* @param $identityProvider |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct(User $user, \stdClass $profile, string $identityProvider) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->profile = $profile; |
|
71
|
|
|
$this->user = $user; |
|
72
|
|
|
$this->identityProvider = $identityProvider; |
|
73
|
|
|
$this->boot(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @throws NotImplementedException | UnauthorizedException |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function boot() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->loadServices(); |
|
83
|
|
|
|
|
84
|
|
|
if (!$this->validator->validate($this->profile)) { |
|
85
|
|
|
throw new UnauthorizedException('Invalid profile data found in the access token'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return void |
|
91
|
|
|
* @throws NotImplementedException |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function loadServices() |
|
94
|
|
|
{ |
|
95
|
|
|
switch ($this->identityProvider) { |
|
96
|
|
|
case "auth0": |
|
97
|
|
|
$this->transformer = new Auth0DatabaseProfileTransformer(); |
|
98
|
|
|
$this->validator = new Auth0DatabaseProfileValidator(); |
|
99
|
|
|
break; |
|
100
|
|
|
case "facebook": |
|
101
|
|
|
$this->transformer = new Auth0FacebookProfileTransformer(); |
|
102
|
|
|
$this->validator = new Auth0FacebookProfileValidator(); |
|
103
|
|
|
break; |
|
104
|
|
|
case "google-oauth2": |
|
105
|
|
|
$this->transformer = new Auth0GoogleProfileTransformer(); |
|
106
|
|
|
$this->validator = new Auth0GoogleProfileValidator(); |
|
107
|
|
|
break; |
|
108
|
|
|
default: |
|
109
|
|
|
throw new NotImplementedException('Unsupported identity provider'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function run() |
|
114
|
|
|
{ |
|
115
|
|
|
$profile = $this->transformer->transformProfile($this->profile); |
|
116
|
|
|
|
|
117
|
|
|
if ($this->profileHasChanged()) { |
|
118
|
|
|
$profile['provider'] = $this->identityProvider; |
|
119
|
|
|
$this->user->fill($profile); |
|
120
|
|
|
$this->user->save(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this->user; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function profileHasChanged(): bool |
|
127
|
|
|
{ |
|
128
|
|
|
$user = $this->user->toArray(); |
|
129
|
|
|
$profile = (array)$this->profile; |
|
130
|
|
|
|
|
131
|
|
|
return !array_is_subset_of($profile, $user); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths