Esempio n. 1
0
 public function refresh_token()
 {
     $this->autoRender = false;
     $provider = new Stevenmaguire\OAuth2\Client\Provider\Bitbucket(array('clientId' => Configure::read('OAuth.bitbucket_consumer_key'), 'clientSecret' => Configure::read('OAuth.bitbucket_consumer_secret')));
     $token = $provider->getAccessToken('refresh_token', array('refresh_token' => $this->Session->read('oauth_refresh_token')));
     debug($token);
 }
 public function authenticate(CakeRequest $request, CakeResponse $response)
 {
     $provider = new Stevenmaguire\OAuth2\Client\Provider\Bitbucket(array('clientId' => Configure::read('OAuth.bitbucket_consumer_key'), 'clientSecret' => Configure::read('OAuth.bitbucket_consumer_secret'), 'redirectUri' => Configure::read('OAuth.redirect_uri')));
     $session = new CakeSession();
     if (!isset($request->query['code'])) {
         $response->header('Location', $provider->getAuthorizationUrl());
     } else {
         try {
             $token = $provider->getAccessToken('authorization_code', array('code' => $request->query['code']));
         } catch (Exception $e) {
             return false;
         }
         $resourceOwner = $provider->getResourceOwner($token)->toArray();
         App::uses('User', 'Model');
         $User = new User();
         $data = array('User' => array('account_type' => 'bitbucket', 'username' => $resourceOwner['username'], 'display_name' => $resourceOwner['display_name'], 'bitbucket_uuid' => $resourceOwner['uuid'], 'oauth_access_token' => $token->getToken(), 'oauth_refresh_token' => $token->getRefreshToken(), 'oauth_token_expires_in' => $token->getExpires()));
         $existingUser = $User->find('first', array('conditions' => array('User.bitbucket_uuid' => $resourceOwner['uuid'])));
         if (!$existingUser) {
             $User->create();
         } else {
             $data['User']['id'] = $existingUser['User']['id'];
         }
         $User->save($data);
         return $data['User'];
     }
     return false;
 }
 private function _refreshOAuth2AccessToken($user)
 {
     $provider = new Stevenmaguire\OAuth2\Client\Provider\Bitbucket(array('clientId' => Configure::read('OAuth.bitbucket_consumer_key'), 'clientSecret' => Configure::read('OAuth.bitbucket_consumer_secret')));
     $token = $provider->getAccessToken('refresh_token', array('refresh_token' => $user['oauth_refresh_token']));
     $this->User->save(array('id' => $user['id'], 'oauth_access_token' => $token->getToken(), 'oauth_refresh_token' => $token->getRefreshToken(), 'oauth_token_expires_in' => $token->getExpires()));
 }