1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150:
<?php
namespace Antavo;
use Antavo\SignedToken\SignedToken;
/**
* Manages customer authentication with a signed token.
*/
class CustomerToken extends SignedToken {
/**
* Name of the cookie under which customer token is stored.
*
* @var string
*/
const COOKIE_NAME = '__alc';
/**
* Creates an authenticating token for customer and sets it in cookies.
*
* @param mixed $customer Unique customer ID. Scalar values only.
* @param string $secret API secret.
* @param int $expires_in Time-to-live value for cookie (less than 30 days
* in seconds) or Unix Timestamp of the expiration time. Default is 0 (it
* expires with the session).
* @return bool Returns <tt>TRUE</tt> if cookie is set successfully,
* <tt>FALSE</tt> otherwise.
* @static
*/
public static function auth($customer, $secret, $expires_in = 0) {
$token = new static($secret, $expires_in);
$token->setCustomer($customer);
return $token->setCookie();
}
/**
* Creates token from cookie set previously.
*
* Since {@see setToken()} is invoked during creation, exceptions may have
* thrown from here.
*
* @param string $secret API secret.
* @return self Returns token extracted from cookie or <tt>NULL</tt> if
* cookie not found.
*/
public static function createFromCookie($secret) {
if (isset($_COOKIE[self::COOKIE_NAME])) {
$token = new static($secret);
$token->setToken($_COOKIE[self::COOKIE_NAME]);
return $token;
}
return NULL;
}
/**
* Alias of {@see removeCookie()}.
*
* @static
*/
public static function deauth() {
return static::removeCookie();
}
/**
* Returns base domain for site.
*
* @return string
* @static
* @internal
*/
public static function getCookieDomain() {
static $domain;
if (!isset($domain)) {
$domain = implode('.', array_slice(
explode('.', $domain = getenv('HTTP_HOST')),
preg_match('/\.co\.uk$/', $domain)
? -3
: -2
));
}
return $domain;
}
/**
* Returns unique customer ID from payload.
*
* @return mixed Returns customer ID if set, <tt>NULL</tt> otherwise.
*/
public function getCustomer() {
if (isset($this->payload['customer'])) {
return $this->payload['customer'];
}
return NULL;
}
/**
* Returns expiration time for cookie based on token expiration setting.
*
* @return int
* @internal
*/
public function getCookieExpirationTime() {
if ($this->expires_at > 0) {
return $this->getCalculatedExpirationTime();
}
return 0;
}
/**
* Deletes cookie that identifies customer.
*
* @return bool Returns <tt>TRUE</tt> if cookie unset successfully,
* <tt>FALSE</tt> otherwise.
* @static
*/
public static function removeCookie() {
return setcookie(self::COOKIE_NAME, '', time() - 3600, '/', static::getCookieDomain());
}
/**
* Sets token in cookies.
*
* @return bool Returns <tt>TRUE</tt> if cookie is set successfully,
* <tt>FALSE</tt> otherwise. **Please note the difference from all other
* setter methods!**
*/
public function setCookie() {
return setcookie(
self::COOKIE_NAME,
(string) $this,
$this->getCookieExpirationTime(),
'/',
$this->getCookieDomain()
);
}
/**
* Sets unique customer ID for payload.
*
* @param mixed $customer Unique customer ID. Non-scalar values are
* discarded.
* @return self Object instance for method chaining.
*/
public function setCustomer($customer) {
if (is_scalar($customer)) {
$this->payload['customer'] = $customer;
}
return $this;
}
}