Failed Conditions
Pull Request — oauthcreation (#531)
by Simon
06:20
created

ScratchTokenCredentialProvider::authenticate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Security\CredentialProviders;
10
11
use Base32\Base32;
12
use Waca\DataObjects\User;
13
use Waca\Exceptions\ApplicationLogicException;
14
use Waca\PdoDatabase;
15
use Waca\Security\EncryptionHelper;
16
use Waca\SiteConfiguration;
17
18
class ScratchTokenCredentialProvider extends CredentialProviderBase
19
{
20
    /** @var EncryptionHelper */
21
    private $encryptionHelper;
22
23
    /**
24
     * ScratchTokenCredentialProvider constructor.
25
     *
26
     * @param PdoDatabase       $database
27
     * @param SiteConfiguration $configuration
28
     */
29
    public function __construct(PdoDatabase $database, SiteConfiguration $configuration)
30
    {
31
        parent::__construct($database, $configuration, 'scratch');
32
        $this->encryptionHelper = new EncryptionHelper($configuration);
33
    }
34
35
    /**
36
     * Validates a user-provided credential
37
     *
38
     * @param User   $user The user to test the authentication against
39
     * @param string $data The raw credential data to be validated
40
     *
41
     * @return bool
42
     * @throws ApplicationLogicException
43
     */
44
    public function authenticate(User $user, $data)
45
    {
46
        if (is_array($data)) {
47
            return false;
48
        }
49
50
        $storedData = $this->getCredentialData($user->getId());
51
52
        if ($storedData === null) {
53
            throw new ApplicationLogicException('Credential data not found');
54
        }
55
56
        $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData()));
57
58
        $i = array_search($data, $scratchTokens);
59
60
        if($i === false) {
61
            return false;
62
        }
63
64
        unset($scratchTokens[$i]);
65
66
        $storedData->setData($this->encryptionHelper->encryptData(serialize($scratchTokens)));
67
        $storedData->save();
68
69
        return true;
70
    }
71
72
    /**
73
     * @param User   $user   The user the credential belongs to
74
     * @param int    $factor The factor this credential provides
75
     * @param string $data   Unused.
76
     */
77
    public function setCredential(User $user, $factor, $data)
78
    {
79
        $scratch = array();
80
        for ($i = 0; $i < 5; $i++) {
81
            $scratch[] = Base32::encode(openssl_random_pseudo_bytes(10));
82
        }
83
84
        $storedData = $this->getCredentialData($user->getId(), null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
85
86
        if ($storedData !== null) {
87
            $storedData->delete();
88
        }
89
90
        $storedData = $this->createNewCredential($user);
91
92
        $storedData->setData($this->encryptionHelper->encryptData(serialize($scratch)));
93
        $storedData->setFactor($factor);
94
        $storedData->setVersion(1);
95
        $storedData->setPriority(9);
96
97
        $storedData->save();
98
    }
99
100
    /**
101
     * @param int $userId
102
     *
103
     * @return int
104
     * @throws ApplicationLogicException
105
     */
106 View Code Duplication
    public function getRemaining($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $storedData = $this->getCredentialData($userId);
109
110
        if ($storedData === null) {
111
            return 0;
112
        }
113
114
        $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData()));
115
116
        return count($scratchTokens);
117
    }
118
119
    /**
120
     * @param int $userId
121
     *
122
     * @return int
123
     * @throws ApplicationLogicException
124
     */
125 View Code Duplication
    public function getTokens($userId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $storedData = $this->getCredentialData($userId);
128
129
        if ($storedData === null) {
130
            return 0;
131
        }
132
133
        $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData()));
134
135
        return $scratchTokens;
136
    }
137
}