Skip to content

Commit 2286d34

Browse files
Implement sqlauth v2 - support multiple databases, multiple authentication queries, attribute queries selectable based on which authentication query was successful
1 parent f50130b commit 2286d34

10 files changed

Lines changed: 1590 additions & 12 deletions

File tree

src/Auth/Source/SQL1Compat.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Module\sqlauth\Auth\Source;
6+
7+
/**
8+
* Simple SQL authentication source
9+
*
10+
* This class is an example authentication source which authenticates an user
11+
* against a SQL database.
12+
*
13+
* @package SimpleSAMLphp
14+
*/
15+
16+
class SQL1Compat extends SQL2
17+
{
18+
/**
19+
* Constructor for this authentication source.
20+
*
21+
* @param array $info Information about this authentication source.
22+
* @param array $config Configuration.
23+
*/
24+
public function __construct(array $info, array $config)
25+
{
26+
/* Transform SQL (version 1) config to SQL2 config
27+
* Version 1 supported only one database, but multiple queries. The first query was defined
28+
* to be the "authentication query", all subsequent queries were "attribute queries".
29+
*/
30+
$v2config = [
31+
'sqlauth:SQL2',
32+
'databases' => [
33+
'default' => [
34+
'dsn' => $config['dsn'],
35+
'username' => $config['username'],
36+
'password' => $config['password'],
37+
]
38+
],
39+
40+
'auth_queries' => [
41+
'default' => [
42+
'database' => 'default',
43+
'query' => is_array($config['query']) ? $config['query'][0] : $config['query'],
44+
]
45+
],
46+
];
47+
48+
if (array_key_exists('username_regex', $config)) {
49+
$v2config['auth_queries']['default']['username_regex'] = $config['username_regex'];
50+
}
51+
52+
if (is_array($config['query']) && count($config['query']) > 1) {
53+
$v2config['attr_queries'] = [];
54+
for ($i = 1; $i < count($config['query']); $i++) {
55+
$v2config['attr_queries']['query' . $i] = [
56+
'database' => 'default',
57+
'query' => $config['query'][$i],
58+
];
59+
}
60+
}
61+
62+
parent::__construct($info, $v2config);
63+
}
64+
}

0 commit comments

Comments
 (0)