1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Appropriately refresh sending tokens

This commit is contained in:
David Bomba 2022-06-22 19:15:31 +10:00
parent 77cdae7907
commit 3141a33ced
4 changed files with 35 additions and 16 deletions

View File

@ -772,6 +772,8 @@ class LoginController extends BaseController
$oauth_user_token = $socialite_user->accessTokenResponseBody['access_token'];
$oauth_expiry = now()->addSeconds($socialite_user->accessTokenResponseBody['expires_in']) ?: now()->addSeconds(300);
if($user = OAuth::handleAuth($socialite_user, $provider))
{
@ -785,7 +787,8 @@ class LoginController extends BaseController
'oauth_user_id' => $socialite_user->getId(),
'oauth_provider_id' => $provider,
'oauth_user_token' => $oauth_user_token,
'oauth_user_refresh_token' => $socialite_user->accessTokenResponseBody['refresh_token']
'oauth_user_refresh_token' => $socialite_user->accessTokenResponseBody['refresh_token'],
'oauth_user_token_expiry' => $oauth_expiry,
];
$user->update($update_user);

View File

@ -342,23 +342,38 @@ class NinjaMailerJob implements ShouldQueue
private function refreshOfficeToken($user)
{
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => config('ninja.o365.client_id') ,
'client_secret' => config('ninja.o365.client_secret') ,
'scope' => 'email Mail.ReadWrite Mail.Send offline_access profile User.Read openid',
'grant_type' => 'refresh_token',
'refresh_token' => $user->oauth_user_refresh_token
],
])->getBody()->getContents());
if($user->oauth_user_token_expiry->lt(now()))
{
$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
if($token)
return $token->access_token;
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => config('ninja.o365.client_id') ,
'client_secret' => config('ninja.o365.client_secret') ,
'scope' => 'email Mail.ReadWrite Mail.Send offline_access profile User.Read openid',
'grant_type' => 'refresh_token',
'refresh_token' => $user->oauth_user_refresh_token
],
])->getBody()->getContents());
return false;
if($token){
nlog($token);
$user->oauth_user_token = $token->access_token;
$user->oauth_user_token_expiry = now()->addSeconds($token->expires_in);
$user->save();
return $token->access_token;
}
return false;
}
return $user->oauth_user_refresh_token;
}
}

View File

@ -106,6 +106,7 @@ class User extends Authenticatable implements MustVerifyEmail
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'oauth_user_token_expiry' => 'datetime',
];

View File

@ -13,7 +13,7 @@ class FixesForDescriptionInPdfDesigns extends Migration
*/
public function up()
{
\Illuminate\Support\Facades\Artisan::call('ninja:design-update');
\Illuminate\Support\Facades\Artisan::call('ninja:design-update');
}
/**