3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\RegistrationService;
6 use BookStack\Auth\Access\SocialAuthService;
7 use BookStack\Exceptions\SocialDriverNotConfigured;
8 use BookStack\Exceptions\SocialSignInAccountNotUsed;
9 use BookStack\Exceptions\SocialSignInException;
10 use BookStack\Exceptions\UserRegistrationException;
11 use BookStack\Http\Controllers\Controller;
12 use Illuminate\Http\RedirectResponse;
13 use Illuminate\Http\Request;
14 use Illuminate\Routing\Redirector;
15 use Illuminate\Support\Str;
16 use Laravel\Socialite\Contracts\User as SocialUser;
18 class SocialController extends Controller
21 protected $socialAuthService;
22 protected $registrationService;
25 * SocialController constructor.
27 public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
29 $this->middleware('guest')->only(['getRegister', 'postRegister']);
30 $this->socialAuthService = $socialAuthService;
31 $this->registrationService = $registrationService;
36 * Redirect to the relevant social site.
37 * @throws \BookStack\Exceptions\SocialDriverNotConfigured
39 public function getSocialLogin(string $socialDriver)
41 session()->put('social-callback', 'login');
42 return $this->socialAuthService->startLogIn($socialDriver);
46 * Redirect to the social site for authentication intended to register.
47 * @throws SocialDriverNotConfigured
48 * @throws UserRegistrationException
50 public function socialRegister(string $socialDriver)
52 $this->registrationService->ensureRegistrationAllowed();
53 session()->put('social-callback', 'register');
54 return $this->socialAuthService->startRegister($socialDriver);
58 * The callback for social login services.
59 * @throws SocialSignInException
60 * @throws SocialDriverNotConfigured
61 * @throws UserRegistrationException
63 public function socialCallback(Request $request, string $socialDriver)
65 if (!session()->has('social-callback')) {
66 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
69 // Check request for error information
70 if ($request->has('error') && $request->has('error_description')) {
71 throw new SocialSignInException(trans('errors.social_login_bad_response', [
72 'socialAccount' => $socialDriver,
73 'error' => $request->get('error_description'),
77 $action = session()->pull('social-callback');
79 // Attempt login or fall-back to register if allowed.
80 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
81 if ($action === 'login') {
83 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
84 } catch (SocialSignInAccountNotUsed $exception) {
85 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
86 return $this->socialRegisterCallback($socialDriver, $socialUser);
92 if ($action === 'register') {
93 return $this->socialRegisterCallback($socialDriver, $socialUser);
96 return redirect()->back();
100 * Detach a social account from a user.
102 public function detachSocialAccount(string $socialDriver)
104 $this->socialAuthService->detachSocialAccount($socialDriver);
105 session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
106 return redirect(user()->getEditUrl());
110 * Register a new user after a registration callback.
111 * @throws UserRegistrationException
113 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
115 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
116 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
117 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
119 // Create an array of the user data to create a new user instance
121 'name' => $socialUser->getName(),
122 'email' => $socialUser->getEmail(),
123 'password' => Str::random(32)
126 // Take name from email address if empty
127 if (!$userData['name']) {
128 $userData['name'] = explode('@', $userData['email'])[0];
131 $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
132 auth()->login($user);
134 $this->showSuccessNotification(trans('auth.register_success'));
135 return redirect('/');