Skip to content

Commit

Permalink
Merge pull request #22 from ricom/feature/captcha
Browse files Browse the repository at this point in the history
Captcha
  • Loading branch information
Geist5000 authored Dec 2, 2022
2 parents 0aa5b5d + a4c74cc commit ff77d47
Show file tree
Hide file tree
Showing 8 changed files with 1,551 additions and 923 deletions.
13 changes: 9 additions & 4 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\User;
use App\Policies\UserPolicy;
use App\Rules\EmailBlockList;
use App\Services\CaptchaService;
use App\Services\EmailService;
use App\Services\UserService;
use Exception;
Expand Down Expand Up @@ -57,17 +58,19 @@ public function index(): AnonymousResourceCollection
* @return Response Code 201, wenn ein User erstellt wurde
* @throws ValidationException Wenn die Eingabedaten nicht valide sind
*/
public function store(Request $request, EmailService $emailService, UserService $userService): Response
public function store(Request $request, EmailService $emailService, UserService $userService, CaptchaService $captchaService): Response
{
$validated = Validator::validate($request->all(), [
"username" => ["required", "string", "unique:users"],
"password" => ["required", "string", "min:8", "max:120", "regex:" . UserController::$passwordRegex],
"email" => ["required", "email", new EmailBlockList($emailService), "unique:users,email", "unique:" . EmailVerification::class . ",email"],
"anonymous_id" => ["integer", "exists:users,id"],
"anonymous_id" => ["integer", "exists:users,id"]
], [
"password.regex" => __("passwords.invalid_regex")
]);

$captchaService->checkRequest($request);


if (array_key_exists("anonymous_id", $validated)) {
$u = User::find($validated["anonymous_id"]);
Expand All @@ -92,8 +95,9 @@ public function store(Request $request, EmailService $emailService, UserService
* @return Response Code 201, wenn das erstellten erfolgreich war. Response enthält username und password im Body
* @throws Exception Wenn es ein Problem beim Erstellen des Users gab
*/
public function storeAnonymous(UserService $userService): Response
public function storeAnonymous(Request $request, UserService $userService, CaptchaService $captchaService): Response
{
// $captchaService->checkRequest($request);
$password = md5(microtime());
$u = $userService->createAnonymousUser($password);
$u->save();
Expand Down Expand Up @@ -161,10 +165,11 @@ public function update(Request $request, User $user, EmailService $emailService,
* @throws AuthorizationException
* @throws ValidationException
*/
public function portAnonymousUser(Request $request, EmailService $emailService, UserService $userService)
public function portAnonymousUser(Request $request, EmailService $emailService, UserService $userService, CaptchaService $captchaService)
{
$user = \Auth::user();
$this->authorize("anonport", $user);
$captchaService->checkRequest($request);

$validated = Validator::validate($request->all(), [
"email" => ["email", "unique:users,email"],
Expand Down
23 changes: 23 additions & 0 deletions app/Services/CaptchaService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Services;

use Illuminate\Http\Request;

class CaptchaService
{


public function checkRequest(Request $request, string $keyName = "captcha_key", string $captchaName = "captcha"): void
{
$key = $request->validate([
$keyName => ["required", "string"]
])[$keyName];

$request->validate([
$captchaName => ["required", "captcha_api:" . $key]
]);

}

}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"laravel/framework": "^9.0",
"laravel/passport": "^10.3",
"laravel/tinker": "^2.5",
"pusher/pusher-php-server": "^7.0"
"pusher/pusher-php-server": "^7.0",
"mews/captcha": "^3.2"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.10",
Expand Down
Loading

0 comments on commit ff77d47

Please sign in to comment.