xref: /plugin/oauth/vendor/lusitanian/oauth/src/OAuth/Common/Token/AbstractToken.php (revision 98a3611675f696f14f2d12205fb81f7db0cf7b25)
1*98a36116SAndreas Gohr<?php
2*98a36116SAndreas Gohr
3*98a36116SAndreas Gohrnamespace OAuth\Common\Token;
4*98a36116SAndreas Gohr
5*98a36116SAndreas Gohr/**
6*98a36116SAndreas Gohr * Base token implementation for any OAuth version.
7*98a36116SAndreas Gohr */
8*98a36116SAndreas Gohrabstract class AbstractToken implements TokenInterface
9*98a36116SAndreas Gohr{
10*98a36116SAndreas Gohr    /**
11*98a36116SAndreas Gohr     * @var string
12*98a36116SAndreas Gohr     */
13*98a36116SAndreas Gohr    protected $accessToken;
14*98a36116SAndreas Gohr
15*98a36116SAndreas Gohr    /**
16*98a36116SAndreas Gohr     * @var string
17*98a36116SAndreas Gohr     */
18*98a36116SAndreas Gohr    protected $refreshToken;
19*98a36116SAndreas Gohr
20*98a36116SAndreas Gohr    /**
21*98a36116SAndreas Gohr     * @var int
22*98a36116SAndreas Gohr     */
23*98a36116SAndreas Gohr    protected $endOfLife;
24*98a36116SAndreas Gohr
25*98a36116SAndreas Gohr    /**
26*98a36116SAndreas Gohr     * @var array
27*98a36116SAndreas Gohr     */
28*98a36116SAndreas Gohr    protected $extraParams = array();
29*98a36116SAndreas Gohr
30*98a36116SAndreas Gohr    /**
31*98a36116SAndreas Gohr     * @param string $accessToken
32*98a36116SAndreas Gohr     * @param string $refreshToken
33*98a36116SAndreas Gohr     * @param int    $lifetime
34*98a36116SAndreas Gohr     * @param array  $extraParams
35*98a36116SAndreas Gohr     */
36*98a36116SAndreas Gohr    public function __construct($accessToken = null, $refreshToken = null, $lifetime = null, $extraParams = array())
37*98a36116SAndreas Gohr    {
38*98a36116SAndreas Gohr        $this->accessToken = $accessToken;
39*98a36116SAndreas Gohr        $this->refreshToken = $refreshToken;
40*98a36116SAndreas Gohr        $this->setLifetime($lifetime);
41*98a36116SAndreas Gohr        $this->extraParams = $extraParams;
42*98a36116SAndreas Gohr    }
43*98a36116SAndreas Gohr
44*98a36116SAndreas Gohr    /**
45*98a36116SAndreas Gohr     * @return string
46*98a36116SAndreas Gohr     */
47*98a36116SAndreas Gohr    public function getAccessToken()
48*98a36116SAndreas Gohr    {
49*98a36116SAndreas Gohr        return $this->accessToken;
50*98a36116SAndreas Gohr    }
51*98a36116SAndreas Gohr
52*98a36116SAndreas Gohr    /**
53*98a36116SAndreas Gohr     * @return string
54*98a36116SAndreas Gohr     */
55*98a36116SAndreas Gohr    public function getRefreshToken()
56*98a36116SAndreas Gohr    {
57*98a36116SAndreas Gohr        return $this->refreshToken;
58*98a36116SAndreas Gohr    }
59*98a36116SAndreas Gohr
60*98a36116SAndreas Gohr    /**
61*98a36116SAndreas Gohr     * @return int
62*98a36116SAndreas Gohr     */
63*98a36116SAndreas Gohr    public function getEndOfLife()
64*98a36116SAndreas Gohr    {
65*98a36116SAndreas Gohr        return $this->endOfLife;
66*98a36116SAndreas Gohr    }
67*98a36116SAndreas Gohr
68*98a36116SAndreas Gohr    /**
69*98a36116SAndreas Gohr     * @param array $extraParams
70*98a36116SAndreas Gohr     */
71*98a36116SAndreas Gohr    public function setExtraParams(array $extraParams)
72*98a36116SAndreas Gohr    {
73*98a36116SAndreas Gohr        $this->extraParams = $extraParams;
74*98a36116SAndreas Gohr    }
75*98a36116SAndreas Gohr
76*98a36116SAndreas Gohr    /**
77*98a36116SAndreas Gohr     * @return array
78*98a36116SAndreas Gohr     */
79*98a36116SAndreas Gohr    public function getExtraParams()
80*98a36116SAndreas Gohr    {
81*98a36116SAndreas Gohr        return $this->extraParams;
82*98a36116SAndreas Gohr    }
83*98a36116SAndreas Gohr
84*98a36116SAndreas Gohr    /**
85*98a36116SAndreas Gohr     * @param string $accessToken
86*98a36116SAndreas Gohr     */
87*98a36116SAndreas Gohr    public function setAccessToken($accessToken)
88*98a36116SAndreas Gohr    {
89*98a36116SAndreas Gohr        $this->accessToken = $accessToken;
90*98a36116SAndreas Gohr    }
91*98a36116SAndreas Gohr
92*98a36116SAndreas Gohr    /**
93*98a36116SAndreas Gohr     * @param int $endOfLife
94*98a36116SAndreas Gohr     */
95*98a36116SAndreas Gohr    public function setEndOfLife($endOfLife)
96*98a36116SAndreas Gohr    {
97*98a36116SAndreas Gohr        $this->endOfLife = $endOfLife;
98*98a36116SAndreas Gohr    }
99*98a36116SAndreas Gohr
100*98a36116SAndreas Gohr    /**
101*98a36116SAndreas Gohr     * @param int $lifetime
102*98a36116SAndreas Gohr     */
103*98a36116SAndreas Gohr    public function setLifetime($lifetime)
104*98a36116SAndreas Gohr    {
105*98a36116SAndreas Gohr        if (0 === $lifetime || static::EOL_NEVER_EXPIRES === $lifetime) {
106*98a36116SAndreas Gohr            $this->endOfLife = static::EOL_NEVER_EXPIRES;
107*98a36116SAndreas Gohr        } elseif (null !== $lifetime) {
108*98a36116SAndreas Gohr            $this->endOfLife = intval($lifetime) + time();
109*98a36116SAndreas Gohr        } else {
110*98a36116SAndreas Gohr            $this->endOfLife = static::EOL_UNKNOWN;
111*98a36116SAndreas Gohr        }
112*98a36116SAndreas Gohr    }
113*98a36116SAndreas Gohr
114*98a36116SAndreas Gohr    /**
115*98a36116SAndreas Gohr     * @param string $refreshToken
116*98a36116SAndreas Gohr     */
117*98a36116SAndreas Gohr    public function setRefreshToken($refreshToken)
118*98a36116SAndreas Gohr    {
119*98a36116SAndreas Gohr        $this->refreshToken = $refreshToken;
120*98a36116SAndreas Gohr    }
121*98a36116SAndreas Gohr
122*98a36116SAndreas Gohr    public function isExpired()
123*98a36116SAndreas Gohr    {
124*98a36116SAndreas Gohr        return ($this->getEndOfLife() !== TokenInterface::EOL_NEVER_EXPIRES
125*98a36116SAndreas Gohr        && $this->getEndOfLife() !== TokenInterface::EOL_UNKNOWN
126*98a36116SAndreas Gohr        && time() > $this->getEndOfLife());
127*98a36116SAndreas Gohr    }
128*98a36116SAndreas Gohr}
129