/** * @param $scopes * * @return AuthorizationResult */ public function isAuthorized($scopes) { $authorizedResult = new AuthorizationResult(); if (isset($_GET['code'])) { $rawAccessToken = $this->client->authenticate($_GET['code']); if (is_string($rawAccessToken)) { $accessToken = json_decode($rawAccessToken)->access_token; } else { if (is_array($rawAccessToken)) { $accessToken = $rawAccessToken['access_token']; } else { $accessToken = $rawAccessToken->access_token; } } $oAuth = new \Google_Service_Oauth2($this->client); $tokenInfo = $oAuth->tokeninfo(['access_token' => $accessToken]); $authorizedScopes = explode(' ', $tokenInfo->getScope()); $authorizedResult->setAccessToken($rawAccessToken); $authorizedResult->setRequiredScopes($scopes); $authorizedResult->setAuthorizedScopes($authorizedScopes); } return $authorizedResult; }
public function getTokenInfo() { if ($this->tokenInfo === null) { $oauth2 = new \Google_Service_Oauth2($this->client); $json = json_decode($this->client->getAccessToken(), true); try { $this->tokenInfo = $oauth2->tokeninfo(array('access_token' => $json['access_token'])); } catch (\Exception $e) { $this->tokenInfo = array('scope' => ''); } } return $this->tokenInfo; }
/** * @param $id * @param $refreshToken * @return array|void */ public function refreshAccess($id, $refreshToken, $force = false) { $fromcache = true; if ($force) { $this->cache->delete($id . '_token'); } if (!($jsontoken = $this->cache->fetch($id . '_token'))) { try { $this->googleclient->refreshToken($refreshToken); $verify_token = $this->googleclient->verifyIdToken(); if ($verify_token->getUserId() != $id) { return; } $jsontoken = $this->googleclient->getAccessToken(); } catch (\Exception $e) { $this->lastexception = $e; $this->cache->delete($id . '_token'); return; } $fromcache = false; $fulltoken = json_decode($jsontoken, true); if (!$this->cache->save($id . '_token', $jsontoken, $fulltoken["expires_in"] - 60)) { return; } } try { $this->googleclient->setAccessToken($jsontoken); $fulltoken = json_decode($jsontoken, true); $fulltoken["refresh_token"] = $refreshToken; $this->setAdwordsOAuth2Validate($fulltoken); $service = new \Google_Service_Oauth2($this->googleclient); $tokeninfo = $service->tokeninfo(array("access_token" => $fulltoken["access_token"])); } catch (\Exception $e) { $this->lastexception = $e; $this->cache->delete($id . '_token'); return; } return array("accessType" => $tokeninfo->accessType, "audience" => $tokeninfo->audience, "email" => $tokeninfo->email, "expiresIn" => $tokeninfo->expiresIn, "issuedTo" => $tokeninfo->issuedTo, "scope" => $tokeninfo->scope, "userId" => $tokeninfo->userId, "verifiedEmail" => $tokeninfo->verifiedEmail, "fromcache" => $fromcache, "force" => $force); }