Skip to content

Commit ee2739d

Browse files
romanetarsmarcet
authored andcommitted
refactor: refactoring password validation and password change panel (#46)
* refactor: refactoring password validation and password change panel Signed-off-by: romanetar <roman_ag@hotmail.com> * fix: review comments --------- Signed-off-by: romanetar <roman_ag@hotmail.com> Change-Id: I535e3b0b75bb439ab560954514686fa5d7a0349a
1 parent e6b8fd6 commit ee2739d

File tree

22 files changed

+151
-171
lines changed

22 files changed

+151
-171
lines changed

.env.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,7 @@ AUTH_ALLOWS_NATIVE_AUTH_CONFIG=1
109109
MAIL_SEND_WELCOME_EMAIL=1
110110
DEFAULT_PROFILE_IMAGE=
111111

112-
AUTH_PASSWORD_RESET_LIFETIME=1800
112+
AUTH_PASSWORD_RESET_LIFETIME=1800
113+
114+
AUTH_PASSWORD_SHAPE_PATTERN="^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-])"
115+
AUTH_PASSWORD_SHAPE_WARNING="Password must include at least one uppercase letter, one lowercase letter, one number, and one special character."

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ model.sql
4848
/public/assets/*.woff
4949
/public/assets/*.png
5050
/public/assets/*.txt
51+
/.env.local
52+
/.phpunit.cache/

app/Http/Controllers/Auth/ForgotPasswordController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use App\Http\Controllers\Controller;
1616
use App\libs\Utils\EmailUtils;
1717
use App\Services\Auth\IUserService;
18-
use Illuminate\Support\Facades\Auth;
1918
use Illuminate\Support\Facades\Log;
2019
use Illuminate\Support\Facades\Validator;
2120
use Illuminate\Http\Request as LaravelRequest;

app/Http/Controllers/Auth/ResetPasswordController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function reset(LaravelRequest $request)
137137

138138
$this->user_service->resetPassword($payload['token'], $payload['password']);
139139

140-
return view("auth.passwords.reset_success");
140+
return view("auth.passwords.reset_success", ['email' => $payload['email']]);
141141
}
142142
catch (ValidationException $ex){
143143
Log::warning($ex);

app/Providers/AppServiceProvider.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
* See the License for the specific language governing permissions and
1212
* limitations under the License.
1313
**/
14+
15+
use App\libs\Utils\TextUtils;
1416
use Illuminate\Support\Facades\App;
1517
use Illuminate\Support\Facades\Config;
1618
use Illuminate\Support\Facades\Log;
1719
use Illuminate\Support\ServiceProvider;
1820
use Illuminate\Support\Facades\Validator;
21+
use models\exceptions\ValidationException;
1922
use Sokil\IsoCodes\IsoCodesFactory;
2023
use Validators\CustomValidator;
2124
use App\Http\Utils\Log\LaravelMailerHandler;
@@ -99,12 +102,28 @@ public function boot()
99102
});
100103

101104
Validator::extend("password_policy", function($attribute, $value, $parameters, $validator){
102-
$min = 8;
103-
$validator->addReplacer('password_policy', function($message, $attribute, $rule, $parameters) use ($validator, $min) {
104-
return sprintf("The %s must be %s–30 characters, and must include a special character", $attribute, $min);
105+
$password = TextUtils::trim($value);
106+
107+
$min_length = Config::get("auth.password_min_length");
108+
if (strlen($password) < $min_length) {
109+
return false;
110+
}
111+
112+
$max_length = Config::get("auth.password_max_length");
113+
if (strlen($password) > $max_length) {
114+
return false;
115+
}
116+
$warning = Config::get("auth.password_shape_warning");
117+
$pattern = Config::get("auth.password_shape_pattern");
118+
if (!preg_match("/$pattern/", $password)) {
119+
return false;
120+
}
121+
122+
$validator->addReplacer('password_policy', function($message, $attribute, $rule, $parameters) use ($validator, $min_length, $max_length, $warning) {
123+
return sprintf("The %s must be %s–%s characters, and %s", $attribute, $min_length, $max_length, $warning);
105124
});
106125

107-
return preg_match("/^((?=.*?[#?!@()$%^&*=_{}[\]:;\"'|<>,.\/~`±§+-])).{8,30}$/", $value);
126+
return true;
108127
});
109128
}
110129

app/Strategies/DefaultLoginStrategy.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
* limitations under the License.
1313
**/
1414

15+
use App\libs\Utils\EmailUtils;
16+
use Illuminate\Support\Facades\Request;
1517
use App\libs\Auth\SocialLoginProviders;
1618
use Illuminate\Support\Facades\Log;
19+
use Illuminate\Support\Facades\Session;
20+
use OAuth2\OAuth2Protocol;
1721
use Utils\IPHelper;
1822
use Services\IUserActionService;
1923
use Utils\Services\IAuthService;
@@ -48,6 +52,26 @@ public function getLogin()
4852
{
4953
Log::debug(sprintf("DefaultLoginStrategy::getLogin"));
5054

55+
// login hint processing
56+
57+
$login_hint = null;
58+
if(Request::has(OAuth2Protocol::OAuth2Protocol_LoginHint)){
59+
$login_hint = Request::query(OAuth2Protocol::OAuth2Protocol_LoginHint);
60+
if(!EmailUtils::isValidEmail($login_hint))
61+
$login_hint = null;
62+
}
63+
64+
if(!empty($login_hint)) {
65+
$user = $this->auth_service->getUserByUsername($login_hint);
66+
if(!is_null($user)) {
67+
Session::put('username', $user->getEmail());
68+
Session::put('user_fullname', $user->getFullName());
69+
Session::put('user_pic', $user->getPic());
70+
Session::put('user_verified', true);
71+
Session::save();
72+
}
73+
}
74+
5175
if (Auth::guest())
5276
return View::make("auth.login", [
5377
'supported_providers' => SocialLoginProviders::buildSupportedProviders()
@@ -73,7 +97,7 @@ public function postLogin(array $params = [])
7397
return Redirect::intended($default_url);
7498
}
7599

76-
public function cancelLogin()
100+
public function cancelLogin()
77101
{
78102
return Redirect::action("HomeController@index");
79103
}

app/libs/Auth/Models/User.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,12 +1321,28 @@ public function getPassword(): ?string
13211321

13221322
/**
13231323
* @param string $password
1324+
* @throws ValidationException
13241325
*/
13251326
public function setPassword(string $password): void
13261327
{
13271328
$password = TextUtils::trim($password);
13281329

1329-
if(empty($this->password_enc)){
1330+
$min_length = Config::get("auth.password_min_length");
1331+
if (strlen($password) < $min_length) {
1332+
throw new ValidationException("Password must be at least $min_length characters.");
1333+
}
1334+
1335+
$max_length = Config::get("auth.password_max_length");
1336+
if (strlen($password) > $max_length) {
1337+
throw new ValidationException("Password must be at most $max_length characters.");
1338+
}
1339+
1340+
$pattern = Config::get("auth.password_shape_pattern");
1341+
if (!preg_match("/$pattern/", $password)) {
1342+
throw new ValidationException(Config::get("auth.password_shape_warning"));
1343+
}
1344+
1345+
if (empty($this->password_enc)) {
13301346
$this->password_enc = AuthHelper::AlgNative;
13311347
}
13321348
$this->password_salt = AuthHelper::generateSalt(self::SaltLen, $this->password_enc);

config/auth.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
'password_reset_lifetime' => env('AUTH_PASSWORD_RESET_LIFETIME', 1800),
103103
'password_min_length' => env('AUTH_PASSWORD_MIN_LENGTH', 8),
104104
'password_max_length' => env('AUTH_PASSWORD_MAX_LENGTH', 30),
105+
'password_shape_pattern' => env('AUTH_PASSWORD_SHAPE_PATTERN', '^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-])'),
106+
'password_shape_warning' => env('AUTH_PASSWORD_SHAPE_WARNING', 'Password must include at least one uppercase letter, one lowercase letter, one number, and one special character.'),
105107
'verification_email_lifetime' => env("AUTH_VERIFICATION_EMAIL_LIFETIME", 600),
106108
'allows_native_auth' => env('AUTH_ALLOWS_NATIVE_AUTH', 1),
107109
'allows_native_on_config' => env('AUTH_ALLOWS_NATIVE_AUTH_CONFIG', 1),

resources/js/admin/edit_user/components/password_change_panel.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

resources/js/admin/edit_user/edit_user.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useEffect, useState} from "react";
1+
import React, {useState} from "react";
22
import ReactDOM from "react-dom";
33
import ArrowBack from '@material-ui/icons/ArrowBack';
44
import Button from "@material-ui/core/Button";
@@ -25,10 +25,11 @@ import ProfileImageUploader from "./components/profile_image_uploader/profile_im
2525
import Navbar from "../../components/navbar/navbar";
2626
import Divider from "@material-ui/core/Divider";
2727
import Link from "@material-ui/core/Link";
28-
import PasswordChangePanel from "./components/password_change_panel";
28+
import PasswordChangePanel from "../../components/password_change_panel";
2929
import LoadingIndicator from "../../components/loading_indicator";
3030
import TopLogo from "../../components/top_logo/top_logo";
3131
import {handleErrorResponse} from "../../utils";
32+
import {buildPasswordValidationSchema} from "../../validator";
3233

3334
import styles from "./edit_user.module.scss";
3435

@@ -65,15 +66,7 @@ const EditUserPage = ({
6566
.email("Enter a valid email"),
6667
third_email: string("Email is required")
6768
.email("Enter a valid email"),
68-
password: string()
69-
.min(passwordPolicy.min_length, `Password must be at least ${passwordPolicy.min_length} characters`)
70-
.max(passwordPolicy.max_length, `Password must be at most ${passwordPolicy.max_length} characters`)
71-
.matches(
72-
/^(?=.*[a-z])(?=.*[!@#$%^&*])/,
73-
"Password must include a special character"
74-
),
75-
password_confirmation: string()
76-
.oneOf([ref('password'), null], 'Passwords must match')
69+
...buildPasswordValidationSchema(passwordPolicy)
7770
});
7871
}
7972

0 commit comments

Comments
 (0)