diff --git a/Client/HttpAdapter/GuzzleHttpAdapter.php b/Client/HttpAdapter/GuzzleHttpAdapter.php index 528704d..8adcc60 100755 --- a/Client/HttpAdapter/GuzzleHttpAdapter.php +++ b/Client/HttpAdapter/GuzzleHttpAdapter.php @@ -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. * @@ -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. * diff --git a/Client/HttpAdapter/HttpAdapterInterface.php b/Client/HttpAdapter/HttpAdapterInterface.php index 292dd01..c4267a0 100755 --- a/Client/HttpAdapter/HttpAdapterInterface.php +++ b/Client/HttpAdapter/HttpAdapterInterface.php @@ -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. * diff --git a/Resources/config/api-services.json b/Resources/config/api-services.json index 1d46e96..3688707 100755 --- a/Resources/config/api-services.json +++ b/Resources/config/api-services.json @@ -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", diff --git a/Security/User/ChateaUserProviderInterface.php b/Security/User/ChateaUserProviderInterface.php index cea5e0a..48d2080 100755 --- a/Security/User/ChateaUserProviderInterface.php +++ b/Security/User/ChateaUserProviderInterface.php @@ -6,4 +6,6 @@ interface ChateaUserProviderInterface extends UserProviderInterface { public function loadUser($username, $password); + + public function loadUserByFacebookId($facebookId); } \ No newline at end of file diff --git a/Security/User/UserProvider.php b/Security/User/UserProvider.php index 4e2f213..7970709 100755 --- a/Security/User/UserProvider.php +++ b/Security/User/UserProvider.php @@ -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; @@ -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. * @@ -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; @@ -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)