Skip to content

Commit

Permalink
Release km socialite
Browse files Browse the repository at this point in the history
  • Loading branch information
CasperLaiTW committed Mar 15, 2017
0 parents commit 31b0a6a
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/.idea
/composer.lock
18 changes: 18 additions & 0 deletions composer.json
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/"
}
}
}
100 changes: 100 additions & 0 deletions src/KmSocialiteProvider.php
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'
]);
}
}

0 comments on commit 31b0a6a

Please sign in to comment.