1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace App\Services\Auth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\UserProvider; |
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
7
|
|
|
use App\Services\Auth\Contracts\OidcAuthenticatable; |
8
|
|
|
use App\Services\Auth\Contracts\OidcUserValidator; |
|
|
|
|
9
|
|
|
use App\Models\Manager; |
10
|
|
|
use App\Models\UserRole; |
11
|
|
|
use App\Models\Applicant; |
12
|
|
|
|
13
|
|
|
class BaseOidcUserProvider implements UserProvider { |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The Eloquent user model. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $model; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The role new users should be created with |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $defaultRole; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new user provider. |
33
|
|
|
* |
34
|
|
|
* @param string $model |
|
|
|
|
35
|
|
|
* @param string $defaultRole |
|
|
|
|
36
|
|
|
* @return void |
|
|
|
|
37
|
|
|
*/ |
38
|
2 |
|
public function __construct($model, $defaultRole) { |
|
|
|
|
39
|
2 |
|
$this->model = $model; |
40
|
2 |
|
$this->defaultRole = $defaultRole; |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retrieve a user by their unique identifier. |
45
|
|
|
* |
46
|
|
|
* @param mixed $identifier |
|
|
|
|
47
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function retrieveById($identifier) { |
|
|
|
|
50
|
|
|
$model = $this->createModel(); |
51
|
|
|
|
52
|
|
|
return $model->newQuery() |
|
|
|
|
53
|
|
|
->where($model->getAuthIdentifierName(), $identifier) |
|
|
|
|
54
|
|
|
->first(); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieve a user by their unique identifier and "remember me" token. |
59
|
|
|
* |
60
|
|
|
* @param mixed $identifier |
|
|
|
|
61
|
|
|
* @param string $token |
|
|
|
|
62
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function retrieveByToken($identifier, $token) { |
|
|
|
|
65
|
|
|
//TODO: Should we implement "remember me" tokens? |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Update the "remember me" token for the given user in storage. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
|
|
|
73
|
|
|
* @param string $token |
|
|
|
|
74
|
|
|
* @return void |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function updateRememberToken(Authenticatable $user, $token) { |
|
|
|
|
77
|
|
|
//TODO: Should we implement "remember me" tokens? |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Retrieve a user by the given credentials. |
82
|
|
|
* |
83
|
|
|
* @param array $credentials |
|
|
|
|
84
|
|
|
* @return App\Services\Auth\Contracts\OidcAuthenticatable|null |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function retrieveByCredentials(array $credentials) { |
|
|
|
|
87
|
|
|
if (empty($credentials)) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
if (isset($credentials['iss']) && isset($credentials['sub'])) { |
91
|
|
|
// First we will try to find a user that matches the openid issuer |
92
|
|
|
// and sub code. |
93
|
|
|
|
94
|
|
|
$model = $this->createModel(); |
95
|
|
|
|
96
|
|
|
$user = $model->findByOidcSub($credentials['iss'], $credentials['sub']); |
97
|
|
|
|
98
|
|
|
debugbar()->info("in Provider.retrieveByCredentials()"); |
99
|
|
|
if ($user) { |
100
|
|
|
debugbar()->info("Provider found user:"); |
101
|
|
|
debugbar()->info($user); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// If no user was found, use the provided credentials to create a |
105
|
|
|
// new user |
106
|
|
|
if ($user === null) { |
107
|
|
|
|
108
|
|
|
$user = $this->createUserFromCredentials($credentials); |
109
|
|
|
if ($user) { |
110
|
|
|
//If a user was created successfully, save it to database |
111
|
|
|
$user->save(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
debugbar()->info("Provider created user:"); |
115
|
|
|
debugbar()->info($user); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
//If running in a local environment, and FORCE_ADMIN is true, |
119
|
|
|
//automatically set any logged in user to (temporarilly) be an admin |
120
|
|
|
if (env('APP_ENV') == 'local' && env('FORCE_ADMIN', false)) { |
121
|
|
|
$adminRole = UserRole::where('name', 'admin')->firstOrFail(); |
122
|
|
|
$user->user_role_id = $adminRole->id; |
123
|
|
|
$user->user_role = $adminRole; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//Ensure the user has a proper profile associated with it |
127
|
|
|
//If now profile exists yet create one. |
128
|
|
|
//Admins should be givven an applicant and manager profile |
129
|
|
|
if ($user->user_role->name == 'applicant' || |
130
|
|
|
$user->user_role->name == 'admin') { |
|
|
|
|
131
|
|
|
$applicantProfile = Applicant::where(['user_id' => $user->id])->first(); |
132
|
|
|
if (!$applicantProfile) { |
133
|
|
|
$applicantProfile = new Applicant(); |
134
|
|
|
$applicantProfile->user_id = $user->id; |
135
|
|
|
$applicantProfile->save(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
if ($user->user_role->name == 'manager' || |
140
|
|
|
$user->user_role->name == 'admin') { |
|
|
|
|
141
|
|
|
$managerProfile = Manager::where(['user_id' => $user->id])->first(); |
142
|
|
|
if (!$managerProfile) { |
143
|
|
|
$managerProfile = new Manager(); |
144
|
|
|
$managerProfile->user_id = $user->id; |
145
|
|
|
$managerProfile->save(); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $user; |
150
|
|
|
} else { |
151
|
|
|
return; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create a new user object using the given credentials. |
157
|
|
|
* |
158
|
|
|
* @param array $credentials |
|
|
|
|
159
|
|
|
* @return App\Services\Auth\Contracts\OidcAuthenticatable|null |
|
|
|
|
160
|
|
|
*/ |
161
|
|
|
public function createUserFromCredentials(array $credentials) { |
|
|
|
|
162
|
|
|
//At a minimum, email, iss and sub codes must be available. |
163
|
|
|
if (!isset($credentials['email']) || !isset($credentials['iss']) || |
164
|
|
|
!isset($credentials['sub'])) { |
|
|
|
|
165
|
|
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$model = $this->createModel(); |
169
|
|
|
|
170
|
|
|
$name = isset($credentials['name']) ? $credentials['name'] : ""; |
171
|
|
|
|
172
|
|
|
return $model->createWithOidcCredentials($name, $credentials['email'], |
|
|
|
|
173
|
|
|
$credentials['iss'], $credentials['sub'], $this->defaultRole); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Validate a user against the given credentials. |
178
|
|
|
* |
179
|
|
|
* @param App\Services\Auth\Contracts\OidcAuthenticatable $user |
|
|
|
|
180
|
|
|
* @param array $credentials |
|
|
|
|
181
|
|
|
* @return bool |
|
|
|
|
182
|
|
|
*/ |
183
|
|
|
public function validateCredentials(Authenticatable $user, array $credentials) { |
|
|
|
|
184
|
|
|
debugbar()->info("in Provider.validateCredentials()"); |
185
|
|
|
|
186
|
|
|
return $user instanceof Authenticatable; |
187
|
|
|
//$subMatches = $credentials['sub'] === $user->getSub($credentials['iss']); |
188
|
|
|
//return $subMatches; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Create a new instance of the model. |
193
|
|
|
* |
194
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
195
|
|
|
*/ |
196
|
|
|
public function createModel() { |
|
|
|
|
197
|
|
|
$class = '\\' . ltrim($this->model, '\\'); |
198
|
|
|
|
199
|
|
|
return new $class; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Gets the name of the Eloquent user model. |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
public function getModel() { |
|
|
|
|
208
|
|
|
return $this->model; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Sets the name of the Eloquent user model. |
213
|
|
|
* |
214
|
|
|
* @param string $model |
|
|
|
|
215
|
|
|
* @return $this |
|
|
|
|
216
|
|
|
*/ |
217
|
|
|
public function setModel($model) { |
|
|
|
|
218
|
|
|
$this->model = $model; |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
224
|
|
|
|