setAttribute() public method

Sets an optional attribute into the current instance.
public setAttribute ( string $name, mixed $value )
$name string Attribute name.
$value mixed Attribute value.
Exemplo n.º 1
0
 public function testGetAttribute()
 {
     $login = new Login('username', 'password', true);
     $login->setAttribute('aname', 'avalue');
     $login->setAttribute('bname', 'bvalue');
     $this->assertSame('avalue', $login->getAttribute('aname'));
     $this->assertSame('bvalue', $login->getAttribute('bname'));
     $login->setAttribute('aname', 'cvalue');
     $this->assertSame('cvalue', $login->getAttribute('aname'));
     $this->assertNull($login->getAttribute('doesnt exist'));
 }
Exemplo n.º 2
0
 /**
  * This method is triggered on the login submit page where user credentials are submitted.
  * On this page the provider should create a new Login object from those credentials, and return the object.
  * This object will be then validated by user providers.
  *
  * @param ConfigObject $config Firewall config
  *
  * @throws TwitterOAuthException
  * @return Login
  */
 public function getLoginObject(ConfigObject $config)
 {
     try {
         // step1 -> get access token
         if (!$this->httpSession()->get('tw_oauth_token_secret', false)) {
             $requestToken = $this->connection->getRequestToken();
             // save the session for later
             $this->httpSession()->save('tw_oauth_token', $requestToken['oauth_token']);
             $this->httpSession()->save('tw_oauth_token_secret', $requestToken['oauth_token_secret']);
             // check response code
             $authUrl = $this->connection->getAuthorizeUrl($requestToken['oauth_token']);
             header('Location: ' . $authUrl);
             die('Redirect');
         } else {
             // request access tokens from twitter
             if ($this->httpRequest()->query('oauth_verifier', false)) {
                 $access_token = $this->connection->requestAccessToken($this->httpSession()->get('tw_oauth_token'), $this->httpSession()->get('tw_oauth_token_secret'), $this->httpRequest()->query('oauth_token'), $this->httpRequest()->query('oauth_verifier'));
             } else {
                 // remove no longer needed request tokens
                 $this->httpSession()->delete('tw_oauth_token');
                 $this->httpSession()->delete('tw_oauth_token_secret');
                 // redirect back to login
                 $this->httpRedirect($this->httpRequest()->getCurrentUrl());
             }
             // save the access tokens. Normally these would be saved in a database for future use.
             $this->httpSession()->save('tw_access_token', $access_token);
             // remove no longer needed request tokens
             $this->httpSession()->delete('tw_oauth_token');
             $this->httpSession()->delete('tw_oauth_token_secret');
         }
     } catch (\Exception $e) {
         $this->httpSession()->delete('tw_oauth_token_secret');
         throw new TwitterOAuthException($e->getMessage());
     }
     // step2 -> return the login object with auth token
     $login = new Login('', '');
     $login->setAttribute('tw_oauth_server', $this->connection);
     $login->setAttribute('tw_oauth_roles', $this->oauthRoles);
     return $login;
 }
Exemplo n.º 3
0
 /**
  * This method is triggered on the login submit page where user credentials are submitted.
  * On this page the provider should create a new Login object from those credentials, and return the object.
  * This object will be then validated by user providers.
  *
  * @param ConfigObject $config Firewall config
  *
  * @throws OAuth2Exception
  * @return Login
  */
 public function getLoginObject(ConfigObject $config)
 {
     // step1 -> get access token
     $oauth2 = $this->getOAuth2Instance();
     if (!$this->httpRequest()->query('code', false)) {
         $this->httpSession()->delete('oauth_token');
         // append state param to make the request more secured
         $state = $this->createOAuth2State();
         $this->httpSession()->save('oauth_state', $state);
         $oauth2->setState($state);
         $oauth2 = $this->getOAuth2Instance();
         $authUrl = $oauth2->getAuthenticationUrl();
         header('Location: ' . $authUrl);
         $this->triggerExit('Redirecting');
     } else {
         if (!$this->httpSession()->get('oauth_token', false)) {
             $accessToken = $oauth2->requestAccessToken();
             $this->httpSession()->save('oauth_token', $accessToken);
         } else {
             $accessToken = $this->httpSession()->get('oauth_token', false);
         }
     }
     // verify oauth state
     $oauthState = $this->httpRequest()->query('state', '');
     $state = $this->httpSession()->get('oauth_state', 'invalid');
     if ($oauthState != $state) {
         throw new OAuth2Exception('The state parameter from OAuth2 response doesn\'t match the users state parameter.');
     }
     $oauth2->setAccessToken($accessToken);
     if ($this->isArray($accessToken) && isset($accessToken['result']['error'])) {
         $this->httpSession()->delete('oauth_token');
         return false;
     }
     // step2 -> return the login object with auth token
     $login = new Login('', '');
     $login->setAttribute('oauth2_server', $oauth2);
     $login->setAttribute('oauth2_roles', $this->oauth2Roles);
     return $login;
 }