OAuthToken.php 5.26 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\authclient;
9 10 11 12 13 14

use yii\base\Object;

/**
 * Token represents OAuth token.
 *
Qiang Xue committed
15 16 17 18 19 20
 * @property integer $expireDuration Token expiration duration. Note that the type of this property differs in
 * getter and setter. See [[getExpireDuration()]] and [[setExpireDuration()]] for details.
 * @property string $expireDurationParamKey Expire duration param key.
 * @property boolean $isExpired Is token expired. This property is read-only.
 * @property boolean $isValid Is token valid. This property is read-only.
 * @property array $params This property is read-only.
Carsten Brandt committed
21
 * @property string $token Token value.
Qiang Xue committed
22
 * @property string $tokenSecret Token secret value.
23 24 25 26
 *
 * @author Paul Klimov <klimov.paul@gmail.com>
 * @since 2.0
 */
27
class OAuthToken extends Object
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
    /**
     * @var string key in {@link _params} array, which stores token key.
     */
    public $tokenParamKey = 'oauth_token';
    /**
     * @var string key in {@link _params} array, which stores token secret key.
     */
    public $tokenSecretParamKey = 'oauth_token_secret';
    /**
     * @var string key in {@link _params} array, which stores token expiration duration.
     * If not set will attempt to fetch its value automatically.
     */
    private $_expireDurationParamKey;
    /**
     * @var array token parameters.
     */
    private $_params = [];
    /**
     * @var integer object creation timestamp.
     */
    public $createTimestamp;

    public function init()
    {
        if ($this->createTimestamp === null) {
            $this->createTimestamp = time();
        }
    }

    /**
     * @param string $expireDurationParamKey expire duration param key.
     */
    public function setExpireDurationParamKey($expireDurationParamKey)
    {
        $this->_expireDurationParamKey = $expireDurationParamKey;
    }

    /**
     * @return string expire duration param key.
     */
    public function getExpireDurationParamKey()
    {
        if ($this->_expireDurationParamKey === null) {
            $this->_expireDurationParamKey = $this->defaultExpireDurationParamKey();
        }

        return $this->_expireDurationParamKey;
    }

    /**
     * @return array
     */
    public function getParams()
    {
        return $this->_params;
    }

    /**
     * @param array $params
     */
    public function setParams(array $params)
    {
        $this->_params = $params;
    }

    /**
     * Sets param by name.
96 97
     * @param string $name param name.
     * @param mixed $value param value,
98 99 100 101 102 103 104 105
     */
    public function setParam($name, $value)
    {
        $this->_params[$name] = $value;
    }

    /**
     * Returns param by name.
106 107
     * @param string $name param name.
     * @return mixed param value.
108 109 110 111 112 113 114 115
     */
    public function getParam($name)
    {
        return isset($this->_params[$name]) ? $this->_params[$name] : null;
    }

    /**
     * Sets token value.
116
     * @param string $token token value.
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
     * @return static self reference.
     */
    public function setToken($token)
    {
        $this->setParam($this->tokenParamKey, $token);
    }

    /**
     * Returns token value.
     * @return string token value.
     */
    public function getToken()
    {
        return $this->getParam($this->tokenParamKey);
    }

    /**
     * Sets the token secret value.
     * @param string $tokenSecret token secret.
     */
    public function setTokenSecret($tokenSecret)
    {
        $this->setParam($this->tokenSecretParamKey, $tokenSecret);
    }

    /**
     * Returns the token secret value.
     * @return string token secret value.
     */
    public function getTokenSecret()
    {
        return $this->getParam($this->tokenSecretParamKey);
    }

    /**
     * Sets token expire duration.
     * @param string $expireDuration token expiration duration.
     */
    public function setExpireDuration($expireDuration)
    {
        $this->setParam($this->getExpireDurationParamKey(), $expireDuration);
    }

    /**
     * Returns the token expiration duration.
     * @return integer token expiration duration.
     */
    public function getExpireDuration()
    {
        return $this->getParam($this->getExpireDurationParamKey());
    }

    /**
     * Fetches default expire duration param key.
     * @return string expire duration param key.
     */
    protected function defaultExpireDurationParamKey()
    {
        $expireDurationParamKey = 'expires_in';
        foreach ($this->getParams() as $name => $value) {
            if (strpos($name, 'expir') !== false) {
                $expireDurationParamKey = $name;
                break;
            }
        }

        return $expireDurationParamKey;
    }

    /**
     * Checks if token has expired.
     * @return boolean is token expired.
     */
    public function getIsExpired()
    {
        $expirationDuration = $this->getExpireDuration();
        if (empty($expirationDuration)) {
            return false;
        }

        return (time() >= ($this->createTimestamp + $expirationDuration));
    }

    /**
     * Checks if token is valid.
     * @return boolean is token valid.
     */
    public function getIsValid()
    {
        $token = $this->getToken();

        return (!empty($token) && !$this->getIsExpired());
    }
AlexGx committed
210
}