Passed
Push — master ( 3e77af...7c612c )
by Brian
02:49
created

RequireAdminPassword   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 15
dl 0
loc 53
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldConfirmPassword() 0 5 1
A handle() 0 15 3
A __construct() 0 4 1
1
<?php
2
3
namespace App\Modules\Admins\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Contracts\Routing\ResponseFactory;
7
use Illuminate\Contracts\Routing\UrlGenerator;
8
use Illuminate\Http\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
11
/**
12
 * @see \Illuminate\Auth\Middleware\RequirePassword
13
 */
14
class RequireAdminPassword
15
{
16
    /**
17
     * The response factory instance.
18
     *
19
     * @var \Illuminate\Contracts\Routing\ResponseFactory
20
     */
21
    protected $responseFactory;
22
23
    /**
24
     * The URL generator instance.
25
     *
26
     * @var \Illuminate\Contracts\Routing\UrlGenerator
27
     */
28
    protected $urlGenerator;
29
30
    /**
31
     * Create a new middleware instance.
32
     */
33
    public function __construct(ResponseFactory $responseFactory, UrlGenerator $urlGenerator)
34
    {
35
        $this->responseFactory = $responseFactory;
36
        $this->urlGenerator = $urlGenerator;
37
    }
38
39
    /**
40
     * Handle an incoming request.
41
     */
42
    public function handle(Request $request, Closure $next, ?string $redirectToRoute = null): Response
43
    {
44
        if ($this->shouldConfirmPassword($request)) {
45
            if ($request->expectsJson()) {
46
                return $this->responseFactory->json([
47
                    'message' => 'Password confirmation required.',
48
                ], 423);
49
            }
50
51
            return $this->responseFactory->redirectGuest(
52
                $this->urlGenerator->route($redirectToRoute ?? 'admin.password.confirm')
53
            );
54
        }
55
56
        return $next($request);
57
    }
58
59
    /**
60
     * Determine if the confirmation timeout has expired.
61
     */
62
    protected function shouldConfirmPassword(Request $request): bool
63
    {
64
        $confirmedAt = time() - $request->session()->get('admin.auth.password_confirmed_at', 0);
65
66
        return $confirmedAt > config('auth.password_timeout', 10800);
67
    }
68
}
69