-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathZohoOAuthClient.php
More file actions
174 lines (154 loc) · 6.43 KB
/
Copy pathZohoOAuthClient.php
File metadata and controls
174 lines (154 loc) · 6.43 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
namespace zcrmsdk\oauth;
use zcrmsdk\crm\utility\Logger;
use zcrmsdk\oauth\exception\ZohoOAuthException;
use zcrmsdk\oauth\utility\ZohoOAuthConstants;
use zcrmsdk\oauth\utility\ZohoOAuthHTTPConnector;
use zcrmsdk\oauth\utility\ZohoOAuthTokens;
class ZohoOAuthClient
{
private $zohoOAuthParams;
private static $zohoOAuthClient;
private function __construct($params)
{
$this->zohoOAuthParams = $params;
}
public static function getInstance($params)
{
self::$zohoOAuthClient = new ZohoOAuthClient($params);
return self::$zohoOAuthClient;
}
public static function getInstanceWithOutParam()
{
return self::$zohoOAuthClient;
}
public function getAccessToken($userEmailId)
{
$persistence = ZohoOAuth::getPersistenceHandlerInstance();
try {
$tokens = $persistence->getOAuthTokens($userEmailId);
} catch (ZohoOAuthException $ex) {
Logger::severe("Exception while retrieving tokens from persistence - " . $ex);
throw $ex;
}
try {
return $tokens->getAccessToken();
} catch (ZohoOAuthException $ex) {
Logger::info("Access Token has expired. Hence refreshing.");
$tokens = self::refreshAccessToken($tokens->getRefreshToken(), $userEmailId);
return $tokens->getAccessToken();
}
}
public function generateAccessToken($grantToken)
{
if ($grantToken == null) {
throw new ZohoOAuthException("Grant Token is not provided.");
}
try {
$conn = self::getZohoConnector(ZohoOAuth::getTokenURL());
$conn->addParam(ZohoOAuthConstants::GRANT_TYPE, ZohoOAuthConstants::GRANT_TYPE_AUTH_CODE);
$conn->addParam(ZohoOAuthConstants::CODE, $grantToken);
$resp = $conn->post();
$responseJSON = self::processResponse($resp);
if (array_key_exists(ZohoOAuthConstants::ACCESS_TOKEN, $responseJSON)) {
$tokens = self::getTokensFromJSON($responseJSON);
$tokens->setUserEmailId(self::getUserEmailIdFromIAM($tokens->getAccessToken()));
ZohoOAuth::getPersistenceHandlerInstance()->saveOAuthData($tokens);
return $tokens;
} else {
throw new ZohoOAuthException("Exception while fetching access token from grant token - " . $resp);
}
} catch (ZohoOAuthException $ex) {
throw new ZohoOAuthException($ex);
}
}
public function generateAccessTokenFromRefreshToken($refreshToken, $userEmailId)
{
self::refreshAccessToken($refreshToken, $userEmailId);
}
public function refreshAccessToken($refreshToken, $userEmailId)
{
if ($refreshToken == null) {
throw new ZohoOAuthException("Refresh token is not provided.");
}
try {
$conn = self::getZohoConnector(ZohoOAuth::getRefreshTokenURL());
$conn->addParam(ZohoOAuthConstants::GRANT_TYPE, ZohoOAuthConstants::GRANT_TYPE_REFRESH);
$conn->addParam(ZohoOAuthConstants::REFRESH_TOKEN, $refreshToken);
$response = $conn->post();
$responseJSON = self::processResponse($response);
if (array_key_exists(ZohoOAuthConstants::ACCESS_TOKEN, $responseJSON)) {
$tokens = self::getTokensFromJSON($responseJSON);
$tokens->setRefreshToken($refreshToken);
$tokens->setUserEmailId($userEmailId);
ZohoOAuth::getPersistenceHandlerInstance()->saveOAuthData($tokens);
return $tokens;
} else {
throw new ZohoOAuthException("Exception while fetching access token from refresh token - " . $response);
}
} catch (ZohoOAuthException $ex) {
throw new ZohoOAuthException($ex);
}
}
private function getZohoConnector($url)
{
$zohoHttpCon = new ZohoOAuthHTTPConnector();
$zohoHttpCon->setUrl($url);
$zohoHttpCon->addParam(ZohoOAuthConstants::CLIENT_ID, $this->zohoOAuthParams->getClientId());
$zohoHttpCon->addParam(ZohoOAuthConstants::CLIENT_SECRET, $this->zohoOAuthParams->getClientSecret());
$zohoHttpCon->addParam(ZohoOAuthConstants::REDIRECT_URL, $this->zohoOAuthParams->getRedirectURL());
return $zohoHttpCon;
}
private function getTokensFromJSON($responseObj)
{
$oAuthTokens = new ZohoOAuthTokens();
$expiresIn = $responseObj[ZohoOAuthConstants::EXPIRES_IN];
if(!array_key_exists(ZohoOAuthConstants::EXPIRES_IN_SEC,$responseObj)){
$expiresIn=$expiresIn*1000;
}
$oAuthTokens->setExpiryTime($oAuthTokens->getCurrentTimeInMillis() + $expiresIn);
$accessToken = $responseObj[ZohoOAuthConstants::ACCESS_TOKEN];
$oAuthTokens->setAccessToken($accessToken);
if (array_key_exists(ZohoOAuthConstants::REFRESH_TOKEN, $responseObj)) {
$refreshToken = $responseObj[ZohoOAuthConstants::REFRESH_TOKEN];
$oAuthTokens->setRefreshToken($refreshToken);
}
return $oAuthTokens;
}
/**
* zohoOAuthParams
*
* @return
*/
public function getZohoOAuthParams()
{
return $this->zohoOAuthParams;
}
/**
* zohoOAuthParams
*
* @param $zohoOAuthParams
*/
public function setZohoOAuthParams($zohoOAuthParams)
{
$this->zohoOAuthParams = $zohoOAuthParams;
}
public function getUserEmailIdFromIAM($accessToken)
{
$connector = new ZohoOAuthHTTPConnector();
$connector->setUrl(ZohoOAuth::getUserInfoURL());
$connector->addHeadder(ZohoOAuthConstants::AUTHORIZATION, ZohoOAuthConstants::OAUTH_HEADER_PREFIX . $accessToken);
$apiResponse = $connector->get();
$jsonResponse = self::processResponse($apiResponse);
if(!is_array($jsonResponse) || !array_key_exists("Email", $jsonResponse)){
throw new ZohoOAuthException("Exception while fetching UserID from access token, Make sure AAAserver.profile.Read scope is included while generating the Grant token " . $jsonResponse);
}
return $jsonResponse['Email'];
}
public function processResponse($apiResponse)
{
list ($headers, $content) = explode("\r\n\r\n", $apiResponse, 2);
$jsonResponse = json_decode($content, true);
return $jsonResponse;
}
}