ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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...
 
 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...
 
 $options
 The options that 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 12 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 47 of file SQL.php.

48 {
49 assert(is_array($info));
50 assert(is_array($config));
51
52 // Call the parent constructor first, as required by the interface
53 parent::__construct($info, $config);
54
55 // Make sure that all required parameters are present.
56 foreach (array('dsn', 'username', 'password', 'query') as $param) {
57 if (!array_key_exists($param, $config)) {
58 throw new Exception('Missing required attribute \'' . $param .
59 '\' for authentication source ' . $this->authId);
60 }
61
62 if (!is_string($config[$param])) {
63 throw new Exception('Expected parameter \'' . $param .
64 '\' for authentication source ' . $this->authId .
65 ' to be a string. Instead it was: ' .
66 var_export($config[$param], true));
67 }
68 }
69
70 $this->dsn = $config['dsn'];
71 $this->username = $config['username'];
72 $this->password = $config['password'];
73 $this->query = $config['query'];
74 if (isset($config['options'])) {
75 $this->options = $config['options'];
76 }
77 }
$config
Definition: bootstrap.php:15
$info
Definition: index.php:5

References $config, and $info.

Member Function Documentation

◆ connect()

sspmod_sqlauth_Auth_Source_SQL::connect ( )
private

Create a database connection.

Returns
PDO The database connection.

Definition at line 85 of file SQL.php.

86 {
87 try {
88 $db = new PDO($this->dsn, $this->username, $this->password, $this->options);
89 } catch (PDOException $e) {
90 throw new Exception('sqlauth:' . $this->authId . ': - Failed to connect to \'' .
91 $this->dsn . '\': '. $e->getMessage());
92 }
93
94 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
95
96 $driver = explode(':', $this->dsn, 2);
97 $driver = strtolower($driver[0]);
98
99 /* Driver specific initialization. */
100 switch ($driver) {
101 case 'mysql':
102 /* Use UTF-8. */
103 $db->exec("SET NAMES 'utf8mb4'");
104 break;
105 case 'pgsql':
106 /* Use UTF-8. */
107 $db->exec("SET NAMES 'UTF8'");
108 break;
109 }
110
111 return $db;
112 }

◆ 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.

Reimplemented from sspmod_core_Auth_UserPassBase.

Definition at line 128 of file SQL.php.

129 {
130 assert(is_string($username));
131 assert(is_string($password));
132
133 $db = $this->connect();
134
135 try {
136 $sth = $db->prepare($this->query);
137 } catch (PDOException $e) {
138 throw new Exception('sqlauth:' . $this->authId .
139 ': - Failed to prepare query: ' . $e->getMessage());
140 }
141
142 try {
143 $sth->execute(array('username' => $username, 'password' => $password));
144 } catch (PDOException $e) {
145 throw new Exception('sqlauth:' . $this->authId .
146 ': - Failed to execute query: ' . $e->getMessage());
147 }
148
149 try {
150 $data = $sth->fetchAll(PDO::FETCH_ASSOC);
151 } catch (PDOException $e) {
152 throw new Exception('sqlauth:' . $this->authId .
153 ': - Failed to fetch result set: ' . $e->getMessage());
154 }
155
156 SimpleSAML\Logger::info('sqlauth:' . $this->authId . ': Got ' . count($data) .
157 ' rows from database');
158
159 if (count($data) === 0) {
160 /* No rows returned - invalid username/password. */
161 SimpleSAML\Logger::error('sqlauth:' . $this->authId .
162 ': No rows in result set. Probably wrong username/password.');
163 throw new SimpleSAML_Error_Error('WRONGUSERPASS');
164 }
165
166 /* Extract attributes. We allow the resultset to consist of multiple rows. Attributes
167 * which are present in more than one row will become multivalued. null values and
168 * duplicate values will be skipped. All values will be converted to strings.
169 */
170 $attributes = array();
171 foreach ($data as $row) {
172 foreach ($row as $name => $value) {
173
174 if ($value === null) {
175 continue;
176 }
177
178 $value = (string)$value;
179
180 if (!array_key_exists($name, $attributes)) {
181 $attributes[$name] = array();
182 }
183
184 if (in_array($value, $attributes[$name], true)) {
185 /* Value already exists in attribute. */
186 continue;
187 }
188
189 $attributes[$name][] = $value;
190 }
191 }
192
193 SimpleSAML\Logger::info('sqlauth:' . $this->authId . ': Attributes: ' .
194 implode(',', array_keys($attributes)));
195
196 return $attributes;
197 }
static info($string)
Definition: Logger.php:199
static error($string)
Definition: Logger.php:166
$password
The password we should connect to the database with.
Definition: SQL.php:27
connect()
Create a database connection.
Definition: SQL.php:85
$username
The username we should connect to the database with.
Definition: SQL.php:22
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
$row
$data
Definition: bench.php:6

Field Documentation

◆ $dsn

sspmod_sqlauth_Auth_Source_SQL::$dsn
private

The DSN we should connect to.

Definition at line 17 of file SQL.php.

◆ $options

sspmod_sqlauth_Auth_Source_SQL::$options
private

The options that we should connect to the database with.

Definition at line 32 of file SQL.php.

◆ $password

sspmod_sqlauth_Auth_Source_SQL::$password
private

The password we should connect to the database with.

Definition at line 27 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 39 of file SQL.php.

◆ $username

sspmod_sqlauth_Auth_Source_SQL::$username
private

The username we should connect to the database with.

Definition at line 22 of file SQL.php.


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