47 assert(
'is_array($info)');
48 assert(
'is_array($config)');
54 foreach (
array(
'dsn',
'username',
'password',
'query') as $param) {
55 if (!array_key_exists($param,
$config)) {
56 throw new Exception(
'Missing required attribute \'' . $param .
57 '\' for authentication source
' . $this->authId); 60 if (!is_string($config[$param])) { 61 throw new Exception('Expected parameter \
'' . $param .
62 '\' for authentication source
' . $this->authId . 63 ' to be a
string. Instead it was:
' . 64 var_export($config[$param], TRUE)); 68 $this->dsn = $config['dsn
']; 69 $this->username = $config['username
']; 70 $this->password = $config['password
']; 71 $this->query = $config['query
']; 80 private function connect() { 82 $db = new PDO($this->dsn, $this->username, $this->password); 83 } catch (PDOException $e) { 84 throw new Exception('sqlauth:
' . $this->authId . ': - Failed
to connect to \
'' .
85 $this->dsn .
'\':
'. $e->getMessage()); 88 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 91 $driver = explode(':
', $this->dsn, 2); 92 $driver = strtolower($driver[0]); 94 /* Driver specific initialization. */ 98 $db->exec("SET NAMES 'utf8mb4
'"); 102 $db->exec("SET NAMES 'UTF8
'"); 123 protected function login($username, $password) { 127 $db = $this->connect(); 130 $sth = $db->prepare($this->query); 131 } catch (PDOException $e) { 132 throw new Exception('sqlauth:
' . $this->authId . 133 ': - Failed
to prepare query:
' . $e->getMessage()); 137 $res = $sth->execute(array('username
' => $username, 'password
' => $password)); 138 } catch (PDOException $e) { 139 throw new Exception('sqlauth:
' . $this->authId . 140 ': - Failed
to execute query:
' . $e->getMessage()); 144 $data = $sth->fetchAll(PDO::FETCH_ASSOC); 145 } catch (PDOException $e) { 146 throw new Exception('sqlauth:
' . $this->authId . 147 ': - Failed
to fetch result
set:
' . $e->getMessage()); 150 SimpleSAML\Logger::info('sqlauth:
' . $this->authId . ': Got
' . count($data) . 151 ' rows from database
'); 153 if (count($data) === 0) { 154 /* No rows returned - invalid username/password. */ 155 SimpleSAML\Logger::error('sqlauth:
' . $this->authId . 156 ': No
rows in result
set. Probably wrong username/password.
'); 157 throw new SimpleSAML_Error_Error('WRONGUSERPASS
'); 160 /* Extract attributes. We allow the resultset to consist of multiple rows. Attributes 161 * which are present in more than one row will become multivalued. NULL values and 162 * duplicate values will be skipped. All values will be converted to strings. 164 $attributes = array(); 165 foreach ($data as $row) { 166 foreach ($row as $name => $value) { 168 if ($value === NULL) { 172 $value = (string)$value; 174 if (!array_key_exists($name, $attributes)) { 175 $attributes[$name] = array(); 178 if (in_array($value, $attributes[$name], TRUE)) { 179 /* Value already exists in attribute. */ 183 $attributes[$name][] = $value; 187 SimpleSAML\Logger::info('sqlauth:
' . $this->authId . ': Attributes:
' . 188 implode(',
', array_keys($attributes)));
$dsn
The DSN we should connect to.
$username
The username we should connect to the database with.
$password
The password we should connect to the database with.
$query
The query we should use to retrieve the attributes for the user.
connect()
Create a database connection.
Create styles array
The data for the language used.
__construct($info, $config)
Constructor for this authentication source.