Skip to content

Commit

Permalink
Merge pull request #79 from jeremykenedy/provider-updates
Browse files Browse the repository at this point in the history
cleanup state callback, cleanup sentry status, add check for if provi…
  • Loading branch information
jeremykenedy authored Dec 15, 2022
2 parents edb6094 + ea1b5b3 commit 5b71408
Show file tree
Hide file tree
Showing 7 changed files with 2,888 additions and 2,820 deletions.
37 changes: 20 additions & 17 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

WWWGROUP=1000
WWWUSER=1000

EMAIL_EXCEPTION_ENABLED=true
EMAIL_EXCEPTION_FROM="${MAIL_FROM_ADDRESS}"
EMAIL_EXCEPTION_TO='[email protected], [email protected]'
EMAIL_EXCEPTION_CC=''
EMAIL_EXCEPTION_BCC=''
EMAIL_EXCEPTION_SUBJECT=''

JWT_SECRET=

# The Sentry IO Settings get overwritten on save through the GUI
SENTRY_IO_ENABLED=0
SENTRY_IO_USER_FEEDBACK_ENABLED=0
SENTRY_LARAVEL_DSN=
SENTRY_TRACES_SAMPLE_RATE=1.0

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
Expand All @@ -65,28 +83,13 @@ VITE_SENTRY_AUTH_TOKEN=''
VITE_SERVER_HTTPS_KEY='/Users/ENTERYOURSYSTEMPATH/.config/valet/Certificates/laravel-secret-messages.test.key'
VITE_SERVER_HTTPS_CERT='/Users/ENTERYOURSYSTEMPATH/.config/valet/Certificates/laravel-secret-messages.test.crt'
VITE_SERVER_HOST='YOUR-PROJECT-URL.test'

WWWGROUP=1000
WWWUSER=1000

EMAIL_EXCEPTION_ENABLED=true
EMAIL_EXCEPTION_FROM="${MAIL_FROM_ADDRESS}"
EMAIL_EXCEPTION_TO='[email protected], [email protected]'
EMAIL_EXCEPTION_CC=''
EMAIL_EXCEPTION_BCC=''
EMAIL_EXCEPTION_SUBJECT=''

JWT_SECRET=
VITE_SENTRY_IO_ENABLED="${SENTRY_IO_ENABLED}"
VITE_APP_ENV="${APP_ENV}"

# These ALL below will all be seeded but can be changed through the super admin GUI.
# If they have a value in the database for the field, that field will never seed again.
# This is to protect and not override the users data.

SENTRY_IO_ENABLED=0
SENTRY_IO_USER_FEEDBACK_ENABLED=0
SENTRY_LARAVEL_DSN=
SENTRY_TRACES_SAMPLE_RATE=1.0

SEED_SUPER_ADMIN_USER_ENABLED=true
SEED_SUPER_ADMIN_USER_NAME='Rick Sanchez'
SEED_SUPER_ADMIN_USER_EMAIL='[email protected]'
Expand Down
21 changes: 18 additions & 3 deletions app/Http/Controllers/Auth/SocialiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use App\Traits\SocialiteProvidersTrait;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Laravel\Socialite\Facades\Socialite;

Expand Down Expand Up @@ -59,8 +61,16 @@ public function getSocialRedirect(string $provider, Request $request)
abort(419);
}

$state = Crypt::encrypt(config('app.key'));
$user = auth('sanctum')->user();

if ($user) {
$token = $user->createToken($provider.'-user-token')->plainTextToken;
$state = Crypt::encrypt($token);
}

return response()->json([
'url' => Socialite::driver($provider)->stateless()->redirect()->getTargetUrl(),
'url' => Socialite::driver($provider)->stateless()->with(['state' => $state])->redirect()->getTargetUrl(),
]);
}

Expand All @@ -79,12 +89,17 @@ public function handleSocialCallback(string $provider, Request $request)
}

$socialUser = Socialite::driver($provider)->stateless()->user();
$userData = $this->findOrCreateUser($provider, $socialUser);
$state = $request->state ? Crypt::decrypt($request->state) : null;
$userData = $this->findOrCreateUser($provider, $socialUser, $state);

$user = $userData['user'];
$token = $userData['token'];

Auth::login($user, true);
if ($user && $token) {
auth()->login($user);
} else {
$token = 'cannot_add';
}

return view('socialite/callback', [
'token' => $token,
Expand Down
49 changes: 33 additions & 16 deletions app/Traits/SocialiteProvidersTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Laravel\Sanctum\PersonalAccessToken;
use Laravel\Socialite\Contracts\User as SocialiteUser;
use Laravel\Socialite\Facades\Socialite;

Expand Down Expand Up @@ -444,28 +445,44 @@ protected function loginsList()
* @param SocialiteUser $user
* @return App\Models\User
*/
protected function findOrCreateUser(string $provider, SocialiteUser $user): array
protected function findOrCreateUser(string $provider, SocialiteUser $user, string $state = null): array
{
$oauthProvider = SocialiteProvider::where('provider', $provider)
->where('provider_user_id', $user->getId())
->first();
$existingUser = null;

if ($oauthProvider) {
$oauthProvider->update([
'access_token' => $user->token,
'refresh_token' => $user->refreshToken,
]);

return [
'user' => $oauthProvider->user,
'token' => $oauthProvider->user->createToken($provider.'-token')->plainTextToken,
];
if ($state && $state != config('app.key')) {
$token = PersonalAccessToken::findToken($state);
if ($token) {
$existingUser = $token->tokenable;
}
} else {
$existingUser = User::whereEmail($user->getEmail())->first();
}

$existingUser = User::whereEmail($user->getEmail())->first();
$oauthProvider = SocialiteProvider::where('provider', $provider)
->where('provider_user_id', $user->getId())
->first();

if (! $existingUser) {
$existingUser = Auth::user();
if ($oauthProvider) {
$oauthProvider->update([
'access_token' => $user->token,
'refresh_token' => $user->refreshToken,
]);

return [
'user' => $oauthProvider->user,
'token' => $oauthProvider->user->createToken($provider.'-token')->plainTextToken,
];
}
} else {
if ($oauthProvider) {
if ($oauthProvider->user->id != $existingUser->id) {
return [
'user' => null,
'token' => null,
];
}
}
}

$user = $this->updateOrCreateUser($provider, $user, $existingUser);
Expand Down
Loading

0 comments on commit 5b71408

Please sign in to comment.