Standard Token object for the AlternC API.
More...
Standard Token object for the AlternC API.
Definition at line 7 of file Token.php.
◆ __construct()
Alternc_Api_Token::__construct |
( |
|
$options = array() | ) |
|
initialize a token object
- Parameters
-
options | any of the public above may contain a dbAdapter, in that case create() will be available |
Definition at line 48 of file Token.php.
50 if (isset($options[
"uid"]) && is_int($options[
"uid"]))
51 $this->uid = $options[
"uid"];
53 if (isset($options[
"isAdmin"]) && is_bool($options[
"isAdmin"]))
54 $this->isAdmin = $options[
"isAdmin"];
◆ toJson()
Alternc_Api_Token::toJson |
( |
| ) |
|
Formats response to json.
- Returns
- string
Definition at line 62 of file Token.php.
64 array(
"uid" => $this->uid,
65 "isAdmin" => $this->isAdmin,
66 "token" => $this->token)
◆ tokenGenerate()
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.
76 if (!(
$db instanceof PDO)) {
77 throw new \Exception(
"No DB Object, can't create", self::ERR_DATABASE_ERROR);
79 if (!isset($options[
"uid"]) || !isset($options[
"isAdmin"])) {
80 throw new \Exception(
"Missing Arguments (uid,isAdmin)", self::ERR_MISSING_ARGUMENT);
87 $stmt =
$db->prepare(
"INSERT IGNORE INTO token SET token=?, expire=DATE_ADD(NOW(), INTERVAL ? SECOND), data=?");
89 $rows = $stmt->rowCount();
Standard Token object for the AlternC API.
References $db, and $token.
Referenced by Alternc_Api_Auth_Login\auth(), and Alternc_Api_Auth_Sharedsecret\auth().
◆ tokenGet()
static Alternc_Api_Token::tokenGet |
( |
|
$token, |
|
|
|
$db |
|
) |
| |
|
static |
Check and return a token.
- Parameters
-
$token | string a 32-chars token |
$db | PDO a PDO object for token table access |
- Returns
- Alternc_Api_Token object or NULL
Definition at line 102 of file Token.php.
103 if (!(
$db instanceof PDO)) {
104 throw new \Exception(
"No DB Object, can't create", self::ERR_DATABASE_ERROR);
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"));
109 $stmt =
$db->prepare(
"SELECT * FROM token WHERE token=?");
110 $stmt->execute(array(
$token));
111 if ($tok = $stmt->fetch(PDO::FETCH_OBJ)) {
114 return new Alternc_Api_Response(array(
"code" => self::ERR_INVALID_TOKEN,
"message" =>
"Invalid token"));
Standard Response object for the AlternC API.
References $db, and $token.
Referenced by Alternc_Api_Service\call().
◆ tokenRandom()
Alternc_Api_Token::tokenRandom |
( |
| ) |
|
Generate a new random token.
- Returns
- string
Definition at line 121 of file Token.php.
122 $chars =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
124 for (
$i = 0;
$i < 32;
$i++)
125 $s.=substr($chars, mt_rand(0, 61), 1);
References $i.
◆ $isAdmin
Alternc_Api_Token::$isAdmin |
◆ $token
Alternc_Api_Token::$token |
◆ $tokenDuration
Alternc_Api_Token::$tokenDuration = 2678400 |
◆ $uid
◆ ERR_DATABASE_ERROR
const Alternc_Api_Token::ERR_DATABASE_ERROR = 112001 |
◆ ERR_INVALID_ARGUMENT
const Alternc_Api_Token::ERR_INVALID_ARGUMENT = 112002 |
◆ ERR_INVALID_TOKEN
const Alternc_Api_Token::ERR_INVALID_TOKEN = 112004 |
◆ ERR_MISSING_ARGUMENT
const Alternc_Api_Token::ERR_MISSING_ARGUMENT = 112003 |
The documentation for this class was generated from the following file: