Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chrysweel committed Nov 24, 2014
2 parents 8212d92 + 0943a26 commit 810e8f1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
50 changes: 50 additions & 0 deletions Client/HttpAdapter/GuzzleHttpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public function withClientCredentials()
throw new ApiException();
}
}

/**
* After the client has been authorized for access, they can use a refresh token to get a new access token.
*
Expand Down Expand Up @@ -291,6 +292,55 @@ public function withRefreshToken($refresh_token)
}
}

/**
* After the client has been authorized for access, they can use a refresh token to get a new access token.
*
* @param string $refresh_token The client refresh token that you obtain in first request of credentials.
*
* @return array|string Associative array with client credentials | Message with error in json format
*
* @throws InvalidArgumentException This exception is thrown if any parameter has errors
*
* @throws AuthenticationException This exception is thrown if you do not credentials or you cannot use this method
*
* @example Get client credentials
*
* $authenticationInstande->withRefreshToken('refresh-token-demo');
*
* array("access_token" => access-token-demo,
* "expires_in" => 3600,
* "token_type" => bearer,
* "scope" => password,
* "refresh_token" => refresh-token-demo
* );
*/
public function withFacebookId($facebook_id)
{
if (!is_string($facebook_id) || 0 >= strlen($facebook_id)) {
throw new InvalidArgumentException("facebook_id must be a non-empty string");
}

$command = $this->getCommand('withFacebookId',
array('client_id'=>$this->getClientId(),'client_secret'=>$this->getSecret(),'facebook_id'=>$facebook_id)
);

try{
return $command->execute();
}catch (ServerErrorResponseException $ex){
throw new ApiException();
}catch (BadResponseException $ex){
if($ex->getResponse()->getStatusCode() == 400){
throw new AuthenticationException($ex->getMessage(), 400, $ex);
}else{
throw new ApiException();
}
}catch(ClientErrorResponseException $ex){
throw new AuthenticationException($ex->getMessage(), 400, $ex);
}catch(CurlException $ex){
throw new ApiException();
}
}

/**
* Disable the service credentials as well as the session.
*
Expand Down
24 changes: 24 additions & 0 deletions Client/HttpAdapter/HttpAdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ public function withClientCredentials();
*/
public function withRefreshToken($refresh_token);

/**
* After the client has been authorized for access, they can use a refresh token to get a new access token.
*
* @param string $refresh_token The client refresh token that you obtain in first request of credentials.
*
* @return array|string Associative array with client credentials | Message with error in json format
*
* @throws InvalidArgumentException This exception is thrown if any parameter has errors
*
* @throws AuthenticationException This exception is thrown if you do not credentials or you cannot use this method
*
* @example Get client credentials
*
* $authenticationInstande->withRefreshToken('refresh-token-demo');
*
* array("access_token" => access-token-demo,
* "expires_in" => 3600,
* "token_type" => bearer,
* "scope" => password,
* "refresh_token" => refresh-token-demo
* );
*/
public function withFacebookId($facebook_id);

/**
* Disable the service credentials as well as the session.
*
Expand Down
31 changes: 31 additions & 0 deletions Resources/config/api-services.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,37 @@
}
}
},
"withFacebookId":{
"httpMethod": "POST",
"uri": "oauth/v2/token",
"summary": "Auth client with Facebook id",
"parameters": {
"grant_type":{
"location": "json",
"type": "string",
"default": "facebook_id",
"description": "the grant_type"
},
"client_id":{
"location": "json",
"type": "string",
"required": true,
"description": "the client_id"
},
"client_secret":{
"location": "json",
"type": "string",
"required": true,
"description": "the secret"
},
"facebook_id":{
"location": "json",
"type": "string",
"required": true,
"description": "the facebook id"
}
}
},
"RevokeToken":{
"httpMethod": "DELETE",
"uri": "api/oauth/v2/revoke",
Expand Down
2 changes: 2 additions & 0 deletions Security/User/ChateaUserProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
interface ChateaUserProviderInterface extends UserProviderInterface
{
public function loadUser($username, $password);

public function loadUserByFacebookId($facebookId);
}
23 changes: 21 additions & 2 deletions Security/User/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Ant\Bundle\ChateaSecureBundle\Client\HttpAdapter\AuthenticationException;
use Ant\Bundle\ChateaSecureBundle\Client\HttpAdapter\Exception\ApiException;
use Ant\Bundle\ChateaSecureBundle\Client\HttpAdapter\HttpAdapterInterface;
use Ant\Bundle\ChateaClientBundle\Api\Model\User as ApiUser;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
Expand Down Expand Up @@ -37,6 +38,22 @@ public function loadUser($username, $password)
}
}

public function loadUserByFacebookId($facebookId)
{
if (empty($facebookId)) {
throw new \InvalidArgumentException('The facebookId cannot be empty.');
}

try {
$data = $this->authentication->withFacebookId($facebookId);
return $this->mapJsonToUser($data);
} catch (ApiException $ae) {
throw new BadCredentialsException('Authentication service down');
} catch (AuthenticationException $e) {
throw new UsernameNotFoundException('Incorrect facebookId',30,$e);
}
}

/**
* Loads the user for the given username.
*
Expand Down Expand Up @@ -76,7 +93,9 @@ public function loadUserByUsername($username)
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User){
if($user instanceof ApiUser){
return $this->loadUser($user->getUsername(), $user->getPlainPassword());
}else if (!$user instanceof User){
$ex = new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));

throw $ex;
Expand Down Expand Up @@ -111,7 +130,7 @@ public function refreshUser(UserInterface $user)
*/
public function supportsClass($class)
{
return $class === ' Ant\Bundle\ChateaSecureBundle\Security\User\User';
return $class === 'Ant\Bundle\ChateaSecureBundle\Security\User\User';
}

protected function mapJsonToUser($data)
Expand Down

0 comments on commit 810e8f1

Please sign in to comment.