public function refreshToken(Oauth_TokenModel $model) { $token = $model->getToken(); if (is_object($token)) { $provider = craft()->oauth->getProvider($model->providerHandle); $provider->source->setToken($token); $token = $provider->source->retrieveAccessToken(); $time = time(); // $time = time() + 3590; // google ttl // $time = time() + 50400005089; // facebook ttl if ($time > $token->getEndOfLife()) { // refresh token if (method_exists($provider->source->service, 'refreshAccessToken')) { if ($token->getRefreshToken()) { // generate new token $newToken = $provider->source->service->refreshAccessToken($token); // keep our refresh token as it always remains valid $refreshToken = $token->getRefreshToken(); $newToken->setRefreshToken($refreshToken); // make new token current $model->encodedToken = $this->encodeToken($newToken); return true; } else { // if($provider->class == 'google') // { // $accessToken = $token->getAccessToken(); // $client = new Client(); // try { // $response = $client->get('https://accounts.google.com/o/oauth2/revoke?token='.$accessToken)->send(); // } // catch(\Exception $e) // { // } // // $this->deleteToken($model); // } } } } } return false; }
/** * Get tokens by provider */ public function getTokensByProvider($providerHandle) { $conditions = 'providerHandle=:providerHandle'; $params = array(':providerHandle' => $providerHandle); $records = Oauth_TokenRecord::model()->findAll($conditions, $params); return Oauth_TokenModel::populateModels($records); }
public function getUserToken($handle, $userId = null) { Craft::log(__METHOD__, LogLevel::Info, true); $record = $this->_getTokenRecord($handle, null, $userId); if ($record) { return Oauth_TokenModel::populateModel($record); } return null; }