|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Oakhope\OAuth2\Client\Token\MiniProgram; |
|
5
|
|
|
|
|
6
|
|
|
class AccessToken extends \League\OAuth2\Client\Token\AccessToken |
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var string |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $sessionKey; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $openId; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $unionId; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $values = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Constructs an access token. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $options An array of options returned by the service provider |
|
32
|
|
|
* in the access token request. The `access_token` option is required. |
|
33
|
|
|
* @throws \InvalidArgumentException if `access_token` is not provided in `$options`. |
|
34
|
|
|
*/ |
|
35
|
9 |
|
public function __construct(array $options = []) |
|
36
|
|
|
{ |
|
37
|
9 |
|
$options['access_token'] = 'session_key'; |
|
38
|
|
|
|
|
39
|
9 |
|
parent::__construct($options); |
|
40
|
|
|
|
|
41
|
9 |
|
if (empty($options['session_key'])) { |
|
42
|
|
|
throw new \InvalidArgumentException('Required option not passed: "session_key"'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
$this->sessionKey = $options['session_key']; |
|
46
|
|
|
|
|
47
|
9 |
|
if (!empty($options['openid'])) { |
|
48
|
9 |
|
$this->openId = $options['openid']; |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
9 |
|
if (!empty($options['unionid'])) { |
|
52
|
9 |
|
$this->unionId = $options['unionid']; |
|
53
|
6 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Capture any additional values that might exist in the token but are |
|
56
|
|
|
// not part of the standard response. Vendors will sometimes pass |
|
57
|
|
|
// additional user data this way. |
|
58
|
9 |
|
$this->values = array_diff_key($options, array_flip([ |
|
59
|
9 |
|
'session_key', |
|
60
|
6 |
|
'openid', |
|
61
|
|
|
'unionid' |
|
62
|
6 |
|
])); |
|
63
|
9 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns the session key string of this instance. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
6 |
|
public function getSessionKey() |
|
71
|
|
|
{ |
|
72
|
6 |
|
return $this->sessionKey; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Returns the resource owner identifier, if defined. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string|null |
|
79
|
|
|
*/ |
|
80
|
3 |
|
public function getOpenId() |
|
81
|
|
|
{ |
|
82
|
3 |
|
return $this->openId; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Returns the resource owner identifier, if defined. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function getUnionId() |
|
91
|
|
|
{ |
|
92
|
3 |
|
return $this->unionId; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns additional vendor values stored in the token. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getValues() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->values; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the token key. |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function __toString() |
|
111
|
|
|
{ |
|
112
|
|
|
return (string) $this->getSessionKey(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns an array of parameters to serialize when this is serialized with |
|
117
|
|
|
* json_encode(). |
|
118
|
|
|
* |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public function jsonSerialize() |
|
122
|
|
|
{ |
|
123
|
|
|
$parameters = $this->values; |
|
124
|
|
|
|
|
125
|
|
|
if ($this->sessionKey) { |
|
126
|
|
|
$parameters['sessionKey'] = $this->sessionKey; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if ($this->openId) { |
|
130
|
|
|
$parameters['openid'] = $this->openId; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if ($this->unionId) { |
|
134
|
|
|
$parameters['unionid'] = $this->unionId; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return $parameters; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|