Completed
Branch newinternal (4dede1)
by Simon
03:45
created

AuthUtility::isCredentialVersionLatest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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;
10
11
class AuthUtility
12
{
13
	/**
14
	 * Test the specified data against the specified credentials
15
	 *
16
	 * @param string $password
17
	 * @param string $credentials
18
	 *
19
	 * @return bool
20
	 */
21 1
	public static function testCredentials($password, $credentials)
22
	{
23 1
		global $minimumPasswordVersion;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
24
25 1
		if (substr($credentials, 0, 1) != ":") {
26 1
			return false;
27
		}
28
29
		// determine password version
30
		$data = explode(':', substr($credentials, 1));
31
32
		// call the encryptVersion function for the version that this password actually is.
33
		// syntax: :1:SALT:HASH
1 ignored issue
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
		// syntax: :2:x:HASH
1 ignored issue
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
35
36
		// check the version is one of the allowed ones:
37
		if ($minimumPasswordVersion > $data[0]) {
38
			return false;
39
		}
40
41
		if ($data[0] == 1) {
42
			return $credentials == self::encryptVersion1($password, $data[1]);
43
		}
44
45
		if ($data[0] == 2) {
46
			return self::verifyVersion2($password, $data[2]);
47
		}
48
49
		return false;
50
	}
51
52
	/**
53
	 * @param string $credentials
54
	 *
55
	 * @return bool
56
	 */
57 1
	public static function isCredentialVersionLatest($credentials)
58
	{
59 1
		return substr($credentials, 0, 3) === ":2:";
60
	}
61
62
	/**
63
	 * Encrypts a user's password with the latest version of the hash algorithm
64
	 *
65
	 * @param string $password
66
	 *
67
	 * @return string
68
	 */
69 1
	public static function encryptPassword($password)
70
	{
71 1
		return self::encryptVersion2($password);
72
	}
73
74
	/**
75
	 * @param string $password
76
	 * @param string $salt
77
	 *
78
	 * @return string
79
	 */
80
	private static function encryptVersion1($password, $salt)
81
	{
82
		return ':1:' . $salt . ':' . md5($salt . '-' . md5($password));
83
	}
84
85
	/**
86
	 * @param string $password
87
	 *
88
	 * @return string
89
	 */
90 1
	private static function encryptVersion2($password)
91
	{
92 1
		return ':2:x:' . password_hash($password, PASSWORD_BCRYPT);
93
	}
94
95
	/**
96
	 * @param string $password
97
	 * @param string $hash
98
	 *
99
	 * @return bool
100
	 */
101
	private static function verifyVersion2($password, $hash)
102
	{
103
		return password_verify($password, $hash);
104
	}
105
}
106