-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
474 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ composer.lock | |
.DS_Store | ||
Thumbs.db | ||
phpunit.xml | ||
.phpunit.result.cache | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
preset: laravel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,75 @@ | ||
# Social Grant for Laravel Passport | ||
|
||
This package is useful to combine your Oauth2 Server with Social Login (facebook, google, github ...). | ||
|
||
If you have a api that accepts registration/login using google, facebook, github or any other social login, | ||
you will be able to exchange the access token given by the social login provider to a `access_token` + `refresh_token` from our own application. | ||
You will be able to resolve a existing user or create a new user if a user is not yet registered on your application. | ||
This package adds a social grant to your Oauth2 Server. | ||
|
||
## Installation | ||
|
||
This package can be installed through Composer. | ||
You can install the package via composer: | ||
|
||
``` | ||
composer require adaojunior/passport-social-grant | ||
``` | ||
|
||
In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file: | ||
|
||
The package will automatically register its service provider. Or you may manually add the service provider in your `config/app.php` file: | ||
|
||
```php | ||
// config/app.php | ||
'providers' => [ | ||
... | ||
Adaojunior\Passport\SocialGrantServiceProvider::class, | ||
... | ||
// ... | ||
Adaojunior\PassportSocialGrant\SocialGrantServiceProvider::class, | ||
]; | ||
``` | ||
|
||
You must also implement `Adaojunior\Passport\SocialUserResolverInterface`: | ||
## Setup | ||
|
||
1. Implement the `SocialGrantUserProvider` interface: | ||
|
||
```php | ||
... | ||
<?php | ||
|
||
use Adaojunior\Passport\SocialGrantException; | ||
use Adaojunior\Passport\SocialUserResolverInterface; | ||
namespace App\SocialGrant; | ||
|
||
class SocialUserResolver implements SocialUserResolverInterface | ||
{ | ||
use Laravel\Socialite\Facades\Socialite; | ||
use Illuminate\Contracts\Auth\Authenticatable; | ||
use League\OAuth2\Server\Entities\ClientEntityInterface; | ||
use Adaojunior\PassportSocialGrant\SocialGrantUserProvider; | ||
|
||
class UserProvider implements SocialGrantUserProvider | ||
{ | ||
/** | ||
* Resolves user by given network and access token. | ||
* | ||
* @param string $network | ||
* @param string $accessToken | ||
* @return \Illuminate\Contracts\Auth\Authenticatable | ||
*/ | ||
public function resolve($network, $accessToken, $accessTokenSecret = null) | ||
{ | ||
switch ($network) { | ||
case 'facebook': | ||
return $this->authWithFacebook($accessToken); | ||
break; | ||
default: | ||
throw SocialGrantException::invalidNetwork(); | ||
break; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Resolves user by facebook access token. | ||
* Retrieve a user by provider and access token. | ||
* | ||
* @param string $provider | ||
* @param string $accessToken | ||
* @return \App\User | ||
* @param ClientEntityInterface $client | ||
* @return Authenticatable|null | ||
*/ | ||
protected function authWithFacebook($accessToken) | ||
public function getUserByAccessToken(string $provider, string $accessToken, ClientEntityInterface $client):? Authenticatable | ||
{ | ||
... | ||
|
||
} | ||
} | ||
|
||
``` | ||
|
||
Register on AppServiceProvider: | ||
2. Bind `SocialGrantUserProvider` interface to your implementation in the `register` method of your application service provider `app/Providers/AppServiceProvider.php`: | ||
|
||
```php | ||
$this->app->singleton(SocialUserResolverInterface::class, SocialUserResolver::class); | ||
$this->app->bind( | ||
Adaojunior\PassportSocialGrant\SocialGrantUserProvider::class, | ||
App\SocialGrant\UserProvider::class | ||
); | ||
``` | ||
|
||
## Usage | ||
|
||
## Request | ||
|
||
```php | ||
$response = $http->post('http://your-app.com/oauth/token', [ | ||
$response = $http->post('http://your.app/oauth/token', [ | ||
'form_params' => [ | ||
'grant_type' => 'social', | ||
'client_id' => 'client-id', | ||
'client_secret' => 'client-secret', | ||
'network' => 'facebook', /// or any other network that your server is able to resolve. | ||
'access_token' => 'A_ACCESS_TOKEN_PROVIDED_BY_THE_SOCIAL_LOGIN_PROVIDER', | ||
'client_id' => $clientId, | ||
'client_secret' => $clientSecret, | ||
'provider' => $providerName, // name of provider (e.g., 'facebook', 'google' etc.) | ||
'access_token' => $providerAccessToken, // access token issued by specified provider | ||
], | ||
]); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
<?php | ||
|
||
namespace Adaojunior\Passport; | ||
namespace Adaojunior\PassportSocialGrant; | ||
|
||
use League\OAuth2\Server\Exception\OAuthServerException; | ||
|
||
class SocialGrantException extends OAuthServerException | ||
{ | ||
public static function invalidNetwork() | ||
public static function invalidProvider() | ||
{ | ||
return self::invalidRequest('network', "Invalid network"); | ||
return self::invalidRequest('provider', 'Invalid provider'); | ||
} | ||
|
||
public static function invalidAccessToken() | ||
{ | ||
return self::invalidRequest('access_token', "Invalid access token"); | ||
return self::invalidRequest('access_token', 'Invalid access token'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.