added a Trait for EveAuth2

eve online service provider 2
eve online socialite provider 2
This commit is contained in:
2019-06-23 06:03:23 -05:00
parent d5032e4fee
commit 90fbcb18ba
3 changed files with 146 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class EveOnlineServiceProvider2 extends ServiceProvider {
/**
* Bootstrap any application services
*
* @return void
*/
public function boot() {
$socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'eveonline',
function ($app) use ($socialite) {
$config = $app['config']['services.eveonline'];
return $socialite->buildProvider(EveOnlineSocialiteProvider::class, $config);
}
);
}
}
?>

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Providers;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\User;
class EveOnlineSocialiteProvider2 extends AbstractProvider implements ProviderInterface {
protected $scopeSeparator = ' ';
/**
* Get the authentication URL for the provider
*
* @param string $state
* @return string
*/
protected function getAuthUrl($state) {
return $this->buildAuthUrlFromBase('https://login.eveonline.com/oauth/authorize', $state);
}
/**
* Get the token URL for the provider
*
* @return string
*/
protected function getTokenUrl() {
return 'https://login.eveonline.com/oauth/token';
}
/**
* Get the raw uer for the given access token
*
* @param string $token
* @return array
*/
protected function getUserByToken($token) {
$reponse = $this->getHttpClient()->get('https://login.eveonline.com/oauth/verify', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
return json_decode($reponse->getBody()->getContents(), true);
}
/**
* Map the raw user array to a Socialite User instance.
*
* @param array $user
* @return \Laravel\Socialite\Two\User
*/
protected function mapUserToObjects(array $user) {
return (new User)->setRaw($user)->map([
'id' => $user['CharacterID'],
'name' => $user['CharacterName'],
'owner_hash' => $user['CharacterOwnerHash'],
'avatar' => 'https://image.eveonline.com/Character/' . $user['CharacterID'] . '_128.jpg',
]);
}
/**
* @param string $code
* @return array
*/
protected function getTokenFields($code) {
return array_merge(parent::getTokenFields($code), [
'grant_type' => 'authorization_code',
]);
}
}
?>

47
app/Traits/EveAuth2.php Normal file
View File

@@ -0,0 +1,47 @@
<?php
namespace App\Traits;
use Exception;
use GuzzleHttp\Client;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\InvalidStateException;
trait EveAuth2 {
protected $user;
public function login() {
try {
return Socialite::driver('eveonline')->redirect();
} catch(Exception $e) {
return back();
}
}
public function callback() {
try {
$this->user = Socialite::driver('eveonline')->user();
} catch(InvalidStateException $e) {
return back();
}
return $this->user;
}
public function get_character() {
//Get more detailed character data from CREST
$httpClient = new Client();
$url = "https://esi.evetech.net/latest/characters/{$this->user->id}/?datasource=tranquility";
try {
$response = $httpClient->get($url);
} catch (Exception $exception) {
return abort(504, 'ESI is not reachable, try again late.');
}
return json_decode($response->getBody()->getContents());
}
}
?>