1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Amazon Login https://developer.amazon.com/docs/login-with-amazon/web-docs.html |
4
|
|
|
*/ |
5
|
|
|
namespace tinymeng\OAuth2\Gateways; |
6
|
|
|
|
7
|
|
|
use tinymeng\OAuth2\Connector\Gateway; |
8
|
|
|
use tinymeng\OAuth2\Exception\OAuthException; |
9
|
|
|
use tinymeng\OAuth2\Helper\ConstCode; |
10
|
|
|
|
11
|
|
|
class Amazon extends Gateway |
12
|
|
|
{ |
13
|
|
|
const API_BASE = 'https://api.amazon.com/'; |
14
|
|
|
protected $AuthorizeURL = 'https://www.amazon.com/ap/oa'; |
15
|
|
|
protected $AccessTokenURL = 'https://api.amazon.com/auth/o2/token'; |
16
|
|
|
protected $UserInfoURL = 'https://api.amazon.com/user/profile'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* 得到跳转地址 |
20
|
|
|
*/ |
21
|
|
|
public function getRedirectUrl() |
22
|
|
|
{ |
23
|
|
|
$this->saveState(); |
24
|
|
|
$params = [ |
25
|
|
|
'client_id' => $this->config['app_id'], |
26
|
|
|
'redirect_uri' => $this->config['callback'], |
27
|
|
|
'response_type' => $this->config['response_type'], |
28
|
|
|
'scope' => $this->config['scope'] ?: 'profile', |
29
|
|
|
'state' => $this->config['state'], |
30
|
|
|
]; |
31
|
|
|
return $this->AuthorizeURL . '?' . http_build_query($params); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* 获取当前授权用户的openid标识 |
36
|
|
|
*/ |
37
|
|
|
public function openid() |
38
|
|
|
{ |
39
|
|
|
$result = $this->getUserInfo(); |
40
|
|
|
return $result['user_id'] ?? ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 获取格式化后的用户信息 |
45
|
|
|
*/ |
46
|
|
|
public function userInfo() |
47
|
|
|
{ |
48
|
|
|
$result = $this->getUserInfo(); |
49
|
|
|
|
50
|
|
|
$userInfo = [ |
51
|
|
|
'open_id' => $result['user_id'] ?? '', |
52
|
|
|
'union_id' => $result['user_id'] ?? '', |
53
|
|
|
'channel' => ConstCode::TYPE_AMAZON, |
54
|
|
|
'nickname' => $result['name'] ?? '', |
55
|
|
|
'gender' => ConstCode::GENDER, |
56
|
|
|
'avatar' => '', |
57
|
|
|
'access_token' => $this->token['access_token'] ?? '', |
58
|
|
|
'native' => $result |
59
|
|
|
]; |
60
|
|
|
return $userInfo; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 获取原始接口返回的用户信息 |
65
|
|
|
*/ |
66
|
|
|
public function getUserInfo() |
67
|
|
|
{ |
68
|
|
|
$this->getToken(); |
69
|
|
|
|
70
|
|
|
$headers = [ |
71
|
|
|
'Authorization: Bearer ' . $this->token['access_token'], |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
$data = $this->get($this->UserInfoURL, [], $headers); |
75
|
|
|
$data = json_decode($data, true); |
76
|
|
|
|
77
|
|
|
if (!isset($data['user_id'])) { |
78
|
|
|
throw new OAuthException('获取Amazon用户信息失败:' . json_encode($data)); |
79
|
|
|
} |
80
|
|
|
return $data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* 获取access_token |
85
|
|
|
*/ |
86
|
|
|
protected function getToken() |
87
|
|
|
{ |
88
|
|
|
if (empty($this->token)) { |
89
|
|
|
$this->checkState(); |
90
|
|
|
$params = [ |
91
|
|
|
'client_id' => $this->config['app_id'], |
92
|
|
|
'client_secret' => $this->config['app_secret'], |
93
|
|
|
'grant_type' => $this->config['grant_type'], |
94
|
|
|
'code' => isset($_REQUEST['code']) ? $_REQUEST['code'] : '', |
95
|
|
|
'redirect_uri' => $this->config['callback'], |
96
|
|
|
]; |
97
|
|
|
$response = $this->post($this->AccessTokenURL, $params); |
98
|
|
|
$this->token = $this->parseToken($response); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 解析access_token方法请求后的返回值 |
104
|
|
|
*/ |
105
|
|
|
protected function parseToken($token) |
106
|
|
|
{ |
107
|
|
|
$data = json_decode($token, true); |
108
|
|
|
if (isset($data['access_token'])) { |
109
|
|
|
return $data; |
110
|
|
|
} |
111
|
|
|
throw new OAuthException('获取Amazon access_token 出错:' . json_encode($data)); |
112
|
|
|
} |
113
|
|
|
} |