-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 31b0a6a
Showing
3 changed files
with
121 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,3 @@ | ||
/vendor/ | ||
/.idea | ||
/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,18 @@ | ||
{ | ||
"name": "vohinc/km-socialite", | ||
"type": "library", | ||
"require": { | ||
"laravel/socialite": "^3.0" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Casper Lai", | ||
"email": "casper.lai@voh-design.com" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Voh\\KmSocialite\\": "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,100 @@ | ||
<?php | ||
|
||
namespace Voh\KmSocialite; | ||
|
||
use Illuminate\Http\Request; | ||
use Laravel\Socialite\Two\AbstractProvider; | ||
use Laravel\Socialite\Two\ProviderInterface; | ||
use Laravel\Socialite\Two\User; | ||
|
||
class KmSocialiteProvider extends AbstractProvider implements ProviderInterface | ||
{ | ||
/** | ||
* Unique KmSocialiteProvider Identifier. | ||
*/ | ||
const IDENTIFIER = 'KM'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopes = ['profile', 'email', 'permission']; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $scopeSeparator = ' '; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $stateless = true; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $baseUrl; | ||
|
||
public function __construct(Request $request, $clientId, $clientSecret, $redirectUrl) | ||
{ | ||
parent::__construct($request, $clientId, $clientSecret, $redirectUrl); | ||
$this->baseUrl = config('services.km.url'); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getAuthUrl($state) | ||
{ | ||
return $this->buildAuthUrlFromBase("{$this->baseUrl}/oauth/authorize", $state); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenUrl() | ||
{ | ||
return "{$this->baseUrl}/oauth/token"; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getUserByToken($token) | ||
{ | ||
$response = $this->getHttpClient()->post("{$this->baseUrl}/api/user/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'], | ||
'name' => $user['name'], | ||
'email' => $user['email'], | ||
'job' => $user['job'], | ||
'unit' => $user['unit'], | ||
'class_no' => $user['classNo'], | ||
'seat' => $user['seat'], | ||
'permission' => $user['permission'], | ||
]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getTokenFields($code) | ||
{ | ||
return array_merge(parent::getTokenFields($code), [ | ||
'grant_type' => 'authorization_code' | ||
]); | ||
} | ||
} |