Skip to content

Commit 65006bd

Browse files
authored
Merge pull request #27 from nathanjrobertson/deprecate_v1
Deprecate Version 1 configuration
2 parents bb5486a + 2f86bb8 commit 65006bd

File tree

5 files changed

+25
-428
lines changed

5 files changed

+25
-428
lines changed

docs/sql.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@ The authentication can be done in one of two ways:
77
- Most commonly, as a part of the SQL query itself (ie. using SQL functions to hash a parameterized password and compare that to a value stored in the database).
88
- Less commonly, just store the hash in the database, retrieve that then compare that hash using PHP's `password_verify()` function to authenticate. This is useful in cases where there is minimal support in the database or to allow the same code to work against many databases without modification. The differences in how this is configured are in a section towards the bottom of this file.
99

10-
There are two different configuration formats supported ("version 1" and "version 2"). Version 1 is simpler, but is more limited in functionality. Version 2 is more powerful and configurable, but a little more verbose. If you wish to authenticate or gather attributes from more than one SQL database, or need more than one SQL query for authentication then you definitely need Version 2.
10+
There are two different configuration formats ("Version 1" and "Version 2"). We highly recommend using the more powerful and configurable Version 2 configuration. Version 1 is now considered deprecated and support for this legacy configuration format will be removed in a future release.
1111

12-
The Version 1 configuration support comes in two flavours (but identical configurations):
12+
If you are starting out you should use the Version 2 (`sqlauth:SQL2`) configuration format.
1313

14-
- `sqlauth:SQL` uses the legacy Version 1 configuration format and code. Eventually the old code will be phased out, and `sqlauth:SQL` will become a synonym for `sqlauth:SQL1Compat`.
15-
- `sqlauth:SQL1Compat` uses the legacy Version 1 configuration, but applies it to the Version 2 code.
16-
17-
If you are starting out we recommend the Version 2 (`sqlauth:SQL2`) configuration format.
14+
If you have existing Version 1 (`sqlauth:SQL` or `sqlauth:SQL1Compat`) configuration, you should migrate to the new Version 2 (`sqlauth:SQL2`) configuration format.
1815

1916
You enable the module in `config/config.php`.
2017

src/Auth/Source/PasswordVerify.php

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44

55
namespace SimpleSAML\Module\sqlauth\Auth\Source;
66

7-
use SimpleSAML\Assert\Assert;
8-
use SimpleSAML\Error;
9-
use SimpleSAML\Logger;
10-
use SimpleSAML\Module\sqlauth\Auth\Source\SQL;
11-
12-
use function array_key_exists;
13-
use function array_keys;
14-
use function count;
15-
use function implode;
16-
use function is_null;
17-
use function password_verify;
18-
use function sprintf;
19-
207
/**
218
* Simple SQL authentication source
229
*
@@ -44,129 +31,6 @@
4431
* @package SimpleSAMLphp
4532
*/
4633

47-
class PasswordVerify extends SQL
34+
class PasswordVerify extends PasswordVerify1Compat
4835
{
49-
/**
50-
* The column in the result set containing the passwordhash.
51-
*/
52-
protected string $passwordhashcolumn = 'passwordhash';
53-
54-
55-
/**
56-
* Constructor for this authentication source.
57-
*
58-
* @param array $info Information about this authentication source.
59-
* @param array $config Configuration.
60-
*/
61-
public function __construct(array $info, array $config)
62-
{
63-
// Call the parent constructor first, as required by the interface
64-
parent::__construct($info, $config);
65-
66-
if (array_key_exists('passwordhashcolumn', $config)) {
67-
$this->passwordhashcolumn = $config['passwordhashcolumn'];
68-
}
69-
}
70-
71-
72-
/**
73-
* Attempt to log in using the given username and password.
74-
*
75-
* On a successful login, this function should return the users attributes. On failure,
76-
* it should throw an exception. If the error was caused by the user entering the wrong
77-
* username or password, a \SimpleSAML\Error\Error('WRONGUSERPASS') should be thrown.
78-
*
79-
* Note that both the username and the password are UTF-8 encoded.
80-
*
81-
* @param string $username The username the user wrote.
82-
* @param string $password The password the user wrote.
83-
* @return array Associative array with the users attributes.
84-
*/
85-
protected function login(string $username, string $password): array
86-
{
87-
$this->verifyUserNameWithRegex($username);
88-
89-
$db = $this->connect();
90-
$params = ['username' => $username];
91-
$attributes = [];
92-
93-
$numQueries = count($this->query);
94-
for ($x = 0; $x < $numQueries; $x++) {
95-
$data = $this->executeQuery($db, $this->query[$x], $params);
96-
97-
Logger::info('sqlauth:' . $this->authId . ': Got ' . count($data) .
98-
' rows from database');
99-
100-
/**
101-
* Sanity check, passwordhash must be in each resulting tuple and must have
102-
* the same value in every tuple.
103-
*
104-
* Note that $pwhash will contain the passwordhash value after this loop.
105-
*/
106-
$pwhash = null;
107-
if ($x === 0) {
108-
if (count($data) === 0) {
109-
// No rows returned - invalid username/password
110-
Logger::error(sprintf(
111-
'sqlauth:%s: No rows in result set. Probably wrong username/password.',
112-
$this->authId,
113-
));
114-
throw new Error\Error('WRONGUSERPASS');
115-
}
116-
117-
foreach ($data as $row) {
118-
if (
119-
!array_key_exists($this->passwordhashcolumn, $row)
120-
|| is_null($row[$this->passwordhashcolumn])
121-
) {
122-
Logger::error(sprintf(
123-
'sqlauth:%s: column `%s` must be in every result tuple.',
124-
$this->authId,
125-
$this->passwordhashcolumn,
126-
));
127-
throw new Error\Error('WRONGUSERPASS');
128-
}
129-
if ($pwhash) {
130-
if ($pwhash != $row[$this->passwordhashcolumn]) {
131-
Logger::error(sprintf(
132-
'sqlauth:%s: column %s must be THE SAME in every result tuple.',
133-
$this->authId,
134-
$this->passwordhashcolumn,
135-
));
136-
throw new Error\Error('WRONGUSERPASS');
137-
}
138-
}
139-
$pwhash = $row[$this->passwordhashcolumn];
140-
}
141-
142-
/**
143-
* This should never happen as the count(data) test above would have already thrown.
144-
* But checking twice doesn't hurt.
145-
*/
146-
Assert::notNull($pwhash);
147-
148-
/**
149-
* VERIFICATION!
150-
* Now to check if the password the user supplied is actually valid
151-
*/
152-
if (!password_verify($password, $pwhash)) {
153-
Logger::error(sprintf(
154-
'sqlauth:%s: password is incorrect.',
155-
$this->authId,
156-
));
157-
throw new Error\Error('WRONGUSERPASS');
158-
}
159-
}
160-
161-
$this->extractAttributes($attributes, $data, [$this->passwordhashcolumn]);
162-
}
163-
164-
Logger::info(sprintf(
165-
'sqlauth:%s: Attributes: %s',
166-
$this->authId,
167-
implode(',', array_keys($attributes)),
168-
));
169-
170-
return $attributes;
171-
}
17236
}

src/Auth/Source/PasswordVerify1Compat.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
namespace SimpleSAML\Module\sqlauth\Auth\Source;
66

7+
use SimpleSAML\Logger;
8+
79
/**
10+
* @deprecated Use the SQL2 class and the new SQL2 configuration format instead.
11+
*
812
* @package SimpleSAMLphp
913
*/
1014

@@ -18,7 +22,12 @@ class PasswordVerify1Compat extends SQL2
1822
*/
1923
public function __construct(array $info, array $config)
2024
{
21-
/* Transform PasswordVerify (version 1) config to SQL2 config
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
2231
* Version 1 supported only one database, but multiple queries. The first query was defined
2332
* to be the "authentication query", all subsequent queries were "attribute queries".
2433
*/

0 commit comments

Comments
 (0)