forked from xjflyttp/yii2-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonAuth.php
137 lines (117 loc) · 3.83 KB
/
AmazonAuth.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace xj\oauth;
use yii\authclient\OAuth2;
use yii\authclient\OAuthToken;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\HttpException;
/**
* @author xjflyttp <[email protected]>
* @see https://images-na.ssl-images-amazon.com/images/G/01/lwa/dev/docs/website-developer-guide._TTH_.pdf
*/
class AmazonAuth extends OAuth2 implements IAuth
{
public $authUrl = 'https://www.amazon.com/ap/oa';
public $tokenUrl = 'https://api.amazon.com/auth/o2/token';
public $apiBaseUrl = 'https://api.amazon.com';
public $scope = 'profile';
/**
* Composes user authorization URL.
* @param array $params additional auth GET params.
* @return string authorization URL.
*/
public function buildAuthUrl(array $params = [])
{
$defaultParams = [
'client_id' => $this->clientId,
'scope' => $this->scope,
'response_type' => 'code',
'redirect_uri' => $this->getReturnUrl(),
];
if ($this->validateAuthState) {
$authState = $this->generateAuthState();
$this->setState('authState', $authState);
$defaultParams['state'] = $authState;
}
return $this->composeUrl($this->authUrl, array_merge($defaultParams, $params));
}
/**
* Fetches access token from authorization code.
* @param string $authCode authorization code, usually comes at $_GET['code'].
* @param array $params additional request params.
* @return OAuthToken access token.
* @throws HttpException on invalid auth state in case [[enableStateValidation]] is enabled.
*/
public function fetchAccessToken($authCode, array $params = [])
{
if ($this->validateAuthState) {
$authState = $this->getState('authState');
if (!isset($_REQUEST['state']) || empty($authState) || strcmp($_REQUEST['state'], $authState) !== 0) {
throw new HttpException(400, 'Invalid auth state parameter.');
} else {
$this->removeState('authState');
}
}
$defaultParams = [
'grant_type' => 'authorization_code',
'code' => $authCode,
'redirect_uri' => $this->getReturnUrl(),
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
];
$request = $this->createRequest()
->setMethod('POST')
->addHeaders(['Content-Type' => 'application/x-www--urlencoded;charset=UTF-8'])
->setUrl($this->tokenUrl)
->setData(array_merge($defaultParams, $params));
$response = $this->sendRequest($request);
$token = $this->createToken(['params' => $response]);
$this->setAccessToken($token);
return $token;
}
/**
* Clean ReturnUrl scope param
* @return string
*/
public function getReturnUrl()
{
$returnUrl = parent::getReturnUrl();
$scope = "scope=" . urlencode($this->scope);
$returnUrl = str_replace(["&{$scope}", "{$scope}&", "?{$scope}"], '', $returnUrl);
return $returnUrl;
}
/**
*
* @return array
* @see http://open.weibo.com/wiki/Oauth2/get_token_info
* @see http://open.weibo.com/wiki/2/users/show
*/
protected function initUserAttributes()
{
return $this->api('/auth/O2/tokeninfo');
}
/**
* get UserInfo
* @return array
*/
public function getUserInfo()
{
return $this->api("/user/profile");
}
/**
* @return string
*/
public function getOpenid()
{
$attributes = $this->getUserAttributes();
return ArrayHelper::getValue($attributes, 'user_id');
}
protected function defaultName()
{
return 'Amazon';
}
protected function defaultTitle()
{
return 'Amazon';
}
}