Passed
Push — master ( 23587f...1016f0 )
by Jan
04:38 queued 10s
created

BackupCodeManager::enableBackupCodes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
 */
21
22
namespace App\Services\TFA;
23
24
25
use App\Entity\UserSystem\User;
26
27
/**
28
 * This services offers methods to manage backup codes for two factor authentication
29
 * @package App\Services\TFA
30
 */
31
class BackupCodeManager
32
{
33
    protected $backupCodeGenerator;
34
35
    public function __construct(BackupCodeGenerator $backupCodeGenerator)
36
    {
37
        $this->backupCodeGenerator = $backupCodeGenerator;
38
    }
39
40
    /**
41
     * Enable backup codes for the given user, by generating a set of backup codes.
42
     * If the backup codes were already enabled before, they a
43
     * @param  User  $user
44
     */
45
    public function enableBackupCodes(User $user)
46
    {
47
        if(empty($user->getBackupCodes())) {
48
           $this->regenerateBackupCodes($user);
49
        }
50
    }
51
52
    /**
53
     * Disable (remove) the backup codes when no other 2 factor authentication methods are enabled.
54
     * @param  User  $user
55
     */
56
    public function disableBackupCodesIfUnused(User $user)
57
    {
58
        if($user->isGoogleAuthenticatorEnabled()) {
59
            return;
60
        }
61
62
        $user->setBackupCodes([]);
63
    }
64
65
    /**
66
     * Generates a new set of backup codes for the user. If no backup codes were available before, new ones are
67
     * generated.
68
     * @param  User  $user The user for which the backup codes should be regenerated
69
     */
70
    public function regenerateBackupCodes(User $user)
71
    {
72
        $codes = $this->backupCodeGenerator->generateCodeSet();
73
        $user->setBackupCodes($codes);
74
    }
75
}