forked from mapkyca/known-oauth2-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Token.php
62 lines (44 loc) · 1.52 KB
/
Token.php
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
<?php
namespace IdnoPlugins\OAuth2 {
class Token extends \Idno\Common\Entity {
function __construct($token_type = 'grant', $expires_in = 2419200){
parent::__construct();
$this->access_token = hash('sha256', mt_rand() . microtime(true));
$this->refresh_token = hash('sha256', mt_rand() . microtime(true));
$this->expires_in = $expires_in; // Default expires is 1 month, like facebook
$this->token_type = $token_type;
$this->setTitle($this->access_token); // better stub generation, not that it matters
}
/**
* Check whether a token is valid (i.e. not expired) and that an application with the given key exists.
*/
function isValid() {
if (!\IdnoPlugins\OAuth2\Application::getOne(['key' => $this->key])) return false;
return ($this->created + $this->expires_in > time());
}
/**
* Saves changes to this object based on user input
* @return true|false
*/
function saveDataFromInput() {
if (empty($this->_id)) {
$new = true;
} else {
$new = false;
}
$this->setAccess('PUBLIC');
return $this->save();
}
function jsonSerialize() { // Code is only ever serialised as part of something else
$return = [
'access_token' => $this->access_token,
'refresh_token' => $this->refresh_token,
'expires_in' => $this->expires_in,
'token_Type' => $this->token_type
];
if ($this->state) $return['state'] = $this->state;
if ($this->scope) $return['scope'] = $this->scope;
return $return;
}
}
}