ForgotYourPasswordService::checkToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace Mouf\Security\Password;
4
5
use Mouf\Security\Password\Api\ForgotYourPasswordDao;
6
use Mouf\Security\Password\Api\TokenNotFoundException;
7
use Mouf\Security\UserService\UserService;
8
use Psr\Http\Message\UriInterface;
9
use Ramsey\Uuid\Uuid;
10
use TheCodingMachine\Mail\Template\SwiftTwigMailTemplate;
11
12
class ForgotYourPasswordService
13
{
14
    /**
15
     * @var ForgotYourPasswordDao
16
     */
17
    private $forgetYourPasswordDao;
18
19
    /**
20
     * @var \Swift_Mailer
21
     */
22
    private $swiftMailer;
23
24
    /**
25
     * @var SwiftTwigMailTemplate
26
     */
27
    private $mailTemplate;
28
29
    /**
30
     * @var UserService
31
     */
32
    private $userService;
33
34
    /**
35
     * ForgotYourPasswordService constructor.
36
     *
37
     * @param ForgotYourPasswordDao $forgetYourPasswordDao
38
     * @param \Swift_Mailer         $swiftMailer
39
     * @param SwiftTwigMailTemplate $mailTemplate
40
     * @param UserService           $userService
41
     */
42
    public function __construct(ForgotYourPasswordDao $forgetYourPasswordDao, \Swift_Mailer $swiftMailer, SwiftTwigMailTemplate $mailTemplate, UserService $userService)
43
    {
44
        $this->forgetYourPasswordDao = $forgetYourPasswordDao;
45
        $this->swiftMailer = $swiftMailer;
46
        $this->mailTemplate = $mailTemplate;
47
        $this->userService = $userService;
48
    }
49
50
    /**
51
     * Generates and sends via mail a token for user whose mail is $email, stores the token in database and returns the token.
52
     * Throws an EmailNotFoundException if the email is not part of the database.
53
     *
54
     * @param string       $email
55
     * @param UriInterface $resetPasswordUrl The URL to reset the password.
56
     *
57
     * @throws \Mouf\Security\Password\Api\EmailNotFoundException
58
     */
59
    public function sendMail(string $email, UriInterface $resetPasswordUrl)
60
    {
61
        // Let's generate a new token
62
        $token = Uuid::uuid4()->toString();
63
64
        // Let's store this new token
65
        $this->forgetYourPasswordDao->setToken($email, $token);
66
67
        $user = $this->forgetYourPasswordDao->getUserByToken($token);
68
69
        $resetPasswordUrl = $resetPasswordUrl->withQuery('token='.urlencode($token));
70
71
        $mail = $this->mailTemplate->renderMail([
72
            'url' => (string) $resetPasswordUrl,
73
            'website' => $resetPasswordUrl->getHost(),
74
            'user' => $user->getLogin(),
75
        ]);
76
77
        $mail->setTo($email);
78
79
        $this->swiftMailer->send($mail);
80
    }
81
82
    /**
83
     * Returns true if a token is valid, false otherwise.
84
     */
85
    /**
86
     * @param string $token
87
     *
88
     * @return bool
89
     */
90
    public function checkToken(string $token) : bool
91
    {
92
        try {
93
            $this->forgetYourPasswordDao->getUserByToken($token);
94
        } catch (TokenNotFoundException $e) {
95
            return false;
96
        }
97
98
        return true;
99
    }
100
101
    /**
102
     * Uses the $token to replace the $password.
103
     * The token will no longer be usable.
104
     * Just after this action, the user is logged (using the UserService).
105
     *
106
     * Throws an TokenNotFoundException if the token is not part of the database.
107
     *
108
     * @param string $token
109
     * @param string $password
110
     *
111
     * @throws TokenNotFoundException
112
     */
113
    public function useToken(string $token, string $password)
114
    {
115
        $user = $this->forgetYourPasswordDao->getUserByToken($token);
116
117
        $this->forgetYourPasswordDao->setPasswordAndDiscardToken($token, $password);
118
119
        $this->userService->loginWithoutPassword($user->getLogin());
120
    }
121
}
122