-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DraperStudio
committed
Feb 22, 2015
0 parents
commit dbcda21
Showing
6 changed files
with
222 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Created by https://www.gitignore.io | ||
|
||
### Composer ### | ||
composer.phar | ||
vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 DraperStudio | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,71 @@ | ||
# Google OAuth2 Provider for Laravel Socialite | ||
|
||
<!-- START doctoc generated TOC please keep comment here to allow auto update --> | ||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> | ||
## Contents | ||
|
||
- [Installation](#installation) | ||
- [1. Composer](#1-composer) | ||
- [2. Service Provider](#2-service-provider) | ||
- [3. Add the Event and Listeners](#3-add-the-event-and-listeners) | ||
- [4. Services Array and .env](#4-services-array-and-env) | ||
- [Add to `config/services.php`.](#add-to-configservicesphp) | ||
- [Append provider values to your `.env` file](#append-provider-values-to-your-env-file) | ||
- [Usage](#usage) | ||
|
||
<!-- END doctoc generated TOC please keep comment here to allow auto update --> | ||
|
||
|
||
## Installation | ||
|
||
### 1. Composer | ||
|
||
```bash | ||
// This assumes that you have composer installed globally | ||
composer require socialiteproviders/google | ||
``` | ||
|
||
### 2. Service Provider | ||
|
||
* [See the docs on how to install the `SocialiteProviders` service provider.](https://github.com/SocialiteProviders/Manager#2-service-provider) | ||
|
||
|
||
### 3. Add the Event and Listeners | ||
|
||
* The listener that you will be adding is `'SocialiteProviders\Google\GoogleExtendSocialite@handle',`. | ||
|
||
* [See the docs on how to install](https://github.com/SocialiteProviders/Manager#3-add-the-event-and-listeners) | ||
|
||
### 4. Services Array and .env | ||
|
||
#### Add to `config/services.php`. | ||
|
||
```php | ||
'google' => [ | ||
'client_id' => env('GOOGLE_KEY'), | ||
'client_secret' => env('GOOGLE_SECRET'), | ||
'redirect' => env('GOOGLE_REDIRECT_URI'), | ||
], | ||
``` | ||
|
||
#### Append provider values to your `.env` file | ||
|
||
```php | ||
// other values above | ||
GOOGLE_KEY=yourkeyfortheservice | ||
GOOGLE_SECRET=yoursecretfortheservice | ||
GOOGLE_REDIRECT_URI=https://example.com/login | ||
``` | ||
|
||
* [See the main docs for more information](https://github.com/SocialiteProviders/Manager#4-services-array-and-env) | ||
|
||
|
||
## Usage | ||
|
||
* You should now be able to use it like you would regularly use Socialite: | ||
|
||
```php | ||
return Socialite::with('google')->redirect(); | ||
``` | ||
|
||
* [See the main docs for more information](https://github.com/SocialiteProviders/Manager#usage) |
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,18 @@ | ||
{ | ||
"name": "socialiteproviders/google", | ||
"description": "Google OAuth2 Provider for Laravel Socialite", | ||
"license": "MIT", | ||
"authors": [{ | ||
"name": "DraperStudio", | ||
"email": "[email protected]" | ||
}], | ||
"require": { | ||
"php": ">=5.4.0", | ||
"socialiteproviders/manager": "0.1.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"SocialiteProviders\\Google\\": "src/" | ||
} | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
namespace SocialiteProviders\Google; | ||
|
||
use SocialiteProviders\Manager\SocialiteWasCalled; | ||
|
||
class GoogleExtendSocialite | ||
{ | ||
public function handle(SocialiteWasCalled $socialiteWasCalled) | ||
{ | ||
$socialiteWasCalled->extendSocialite('google', __NAMESPACE__.'\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
namespace SocialiteProviders\Google; | ||
|
||
use Laravel\Socialite\Two\AbstractProvider; | ||
use Laravel\Socialite\Two\ProviderInterface; | ||
use Laravel\Socialite\Two\User; | ||
|
||
class Provider extends AbstractProvider implements ProviderInterface | ||
{ | ||
/** | ||
* The scopes being requested. | ||
* | ||
* @var array | ||
*/ | ||
protected $scopes = [ | ||
'https://www.googleapis.com/auth/plus.me', | ||
'https://www.googleapis.com/auth/plus.login', | ||
'https://www.googleapis.com/auth/plus.profile.emails.read', | ||
]; | ||
|
||
/** | ||
* The separating character for the requested scopes. | ||
* | ||
* @var string | ||
*/ | ||
protected $scopeSeparator = ' '; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return 'https://accounts.google.com/o/oauth2/token'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken($token) | ||
{ | ||
$response = $this->getHttpClient()->get('https://www.googleapis.com/plus/v1/people/me', [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer '.$token, | ||
], | ||
]); | ||
|
||
return json_decode($response->getBody(), true); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function mapUserToObject(array $user) | ||
{ | ||
return (new User())->setRaw($user)->map([ | ||
'id' => $user['id'], | ||
'nickname' => array_get($user, 'nickname'), | ||
'name' => $user['displayName'], | ||
'email' => $user['emails'][0]['value'], | ||
'avatar' => array_get($user, 'image')['url'], | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenFields($code) | ||
{ | ||
return array_merge(parent::getTokenFields($code), ['grant_type' => 'authorization_code']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function user() | ||
{ | ||
$user = $this->mapUserToObject($this->getUserByToken( | ||
$token = $this->getAccessToken($this->getCode()) | ||
)); | ||
|
||
return $user->setToken($token); | ||
} | ||
} |