Compare commits

...

2 Commits

Author SHA1 Message Date
b1ek 6722c9a75d
refactor: unfuck register request 2024-08-30 08:46:03 +10:00
b1ek e28348cfbf
docs: add laravel validation errors to openapi 2024-08-30 08:45:45 +10:00
7 changed files with 203 additions and 21 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Http\Requests\RegisterRequest;
use App\Models\User; use App\Models\User;
use Hash; use Hash;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
@ -10,9 +11,9 @@ use Validator;
class PublicUserController extends Controller class PublicUserController extends Controller
{ {
public function register(Request $request) public function register(RegisterRequest $request)
{ {
$data = $request->all()['user']; $data = $request->all();
if (User::where([ 'email' => $data['email'] ])->count() != 0) { if (User::where([ 'email' => $data['email'] ])->count() != 0) {
return response() return response()
->json('email_taken', 400); ->json('email_taken', 400);

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Requests;
use App\Rules\ZxcvbnRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
class RegisterRequest extends RestRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'last_name' => ['required', 'string'],
'name' => ['required', 'string'],
'middle_name' => ['required', 'string'],
'email' => ['required', 'email'],
'phone' => ['required', 'regex:/^\+\d+$/'],
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule ])],
];
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
class RestRequest extends FormRequest
{
protected function failedValidation(Validator $validator)
{
$exception = $validator->getException();
throw new HttpResponseException(response()->json($validator->errors(), 422));
}
}

24
app/Rules/ZxcvbnRule.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use ZxcvbnPhp\Zxcvbn;
class ZxcvbnRule implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$value = (string) $value;
$zxcvbn = new Zxcvbn();
if ($zxcvbn->passwordStrength($value)['score'] < 3) {
$fail('Password is not secure enough!');
}
}
}

View File

@ -6,6 +6,8 @@
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "^8.2", "php": "^8.2",
"bjeavons/zxcvbn-php": "^1.3",
"egulias/email-validator": "^4.0",
"laravel/framework": "^11.9", "laravel/framework": "^11.9",
"laravel/tinker": "^2.9" "laravel/tinker": "^2.9"
}, },

57
composer.lock generated
View File

@ -4,8 +4,63 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "7e8c3c14ff33b199b4a0838993eb8423", "content-hash": "847793c6725e2d2fecbf33402e6d4c4d",
"packages": [ "packages": [
{
"name": "bjeavons/zxcvbn-php",
"version": "1.3.1",
"source": {
"type": "git",
"url": "https://github.com/bjeavons/zxcvbn-php.git",
"reference": "994928ae5b17ecff8baa2406832d37bdf01116c0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/bjeavons/zxcvbn-php/zipball/994928ae5b17ecff8baa2406832d37bdf01116c0",
"reference": "994928ae5b17ecff8baa2406832d37bdf01116c0",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": "^7.2 | ^8.0 | ^8.1",
"symfony/polyfill-mbstring": ">=1.3.1"
},
"require-dev": {
"php-coveralls/php-coveralls": "*",
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "3.*"
},
"suggest": {
"ext-gmp": "Required for optimized binomial calculations (also requires PHP >= 7.3)"
},
"type": "library",
"autoload": {
"psr-4": {
"ZxcvbnPhp\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "See contributors",
"homepage": "https://github.com/bjeavons/zxcvbn-php"
}
],
"description": "Realistic password strength estimation PHP library based on Zxcvbn JS",
"homepage": "https://github.com/bjeavons/zxcvbn-php",
"keywords": [
"password",
"zxcvbn"
],
"support": {
"issues": "https://github.com/bjeavons/zxcvbn-php/issues",
"source": "https://github.com/bjeavons/zxcvbn-php/tree/1.3.1"
},
"time": "2021-12-21T18:37:02+00:00"
},
{ {
"name": "brick/math", "name": "brick/math",
"version": "0.12.1", "version": "0.12.1",

View File

@ -16,9 +16,6 @@ paths:
content: content:
application/json: application/json:
schema: schema:
type: object
properties:
user:
type: object type: object
properties: properties:
last_name: last_name:
@ -56,6 +53,12 @@ paths:
schema: schema:
type: string type: string
example: 'bad_password' example: 'bad_password'
422:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
/api/users/login: /api/users/login:
post: post:
tags: tags:
@ -86,10 +89,13 @@ paths:
| key | val | | key | val |
| --- | --- | | --- | --- |
| `bad_email` | email is not a valid email |
| `bad_password` | authentication unsuccessful; this means that there is either no user with this email, or password doesn't match | | `bad_password` | authentication unsuccessful; this means that there is either no user with this email, or password doesn't match |
422:
This error also might be sent by laravel if your body is corrupted description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
/api/users/reset: /api/users/reset:
post: post:
tags: tags:
@ -112,6 +118,12 @@ paths:
200: 200:
description: |- description: |-
The password is reset The password is reset
422:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
/api/users/private/list: /api/users/private/list:
get: get:
@ -147,8 +159,12 @@ paths:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/User' $ref: '#/components/schemas/User'
400: 422:
description: Invalid ID description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
404: 404:
description: User not found description: User not found
/api/users/private/edit/{id}: /api/users/private/edit/{id}:
@ -166,6 +182,12 @@ paths:
responses: responses:
200: 200:
description: OK description: OK
422:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
requestBody: requestBody:
description: |- description: |-
All fields of `user` are required. The whole record will be updated with exactly what you provide here. It is assumed that you already have all information about the user beforehand All fields of `user` are required. The whole record will be updated with exactly what you provide here. It is assumed that you already have all information about the user beforehand
@ -209,6 +231,12 @@ paths:
items: items:
type: string type: string
example: 'userid' example: 'userid'
422:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
delete: delete:
summary: Remove user(s) from trash summary: Remove user(s) from trash
tags: tags:
@ -237,6 +265,12 @@ paths:
items: items:
type: string type: string
example: 'userid' example: 'userid'
422:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
/api/users/private/trash/clean: /api/users/private/trash/clean:
delete: delete:
summary: Delete user(s) for good from trash summary: Delete user(s) for good from trash
@ -262,6 +296,12 @@ paths:
type: array type: array
items: items:
type: string type: string
422:
description: Validation error
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
/api/users/private/trash/list: /api/users/private/trash/list:
get: get:
summary: List users in trash summary: List users in trash
@ -319,6 +359,14 @@ components:
password: password:
type: string type: string
example: 'argon2-hash-here' example: 'argon2-hash-here'
ValidationError:
type: object
properties:
field_name:
type: array
items:
type: string
example: 'This field is invalid!'
securitySchemes: securitySchemes:
session: session:
type: http type: http