3 final class PhabricatorAuthTemporaryToken
extends PhabricatorAuthDAO
4 implements PhabricatorPolicyInterface
{
6 // NOTE: This is usually a PHID, but may be some other kind of resource
7 // identifier for some token types.
8 protected $tokenResource;
10 protected $tokenExpires;
13 protected $properties = array();
15 private $isNew = false;
17 protected function getConfiguration() {
19 self
::CONFIG_TIMESTAMPS
=> false,
20 self
::CONFIG_SERIALIZATION
=> array(
21 'properties' => self
::SERIALIZATION_JSON
,
23 self
::CONFIG_COLUMN_SCHEMA
=> array(
24 'tokenResource' => 'phid',
25 'tokenType' => 'text64',
26 'tokenExpires' => 'epoch',
27 'tokenCode' => 'text64',
28 'userPHID' => 'phid?',
30 self
::CONFIG_KEY_SCHEMA
=> array(
32 'columns' => array('tokenResource', 'tokenType', 'tokenCode'),
35 'key_expires' => array(
36 'columns' => array('tokenExpires'),
39 'columns' => array('userPHID'),
42 ) + parent
::getConfiguration();
45 private function newTokenTypeImplementation() {
46 $types = PhabricatorAuthTemporaryTokenType
::getAllTypes();
48 $type = idx($types, $this->tokenType
);
56 public function getTokenReadableTypeName() {
57 $type = $this->newTokenTypeImplementation();
59 return $type->getTokenReadableTypeName($this);
62 return $this->tokenType
;
65 public function isRevocable() {
66 if ($this->tokenExpires
< time()) {
70 $type = $this->newTokenTypeImplementation();
72 return $type->isTokenRevocable($this);
78 public function revokeToken() {
79 if ($this->isRevocable()) {
80 $this->setTokenExpires(PhabricatorTime
::getNow() - 1)->save();
85 public static function revokeTokens(
86 PhabricatorUser
$viewer,
87 array $token_resources,
90 $tokens = id(new PhabricatorAuthTemporaryTokenQuery())
92 ->withTokenResources($token_resources)
93 ->withTokenTypes($token_types)
97 foreach ($tokens as $token) {
98 $token->revokeToken();
102 public function getTemporaryTokenProperty($key, $default = null) {
103 return idx($this->properties
, $key, $default);
106 public function setTemporaryTokenProperty($key, $value) {
107 $this->properties
[$key] = $value;
111 public function setShouldForceFullSession($force_full) {
112 return $this->setTemporaryTokenProperty('force-full-session', $force_full);
115 public function getShouldForceFullSession() {
116 return $this->getTemporaryTokenProperty('force-full-session', false);
119 public function setIsNewTemporaryToken($is_new) {
120 $this->isNew
= $is_new;
124 public function getIsNewTemporaryToken() {
129 /* -( PhabricatorPolicyInterface )----------------------------------------- */
132 public function getCapabilities() {
134 PhabricatorPolicyCapability
::CAN_VIEW
,
138 public function getPolicy($capability) {
139 // We're just implement this interface to get access to the standard
140 // query infrastructure.
141 return PhabricatorPolicies
::getMostOpenPolicy();
144 public function hasAutomaticCapability($capability, PhabricatorUser
$viewer) {