PasswordVerify1Compat   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 32
c 1
b 0
f 0
dl 0
loc 68
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 60 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\sqlauth\Auth\Source;
6
7
use SimpleSAML\Logger;
8
9
/**
10
 * @deprecated Use the SQL2 class and the new SQL2 configuration format instead.
11
 *
12
 * @package SimpleSAMLphp
13
 */
14
15
class PasswordVerify1Compat extends SQL2
16
{
17
    /**
18
     * Constructor for this authentication source.
19
     *
20
     * @param array $info  Information about this authentication source.
21
     * @param array $config  Configuration.
22
     */
23
    public function __construct(array $info, array $config)
24
    {
25
         Logger::warning(
26
             'The sqlauth:PasswordVerify and sqlauth:PasswordVerify1Compat authentication sources are deprecated. ' .
27
             'Please migrate to sqlauth:SQL2 with the new configuration format.',
28
         );
29
30
       /* Transform PasswordVerify (version 1) config to SQL2 config
31
         * Version 1 supported only one database, but multiple queries. The first query was defined
32
         * to be the "authentication query", all subsequent queries were "attribute queries".
33
         */
34
        $v2config = [
35
            'sqlauth:SQL2',
36
            'databases' => [
37
                'default' => [
38
                    'dsn' => $config['dsn'],
39
                    'username' => $config['username'],
40
                    'password' => $config['password'],
41
                ],
42
            ],
43
44
            'auth_queries' => [
45
                'default' => [
46
                    'database' => 'default',
47
                    'query' => is_array($config['query']) ? $config['query'][0] : $config['query'],
48
                    'password_verify_hash_column' => 'passwordhash',
49
                ],
50
            ],
51
        ];
52
53
        if (array_key_exists('username_regex', $config)) {
54
            $v2config['auth_queries']['default']['username_regex'] = $config['username_regex'];
55
        }
56
57
        // Override the default passwordhash column if configured
58
        if (array_key_exists('passwordhash_column', $config)) {
59
            $v2config['auth_queries']['default']['password_verify_hash_column'] = $config['passwordhash_column'];
60
        }
61
62
        $numQueries = is_array($config['query']) ? count($config['query']) : 0;
63
        if ($numQueries > 1) {
64
            $v2config['attr_queries'] = [];
65
            for ($i = 1; $i < $numQueries; $i++) {
66
                $v2config['attr_queries']['query' . $i] = [
67
                    'database' => 'default',
68
                    'query' => $config['query'][$i],
69
                ];
70
            }
71
        }
72
73
        // Copy other config keys that are not specific to SQL1 (eg. core:login_links)
74
        foreach (array_keys($config) as $key) {
75
            if (in_array($key, ['dsn', 'username', 'password', 'query', 'username_regex', 'passwordhashcolumn'])) {
76
                continue;
77
            }
78
79
            $v2config[$key] = $config[$key];
80
        }
81
82
        parent::__construct($info, $v2config);
83
    }
84
}
85