token refreshment
This commit is contained in:
@@ -69,73 +69,74 @@ class Esi {
|
||||
}
|
||||
|
||||
public function GetRefreshToken($charId) {
|
||||
//Get the refresh token from the database
|
||||
$tokenCount = EsiToken::where([
|
||||
'character_id' => $charId,
|
||||
])->count();
|
||||
//Declare variables
|
||||
$currentTime = Carbon::now();
|
||||
$scopes = null;
|
||||
$i = 0;
|
||||
$config = config('esi');
|
||||
|
||||
//If the token is not found, then don't return it.
|
||||
if($tokenCount == 0) {
|
||||
//If the program doesn't find an ESI Token, there is nothing to return
|
||||
if(EsiToken::where(['character_id' => $charId])->count() === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
//Get the ESI Token from the database
|
||||
$token = EsiToken::where([
|
||||
'character_id' => $charId,
|
||||
])->first();
|
||||
|
||||
return $token;
|
||||
//Check the expiration of the token to see if the token has expired and needs to be refreshed using the refresh token
|
||||
$expires = $token->inserted_at + $token->expires_in;
|
||||
$tokenExpiration = Carbon::createFromTimestamp($expires)->toDateTimeString();
|
||||
//If the access token has expired, we need to do a request for a new access token
|
||||
if($currentTime > $token_expiration) {
|
||||
//Get the current scopes of the token
|
||||
$scopesArr = EsiScope::where([
|
||||
'character_id' => $token->character_id,
|
||||
])->get(['scope'])->toArray();
|
||||
|
||||
//Cycle through the scopes, and create the string for scopes to send with the token
|
||||
foreach($scopesArr as $scp) {
|
||||
$scopes .= $scp['scope'];
|
||||
$i++;
|
||||
if($i < sizeof($scopesArr)) {
|
||||
$scopes .= '%20';
|
||||
}
|
||||
}
|
||||
|
||||
//Setup the guzzle client for the request to get a new token
|
||||
$client = new Client(['base_uri' => 'https://login.eveonline.com']);
|
||||
$response = $client->request('POST', '/v2/oauth/token', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'Host' => 'login.eveonline.com',
|
||||
'Authorization' => "Basic " . base64_encode($config['client_id'] . ":" . $config['secret']),
|
||||
],
|
||||
'form_params' => [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $token->refresh_token,
|
||||
],
|
||||
]);
|
||||
//Decode the body of the response which has the token information
|
||||
$body = json_decode($response->getBody(), true);
|
||||
|
||||
|
||||
} else {
|
||||
//If we had a good token which has not expired yet, return the data
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
|
||||
public function SetupEsiAuthentication($token = null) {
|
||||
//Get the platform configuration
|
||||
$config = config('esi');
|
||||
$currentTime = Carbon::now();
|
||||
|
||||
//Declare some variables
|
||||
$authentication = null;
|
||||
$esi = null;
|
||||
$config = config('esi');
|
||||
|
||||
if($token == null) {
|
||||
$esi = new Eseye();
|
||||
} else {
|
||||
$expires = $token->inserted_at + $token->expires_in;
|
||||
$token_expiration = Carbon::createFromTimestamp($expires)->toDateTimeString();
|
||||
|
||||
//If the access token has expired, we need to do a request for a new access token
|
||||
if($currentTime > $token_expiration) {
|
||||
$scopes = null;
|
||||
|
||||
//Get the scopes to pass to the guzzle client
|
||||
$scopesArr = EsiScope::where([
|
||||
'character_id' => $token->character_id,
|
||||
])->get(['scope'])->toArray();
|
||||
|
||||
$i = 0;
|
||||
foreach($scopesArr as $scp) {
|
||||
$scopes .= $scp['scope'];
|
||||
$i++;
|
||||
if($i < sizeof($scopesArr)) {
|
||||
$scopes .= "%20";
|
||||
}
|
||||
}
|
||||
|
||||
//Setup the new guzzle client
|
||||
$client = new Client(['base_uri' => 'https://login.eveonline.com']);
|
||||
$response = $client->request('POST', '/v2/oauth/token', [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
'Host' => 'login.eveonline.com',
|
||||
'Authorization' => "Basic " . base64_encode($config['client_id'] . ":" . $config['secret']),
|
||||
],
|
||||
'form_params' => [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $token->refresh_token,
|
||||
]
|
||||
]);
|
||||
|
||||
dd(json_decode($response->getBody(), true));
|
||||
}
|
||||
|
||||
//Setup the esi authentication container
|
||||
$authentication = new EsiAuthentication([
|
||||
'client_id' => $config['client_id'],
|
||||
'secret' => $config['secret'],
|
||||
|
||||
Reference in New Issue
Block a user