Example #1
0
 /**
  * Takes an identity provider access token and fetches the user data from that provider.
  * A new User object is created if that user doesn't exist.
  * Access tokens are returned upon successful authentication using one of the SDKs from
  * Facebook, Google, Twitter, etc.
  * <b>Note:</b> Twitter uses OAuth 1 and gives you a token and a token secret.
  * <b>You must concatenate them like this: <code>{oauth_token}:{oauth_token_secret}</code> and
  * use that as the provider access token.</b>
  * @param $provider identity provider, e.g. 'facebook', 'google'...
  * @param $providerToken access token from a provider like Facebook, Google, Twitter
  * @return ParaObject|null a User object or null if something failed
  */
 public function signIn($provider, $providerToken)
 {
     if ($provider != null && $providerToken != null) {
         $credentials = array();
         $credentials["appid"] = $this->accessKey;
         $credentials["provider"] = $provider;
         $credentials["token"] = $providerToken;
         $result = $this->getEntity($this->invokePost(self::JWT_PATH, $credentials));
         if ($result != null && array_key_exists("user", $result) && array_key_exists("jwt", $result)) {
             $jwtData = $result["jwt"];
             $userData = $result["user"];
             $this->tokenKey = $jwtData["access_token"];
             $this->tokenKeyExpires = $jwtData["expires"];
             $this->tokenKeyNextRefresh = $jwtData["refresh"];
             $obj = new ParaObject();
             $obj->setFields($userData);
             return $obj;
         } else {
             $this->clearAccessToken();
         }
     }
     return null;
 }
 public function testValidationConstraints()
 {
     // Validations
     $kittenType = "kitten";
     $constraints = $this->pc->validationConstraints();
     $this->assertFalse(empty($constraints));
     $this->assertTrue(array_key_exists("app", $constraints));
     $this->assertTrue(array_key_exists("user", $constraints));
     $constraint = $this->pc->validationConstraints("app");
     $this->assertFalse(empty($constraint));
     $this->assertTrue(array_key_exists("app", $constraint));
     $this->assertEquals(1, sizeof($constraint));
     $this->pc->addValidationConstraint($kittenType, "paws", Constraint::required());
     $constraint = $this->pc->validationConstraints($kittenType);
     $this->assertTrue(array_key_exists("paws", $constraint[$kittenType]));
     $ct = new ParaObject("felix");
     $ct->setType($kittenType);
     $ct2 = null;
     try {
         // validation fails
         $ct2 = $this->pc->create($ct);
     } catch (\Exception $e) {
     }
     $this->assertNull($ct2);
     $ct->paws = "4";
     $this->assertNotNull($this->pc->create($ct));
     $this->pc->removeValidationConstraint($kittenType, "paws", "required");
     $constraint = $this->pc->validationConstraints($kittenType);
     $this->assertTrue(empty($constraint));
     $this->assertFalse(array_key_exists($kittenType, $constraint));
 }