Alternc  latest
Alternc logiel libre pour l'hébergement
Sharedsecret.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Authentication API used by server to authenticate a user using a
5  * SHARED SECRET (ApiKey)
6  */
8 
9  private $db; // PDO object
10 
11  const ERR_INVALID_ARGUMENT = 1111801;
12  const ERR_INVALID_SECRET = 1111802;
13  const ERR_INVALID_LOGIN = 1111803;
14  const ERR_DISABLED_ACCOUNT = 1111804;
15  const ERR_INVALID_AUTH = 1111805;
16 
17  /**
18  * Constructor of the Shared Secret Api Auth
19  *
20  * @param $service an Alternc_Api_Service object
21  * @return create the object
22  */
23  function __construct($service) {
24 
25  if (!($service instanceof Alternc_Api_Service))
26  throw new \Exception("Invalid argument (service)", ERR_INVALID_ARGUMENT);
27 
28  $this->db = $service->getDb();
29  }
30 
31  /**
32  * Authenticate a user
33  *
34  * @param $options options, depending on the auth scheme, including uid for setuid users
35  * here, login is the alternc username, and secret is a valid shared secret for this user.
36  * @return an Alternc_Api_Token
37  */
38  function auth($options) {
39 
40  if (!isset($options["login"]) || !is_string($options["login"])) {
41  throw new \Exception("Missing required parameter login", self::ERR_INVALID_ARGUMENT);
42  }
43  if (!isset($options["secret"]) || !is_string($options["secret"])) {
44  throw new \Exception("Missing required parameter secret", self::ERR_INVALID_ARGUMENT);
45  }
46  if (!preg_match("#^[0-9a-zA-Z]{32}$#", $options["secret"])) {
47  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_SECRET, "message" => "Invalid shared secret syntax"));
48  }
49 
50  if (!preg_match("#^[0-9a-zA-Z-]{1,32}$#", $options["login"])) { // FIXME : normalize this on AlternC !!!
51  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_LOGIN, "message" => "Invalid login"));
52  }
53 
54  $stmt = $this->db->prepare("SELECT m.enabled,m.uid,m.login,m.su FROM membres m, sharedsecret s WHERE s.uid=m.uid AND m.login=? AND s.secret=?;");
55  $stmt->execute(array($options["login"], $options["secret"]));
56  $me = $stmt->fetch(PDO::FETCH_OBJ);
57  if (!$me)
58  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_AUTH, "message" => "Invalid shared secret"));
59  if (!$me->enabled)
60  return new Alternc_Api_Response(array("code" => self::ERR_DISABLED_ACCOUNT, "message" => "Account is disabled"));
61 
63  array("uid" => (int) $me->uid, "isAdmin" => ($me->su != 0)), $this->db
64  );
65  }
66 
67  /**
68  * instructions on how to use this Auth class
69  * @return array("fields" => array("fields to send, required or not"), "description" => "description of this auth")
70  */
71  function instructions() {
72  return array("fields" => array("login" => "AlternC user account", "secret" => "API Key, Shared secrets, valid for this account, stored in sharedsecret table."),
73  "description" => "Authenticate against an Api Key, also called SharedSecret. distinct from the account's password, can be plenty and revoked independently"
74  );
75  }
76 
77 }
78 
79 // class Alternc_Api_Auth_Sharedsecret
80 
Authentication API used by server to authenticate a user using a SHARED SECRET (ApiKey)
Definition: Sharedsecret.php:7
__construct($service)
Constructor of the Shared Secret Api Auth.
auth($options)
Authenticate a user.
instructions()
instructions on how to use this Auth class
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