-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegisterController.php
More file actions
285 lines (241 loc) · 11.3 KB
/
RegisterController.php
File metadata and controls
285 lines (241 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php namespace App\Http\Controllers\Auth;
/**
* Copyright 2019 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Http\Controllers\Controller;
use App\Http\Utils\CountryList;
use RyanChandler\LaravelCloudflareTurnstile\Rules\Turnstile;
use App\Services\Auth\IUserService;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request as LaravelRequest;
use models\exceptions\ValidationException;
use OAuth2\Factories\OAuth2AuthorizationRequestFactory;
use OAuth2\OAuth2Message;
use OAuth2\Repositories\IClientRepository;
use OAuth2\Services\IMementoOAuth2SerializerService;
use Exception;
/**
* Class RegisterController
* @package App\Http\Controllers\Auth
*/
final class RegisterController extends Controller
{
/**
* @var IUserService
*/
private $user_service;
/**
* @var IClientRepository
*/
private $client_repository;
/**
* @var IMementoOAuth2SerializerService
*/
private $memento_service;
public function __construct
(
IClientRepository $client_repository,
IUserService $user_service,
IMementoOAuth2SerializerService $memento_service
)
{
$this->middleware('guest');
$this->user_service = $user_service;
$this->client_repository = $client_repository;
$this->memento_service = $memento_service;
}
/**
* @param LaravelRequest $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws ValidationException
*/
public function showRegistrationForm(LaravelRequest $request)
{
try {
// if we already logged in ... continue flow
if(Auth::check()){
Log::warning("RegisterController::showRegistrationForm user already logged in, checking if we have a client id");
if ($request->has("redirect_uri") && $request->has("client_id")) {
$redirect_uri = $request->get("redirect_uri");
$client_id = $request->get("client_id");
Log::debug(sprintf("RegisterController::showRegistrationForm redirect_uri %s client_id %s", $redirect_uri, $client_id));
$client = $this->client_repository->getClientById($client_id);
if (is_null($client))
throw new ValidationException("Client does not exists.");
if (!$client->isUriAllowed($redirect_uri))
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client.", $redirect_uri));
Log::debug(sprintf("RegisterController::showRegistrationForm redirect_uri %s client_id %s redirecting", $redirect_uri, $client_id));
return Redirect::to($redirect_uri);
}
Log::debug("RegisterController::showRegistrationForm redirecting to home page");
return Redirect::to('/');
}
$params = [
"redirect_uri" => '',
"email" => '',
"first_name" => '',
"last_name" => '',
"client_id" => '',
'countries' => CountryList::getCountries()
];
// check if we have a former oauth2 request
if ($this->memento_service->exists()) {
Log::debug("RegisterController::showRegistrationForm exist a oauth auth request on session");
$oauth_auth_request = OAuth2AuthorizationRequestFactory::getInstance()->build
(
OAuth2Message::buildFromMemento($this->memento_service->load())
);
if ($oauth_auth_request->isValid()) {
$redirect_uri = $oauth_auth_request->getRedirectUri();
$client_id = $oauth_auth_request->getClientId();
Log::debug(sprintf( "RegisterController::showRegistrationForm exist a oauth auth request is valid for client id %s", $client_id));
$client = $this->client_repository->getClientById($client_id);
if (is_null($client))
throw new ValidationException("client does not exists");
if (!$client->isUriAllowed($redirect_uri))
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
$this->memento_service->serialize($oauth_auth_request->getMessage()->createMemento());
}
}
// check if we have explicit params at query string
if ($request->has("redirect_uri") && $request->has("client_id")) {
$redirect_uri = $request->get("redirect_uri");
$client_id = $request->get("client_id");
$client = $this->client_repository->getClientById($client_id);
if (is_null($client))
throw new ValidationException("client does not exists");
if (!$client->isUriAllowed($redirect_uri))
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
$params['redirect_uri'] = $redirect_uri;
$params['client_id'] = $client_id;
}
if($request->has('email')){
$params['email'] = $request->get("email");
}
if($request->has('first_name')){
$params['first_name'] = $request->get("first_name");
}
if($request->has('last_name')){
$params['last_name'] = $request->get("last_name");
}
return view('auth.register', $params);
}
catch(\Exception $ex){
Log::warning($ex);
}
return view("auth.register_error");
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
$rules = [
'first_name' => 'required|string|max:100',
'last_name' => 'required|string|max:100',
'country_iso_code' => 'required|string|country_iso_alpha2_code',
'email' => 'required|string|email|max:255',
'password' => 'required|string|confirmed|password_policy',
'cf-turnstile-response' => ['required', new Turnstile()],
];
if(!empty(Config::get("app.code_of_conduct_link", null))){
$rules['agree_code_of_conduct'] = 'required|string|in:true';
}
return Validator::make($data, $rules);
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(LaravelRequest $request)
{
$validator = null;
try {
$payload = $request->all();
$validator = $this->validator($payload);
if (!$validator->passes()) {
return back()
->withInput($request->only(['first_name', 'last_name', 'country_iso_code','email','client_id', 'redirect_uri']))
->withErrors($validator);
}
$user = $this->user_service->registerUser($payload);
$params = [
'client_id' => '',
'redirect_uri' => '',
];
// check if we have a former oauth2 request
if ($this->memento_service->exists()) {
Log::debug("RegisterController::register exist a oauth auth request on session");
$oauth_auth_request = OAuth2AuthorizationRequestFactory::getInstance()->build
(
OAuth2Message::buildFromMemento($this->memento_service->load())
);
if ($oauth_auth_request->isValid()) {
$redirect_uri = $oauth_auth_request->getRedirectUri();
$client_id = $oauth_auth_request->getClientId();
Log::debug(sprintf( "RegisterController::register exist a oauth auth request is valid for client id %s", $client_id));
$client = $this->client_repository->getClientById($client_id);
if (is_null($client))
throw new ValidationException("client does not exists");
if (!$client->isUriAllowed($redirect_uri))
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
$this->memento_service->serialize($oauth_auth_request->getMessage()->createMemento());
$params['redirect_uri'] = action('OAuth2\OAuth2ProviderController@auth');
Auth::login($user, false);
}
}
// check redirect uri with associated client
if($request->has("redirect_uri") && $request->has("client_id")){
$redirect_uri = $request->get("redirect_uri");
$client_id = $request->get("client_id");
$client = $this->client_repository->getClientById($client_id);
if(is_null($client))
throw new ValidationException("client does not exists");
if(!$client->isUriAllowed($redirect_uri))
throw new ValidationException(sprintf("redirect_uri %s is not allowed on associated client", $redirect_uri));
$params['client_id'] = $client_id;
$params['redirect_uri'] = $redirect_uri;
Auth::login($user, false);
}
return view("auth.register_success", $params);
}
catch (ValidationException $ex){
Log::warning($ex);
if(!is_null($validator)) {
$validator->getMessageBag()->add('validation', sprintf
(
"It looks like a user with this email address already exists." .
"You can either <a href=\'%s\'>sign in</a> or <a href=\'%s\'>reset your password</a> if you\'ve forgotten it.",
URL::action("UserController@getLogin"),
URL::action("Auth\ForgotPasswordController@showLinkRequestForm")
));
}
return back()
->withInput($request->only(['first_name', 'last_name', 'country_iso_code','email']))
->withErrors($validator);
}
catch(Exception $ex){
Log::warning($ex);
}
return view("auth.register_error");
}
}