Alternc  latest
Alternc logiel libre pour l'hébergement
Login.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Authentication API used by server to authenticate a user
5  * using his alternc login and password
6  */
8 
9  private $db; // PDO object
10 
11  const ERR_INVALID_ARGUMENT = 1111201;
12 
13  /**
14  * Constructor of the Login Api Auth
15  *
16  * @param $service an Alternc_Api_Service object
17  * @return create the object
18  */
19  function __construct($service) {
20 
21  if (!($service instanceof Alternc_Api_Service))
22  throw new \Exception("Invalid argument (service)", ERR_INVALID_ARGUMENT);
23 
24  $this->db = $service->getDb();
25  }
26 
27  /**
28  * Authenticate a user
29  *
30  * @param $options options, depending on the auth scheme, including uid for setuid users
31  * here, login is the AlternC username, and password is the password for this username.
32  * @return an Alternc_Api_Token
33  */
34  function auth($options) {
35 
36  if (!isset($options["login"]) || !is_string($options["login"])) {
37  throw new \Exception("Missing required parameter login", self::ERR_INVALID_ARGUMENT);
38  }
39  if (!isset($options["password"]) || !is_string($options["password"])) {
40  throw new \Exception("Missing required parameter password", self::ERR_INVALID_ARGUMENT);
41  }
42 
43  if (!preg_match("#^[0-9a-zA-Z-]{1,32}$#", $options["login"])) { // FIXME : normalize this on AlternC !!!
44  throw new \Exception("Invalid login", self::ERR_INVALID_LOGIN);
45  }
46 
47  $stmt = $db->query("SELECT m.enabled,m.uid,m.login,m.su FROM membres m WHERE m.login=? AND m.password=?;", array($options["login"], $options["password"]), PDO::FETCH_CLASS);
48  $me = $stmt->fetch();
49  if (!$me)
50  return new Alternc_Api_Response(array("code" => ERR_INVALID_AUTH, "message" => "Invalid login or password"));
51  if (!$me->enabled)
52  return new Alternc_Api_Response(array("code" => ERR_DISABLED_ACCOUNT, "message" => "Account is disabled"));
53 
55  array("uid" => $me->uid, "isAdmin" => ($me->su != 0)), $this->db
56  );
57  }
58 
59  /**
60  * instructions on how to use this Auth class
61  * @return array("fields" => array("fields to send, required or not"), "description" => "description of this auth")
62  */
63  function instructions() {
64  return array("fields" => array("login" => "AlternC user account", "password" => "AlternC's user password stored in membres table."),
65  "description" => "Authenticate against an AlternC user and password, the same as for the control panel"
66  );
67  }
68 
69 }
70 
71 // class Alternc_Api_Auth_Login
72 
Authentication API used by server to authenticate a user using his alternc login and password.
Definition: Login.php:7
__construct($service)
Constructor of the Login Api Auth.
Definition: Login.php:19
const ERR_INVALID_ARGUMENT
Definition: Login.php:11
instructions()
instructions on how to use this Auth class
Definition: Login.php:63
auth($options)
Authenticate a user.
Definition: Login.php:34
Standard Response object for the AlternC API.
Definition: Response.php:7
Service API used by server to export API methods this class can be used to implement an API service /...
Definition: Service.php:11
static tokenGenerate($options, $db)
Create a new token in the DB for the associated user/admin.
Definition: Token.php:75
Authentication API used by server to authenticate a user using a specific method.
Definition: Interface.php:7