Completed
Push — v3.0.0-dev ( c7f980...54069c )
by Hilmi Erdem
28:15
created

OtpController::store()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
ccs 0
cts 30
cp 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * @copyright 2018 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\TemporaryAccess\Http\Controllers;
9
10
use Illuminate\Http\Request;
11
use Illuminate\Contracts\View\View;
12
use App\Http\Controllers\Controller;
13
use Illuminate\Validation\Validator;
14
use Illuminate\Http\RedirectResponse;
15
use Erdemkeren\TemporaryAccess\TokenInterface;
16
use Illuminate\Contracts\Auth\Authenticatable;
17
use Illuminate\Support\Facades\Validator as ValidatorFacade;
18
use Erdemkeren\TemporaryAccess\TemporaryAccessFacade as TemporaryAccess;
19
20
/**
21
 * Class OtpController.
22
 */
23
class OtpController extends Controller
24
{
25
    /**
26
     * * Show the form for the otp submission.
27
     *
28
     * @param Request $request
29
     *
30
     * @return RedirectResponse|View
31
     */
32
    public function create(Request $request)
33
    {
34
        if (! session('otp_requested', false)) {
35
            return redirect('/');
36
        }
37
38
        return view('otp.create');
39
    }
40
41
    /**
42
     * Store the otp in cookies and redirect user
43
     * to their original path.
44
     *
45
     * @param Request $request
46
     *
47
     * @return RedirectResponse
48
     */
49
    public function store(Request $request): RedirectResponse
50
    {
51
        $validator = $this->getOtpSubmissionRequestValidator($request);
52
53
        if ($validator->fails()) {
54
            return redirect()->back()->withErrors($validator);
55
        }
56
57
        if (! $token = $this->retrieveOtpTokenByPlainText(
58
            $request->user(),
59
            $request->input('password')
60
        )) {
61
            $validator->getMessageBag()->add(
62
                'password',
63
                'The password is not valid.'
64
            );
65
66
            return redirect()->back()->withErrors($validator);
67
        }
68
69
        if ($token->expired()) {
70
            $validator->getMessageBag()->add(
71
                'password',
72
                'The password is expired.'
73
            );
74
75
            redirect()->back()->withErrors($validator);
76
        }
77
78
        session()->forget('otp_requested');
79
80
        return redirect()
81
            ->to(session()->pull('otp_redirect_url'))
82
            ->withCookie(
83
                cookie()->make('otp_token', (string) $token, $token->expiryTime() / 60)
84
            );
85
    }
86
87
    /**
88
     * Validate the given otp submission request.
89
     *
90
     * @param Request $request
91
     *
92
     * @return Validator
93
     */
94
    private function getOtpSubmissionRequestValidator(Request $request): Validator
95
    {
96
        return ValidatorFacade::make($request->all(), [
97
            'password' => 'required|string',
98
        ]);
99
    }
100
101
    /**
102
     * Retrieve a token by the given user and password.
103
     *
104
     * @param Authenticatable $user
105
     * @param string          $password
106
     *
107
     * @return mixed
108
     */
109
    private function retrieveOtpTokenByPlainText(Authenticatable $user, string $password): ?TokenInterface
110
    {
111
        return TemporaryAccess::retrieveByPlainText($user, $password);
112
    }
113
}
114