ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
sspmod_sqlauth_Auth_Source_SQL Class Reference
+ Inheritance diagram for sspmod_sqlauth_Auth_Source_SQL:
+ Collaboration diagram for sspmod_sqlauth_Auth_Source_SQL:

Public Member Functions

 __construct ($info, $config)
 Constructor for this authentication source. More...
 
- Public Member Functions inherited from sspmod_core_Auth_UserPassBase
 __construct ($info, &$config)
 Constructor for this authentication source. More...
 
 setForcedUsername ($forcedUsername)
 Set forced username. More...
 
 getLoginLinks ()
 Return login links from configuration. More...
 
 getRememberUsernameEnabled ()
 Getter for the authsource config option remember.username.enabled. More...
 
 getRememberUsernameChecked ()
 Getter for the authsource config option remember.username.checked. More...
 
 isRememberMeEnabled ()
 Check if the "remember me" feature is enabled. More...
 
 isRememberMeChecked ()
 Check if the "remember me" checkbox should be checked. More...
 
 authenticate (&$state)
 Initialize login. More...
 
- Public Member Functions inherited from SimpleSAML_Auth_Source
 __construct ($info, &$config)
 Constructor for an authentication source. More...
 
 getAuthId ()
 Retrieve the ID of this authentication source. More...
 
 authenticate (&$state)
 Process a request. More...
 
 reauthenticate (array &$state)
 Reauthenticate an user. More...
 
 initLogin ($return, $errorURL=null, array $params=array())
 Start authentication. More...
 
 logout (&$state)
 Log out from this authentication source. More...
 

Protected Member Functions

 login ($username, $password)
 Attempt to log in using the given username and password. More...
 
- Protected Member Functions inherited from sspmod_core_Auth_UserPassBase
 login ($username, $password)
 Attempt to log in using the given username and password. More...
 
- Protected Member Functions inherited from SimpleSAML_Auth_Source
 addLogoutCallback ($assoc, $state)
 Add a logout callback association. More...
 
 callLogoutCallback ($assoc)
 Call a logout callback based on association. More...
 

Private Member Functions

 connect ()
 Create a database connection. More...
 

Private Attributes

 $dsn
 The DSN we should connect to. More...
 
 $username
 The username we should connect to the database with. More...
 
 $password
 The password we should connect to the database with. More...
 
 $query
 The query we should use to retrieve the attributes for the user. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from sspmod_core_Auth_UserPassBase
static handleLogin ($authStateId, $username, $password)
 Handle login request. More...
 
- Static Public Member Functions inherited from SimpleSAML_Auth_Source
static getSourcesOfType ($type)
 Get sources of a specific type. More...
 
static completeAuth (&$state)
 Complete authentication. More...
 
static loginCompleted ($state)
 Called when a login operation has finished. More...
 
static completeLogout (&$state)
 Complete logout. More...
 
static getById ($authId, $type=null)
 Retrieve authentication source. More...
 
static logoutCallback ($state)
 Called when the authentication source receives an external logout request. More...
 
static getSources ()
 Retrieve list of authentication sources. More...
 
- Data Fields inherited from sspmod_core_Auth_UserPassBase
const STAGEID = 'sspmod_core_Auth_UserPassBase.state'
 The string used to identify our states. More...
 
const AUTHID = 'sspmod_core_Auth_UserPassBase.AuthId'
 The key of the AuthId field in the state. More...
 
- Static Protected Member Functions inherited from SimpleSAML_Auth_Source
static validateSource ($source, $id)
 Make sure that the first element of an auth source is its identifier. More...
 
- Protected Attributes inherited from sspmod_core_Auth_UserPassBase
 $loginLinks
 Links to pages from login page. More...
 
 $rememberUsernameEnabled = FALSE
 
 $rememberUsernameChecked = FALSE
 
 $rememberMeEnabled = FALSE
 
 $rememberMeChecked = FALSE
 
- Protected Attributes inherited from SimpleSAML_Auth_Source
 $authId
 

Detailed Description

Definition at line 11 of file SQL.php.

Constructor & Destructor Documentation

◆ __construct()

sspmod_sqlauth_Auth_Source_SQL::__construct (   $info,
  $config 
)

Constructor for this authentication source.

Parameters
array$infoInformation about this authentication source.
array$configConfiguration.

Definition at line 46 of file SQL.php.

References $config, $info, array, and to.

46  {
47  assert('is_array($info)');
48  assert('is_array($config)');
49 
50  // Call the parent constructor first, as required by the interface
51  parent::__construct($info, $config);
52 
53  // Make sure that all required parameters are present.
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);
58  }
59 
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));
65  }
66  }
67 
68  $this->dsn = $config['dsn'];
69  $this->username = $config['username'];
70  $this->password = $config['password'];
71  $this->query = $config['query'];
72  }
File written to
Create styles array
The data for the language used.
$info
Definition: index.php:5

Member Function Documentation

◆ connect()

sspmod_sqlauth_Auth_Source_SQL::connect ( )
private

Create a database connection.

Returns
PDO The database connection.

Definition at line 80 of file SQL.php.

80  {
81  try {
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());
86  }
87 
88  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
89 
90 
91  $driver = explode(':', $this->dsn, 2);
92  $driver = strtolower($driver[0]);
93 
94  /* Driver specific initialization. */
95  switch ($driver) {
96  case 'mysql':
97  /* Use UTF-8. */
98  $db->exec("SET NAMES 'utf8mb4'");
99  break;
100  case 'pgsql':
101  /* Use UTF-8. */
102  $db->exec("SET NAMES 'UTF8'");
103  break;
104  }
105 
106  return $db;
107  }

◆ login()

sspmod_sqlauth_Auth_Source_SQL::login (   $username,
  $password 
)
protected

Attempt to log in using the given username and password.

On a successful login, this function should return the users attributes. On failure, it should throw an exception. If the error was caused by the user entering the wrong username or password, a SimpleSAML_Error_Error('WRONGUSERPASS') should be thrown.

Note that both the username and the password are UTF-8 encoded.

Parameters
string$usernameThe username the user wrote.
string$passwordThe password the user wrote.
Returns
array Associative array with the users attributes.

Definition at line 123 of file SQL.php.

123  {
124  assert('is_string($username)');
125  assert('is_string($password)');
126 
127  $db = $this->connect();
128 
129  try {
130  $sth = $db->prepare($this->query);
131  } catch (PDOException $e) {
132  throw new Exception('sqlauth:' . $this->authId .
133  ': - Failed to prepare query: ' . $e->getMessage());
134  }
135 
136  try {
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());
141  }
142 
143  try {
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());
148  }
149 
150  SimpleSAML\Logger::info('sqlauth:' . $this->authId . ': Got ' . count($data) .
151  ' rows from database');
152 
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');
158  }
159 
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.
163  */
164  $attributes = array();
165  foreach ($data as $row) {
166  foreach ($row as $name => $value) {
167 
168  if ($value === NULL) {
169  continue;
170  }
171 
172  $value = (string)$value;
173 
174  if (!array_key_exists($name, $attributes)) {
175  $attributes[$name] = array();
176  }
177 
178  if (in_array($value, $attributes[$name], TRUE)) {
179  /* Value already exists in attribute. */
180  continue;
181  }
182 
183  $attributes[$name][] = $value;
184  }
185  }
186 
187  SimpleSAML\Logger::info('sqlauth:' . $this->authId . ': Attributes: ' .
188  implode(',', array_keys($attributes)));
189 
190  return $attributes;
191  }
$username
The username we should connect to the database with.
Definition: SQL.php:23
$password
The password we should connect to the database with.
Definition: SQL.php:29
Add rich text string
connect()
Create a database connection.
Definition: SQL.php:80
$attributes
if($format !==null) $name
Definition: metadata.php:146
static info($string)
Definition: Logger.php:201
foreach($_POST as $key=> $value) $res
static error($string)
Definition: Logger.php:168
Create styles array
The data for the language used.

Field Documentation

◆ $dsn

sspmod_sqlauth_Auth_Source_SQL::$dsn
private

The DSN we should connect to.

Definition at line 17 of file SQL.php.

◆ $password

sspmod_sqlauth_Auth_Source_SQL::$password
private

The password we should connect to the database with.

Definition at line 29 of file SQL.php.

◆ $query

sspmod_sqlauth_Auth_Source_SQL::$query
private

The query we should use to retrieve the attributes for the user.

The username and password will be available as :username and :password.

Definition at line 37 of file SQL.php.

◆ $username

sspmod_sqlauth_Auth_Source_SQL::$username
private

The username we should connect to the database with.

Definition at line 23 of file SQL.php.


The documentation for this class was generated from the following file: