Test Failed
Push — master ( 817d84...d52e3d )
by Raffael
05:44
created

MultiFactorAuth::preUpdateUser()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Idp\Hook;
13
14
use Balloon\App\Idp\Exception;
15
use Balloon\Hook\AbstractHook;
16
use Balloon\Server\RoleInterface;
17
use Balloon\Server\User;
18
use Dolondro\GoogleAuthenticator\Secret;
19
use Dolondro\GoogleAuthenticator\SecretFactory;
20
use Micro\Auth\Adapter\Basic\BasicInterface;
21
use Micro\Auth\Identity;
22
use Psr\Log\LoggerInterface;
23
24
class MultiFactorAuth extends AbstractHook
25
{
26
    /**
27
     * Issuer.
28
     */
29
    public const ISSUER = 'balloon';
30
31
    /**
32
     * Logger.
33
     *
34
     * @var LoggerInterface
35
     */
36
    protected $logger;
37
38
    /**
39
     * Secret factory.
40
     *
41
     * @var SecretFactory
42
     */
43
    protected $secret_factory;
44
45
    /**
46
     * Secret.
47
     *
48
     * @var Secret
49
     */
50
    protected $secret;
51
52
    /**
53
     * Constructor.
54
     */
55
    public function __construct(LoggerInterface $logger, SecretFactory $secret_factory)
56
    {
57
        $this->logger = $logger;
58
        $this->secret_factory = $secret_factory;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function preUpdateUser(User $user, array &$attributes = []): void
65
    {
66
        $existing = $user->getAttributes();
0 ignored issues
show
Unused Code introduced by
$existing is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
        if (isset($attributes['multi_factor_auth']) && $attributes['multi_factor_auth'] === true) {
68
            $secret = $this->secret_factory->create(self::ISSUER, $user->getUsername());
69
            $attributes['google_auth_secret'] = $secret->getSecretKey();
70
            $this->secret = $secret;
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function preDecorateRole(RoleInterface $role, array &$attributes = []): void
78
    {
79
        $mfa = $role->getAttributes()['multi_factor_auth'];
80
        $attributes['multi_factor_auth'] = $mfa;
81
82
        if ($this->secret !== null) {
83
            $attributes['multi_factor_uri'] = $this->secret->getUri();
84
        }
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function preServerIdentity(Identity $identity, ?User &$user): void
91
    {
92
        if (null === $user || ('/index.php/api/v2/tokens' === $_SERVER['ORIG_SCRIPT_NAME'] && isset($_POST['grant_type']) && $_POST['grant_type'] === 'password_mfa')) {
93
            return;
94
        }
95
96
        if ($identity->getAdapter() instanceof BasicInterface && $user->getAttributes()['multi_factor_auth'] === true) {
0 ignored issues
show
Bug introduced by
The class Micro\Auth\Adapter\Basic\BasicInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
97
            throw new Exception\MultiFactorAuthenticationRequired('multi-factor authentication required');
98
        }
99
100
        $this->logger->debug('multi-factor authentication is not required for user ['.$user->getId().']', [
101
            'category' => get_class($this),
102
        ]);
103
    }
104
}
105