Alternc  latest
Alternc logiel libre pour l'h├ębergement
 All Classes Namespaces Files Functions Variables Pages
Alternc_Api_Token Class Reference

Standard Token object for the AlternC API. More...

Public Member Functions

 __construct ($options=array())
 initialize a token object More...
 
 toJson ()
 Formats response to json. More...
 
 tokenRandom ()
 Generate a new random token. More...
 

Static Public Member Functions

static tokenGenerate ($options, $db)
 Create a new token in the DB for the associated user/admin. More...
 
static tokenGet ($token, $db)
 Check and return a token. More...
 

Public Attributes

const ERR_DATABASE_ERROR = 112001
 
const ERR_INVALID_ARGUMENT = 112002
 
const ERR_MISSING_ARGUMENT = 112003
 
const ERR_INVALID_TOKEN = 112004
 
 $uid
 
 $isAdmin
 
 $token
 
 $tokenDuration = 2678400
 

Detailed Description

Standard Token object for the AlternC API.

Definition at line 7 of file Token.php.

Constructor & Destructor Documentation

Alternc_Api_Token::__construct (   $options = array())

initialize a token object

Parameters
optionsany of the public above may contain a dbAdapter, in that case create() will be available

Definition at line 48 of file Token.php.

48  {
49 
50  if (isset($options["uid"]) && is_int($options["uid"]))
51  $this->uid = $options["uid"];
52 
53  if (isset($options["isAdmin"]) && is_bool($options["isAdmin"]))
54  $this->isAdmin = $options["isAdmin"];
55  }

Member Function Documentation

Alternc_Api_Token::toJson ( )

Formats response to json.

Returns
string

Definition at line 62 of file Token.php.

62  {
63  return json_encode(
64  array("uid" => $this->uid,
65  "isAdmin" => $this->isAdmin,
66  "token" => $this->token)
67  );
68  }
static Alternc_Api_Token::tokenGenerate (   $options,
  $db 
)
static

Create a new token in the DB for the associated user/admin.

Returns
string the token (32 chars)

Definition at line 75 of file Token.php.

References $db, and $token.

Referenced by Alternc_Api_Auth_Login\auth(), and Alternc_Api_Auth_Sharedsecret\auth().

75  {
76  if (!($db instanceof PDO)) {
77  throw new \Exception("No DB Object, can't create", self::ERR_DATABASE_ERROR);
78  }
79  if (!isset($options["uid"]) || !isset($options["isAdmin"])) {
80  throw new \Exception("Missing Arguments (uid,isAdmin)", self::ERR_MISSING_ARGUMENT);
81  }
82 
83  $token = new Alternc_Api_Token($options);
84 
85  do {
86  $token->token = $token->tokenRandom();
87  $stmt = $db->prepare("INSERT IGNORE INTO token SET token=?, expire=DATE_ADD(NOW(), INTERVAL ? SECOND), data=?");
88  $stmt->execute(array($token->token, $token->tokenDuration, $token->toJson()));
89  $rows = $stmt->rowCount();
90  } while ($rows == 0); // prevent collisions
91 
92  return $token;
93  }
global $db
Definition: bootstrap.php:22
Standard Token object for the AlternC API.
Definition: Token.php:7
static Alternc_Api_Token::tokenGet (   $token,
  $db 
)
static

Check and return a token.

Parameters
$tokenstring a 32-chars token
$dbPDO a PDO object for token table access
Returns
Alternc_Api_Token object or NULL

Definition at line 102 of file Token.php.

References $db, and $token.

Referenced by Alternc_Api_Service\call().

102  {
103  if (!($db instanceof PDO)) {
104  throw new \Exception("No DB Object, can't create", self::ERR_DATABASE_ERROR);
105  }
106  if (!is_string($token) || !preg_match("#^[a-zA-Z0-9]{32}$#", $token)) {
107  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_TOKEN, "message" => "Invalid token"));
108  }
109  $stmt = $db->prepare("SELECT * FROM token WHERE token=?");
110  $stmt->execute(array($token));
111  if ($tok = $stmt->fetch(PDO::FETCH_OBJ)) {
112  return new Alternc_Api_Token(json_decode($tok->data, true));
113  }
114  return new Alternc_Api_Response(array("code" => self::ERR_INVALID_TOKEN, "message" => "Invalid token"));
115  }
global $db
Definition: bootstrap.php:22
Standard Token object for the AlternC API.
Definition: Token.php:7
Standard Response object for the AlternC API.
Definition: Response.php:7
Alternc_Api_Token::tokenRandom ( )

Generate a new random token.

Returns
string

Definition at line 121 of file Token.php.

References $i.

121  {
122  $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
123  $s = "";
124  for ($i = 0; $i < 32; $i++)
125  $s.=substr($chars, mt_rand(0, 61), 1);
126  return $s;
127  }
$i

Member Data Documentation

Alternc_Api_Token::$isAdmin

Definition at line 26 of file Token.php.

Alternc_Api_Token::$token

Definition at line 33 of file Token.php.

Referenced by tokenGenerate(), and tokenGet().

Alternc_Api_Token::$tokenDuration = 2678400

Definition at line 40 of file Token.php.

Alternc_Api_Token::$uid

Definition at line 19 of file Token.php.

const Alternc_Api_Token::ERR_DATABASE_ERROR = 112001

Definition at line 9 of file Token.php.

const Alternc_Api_Token::ERR_INVALID_ARGUMENT = 112002

Definition at line 10 of file Token.php.

const Alternc_Api_Token::ERR_INVALID_TOKEN = 112004

Definition at line 12 of file Token.php.

const Alternc_Api_Token::ERR_MISSING_ARGUMENT = 112003

Definition at line 11 of file Token.php.


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