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\Request;
13 use Illuminate\Support\Str;
14 use Laravel\Socialite\Contracts\User as SocialUser;
16 class SocialController extends Controller
19 protected $socialAuthService;
20 protected $registrationService;
23 * SocialController constructor.
25 public function __construct(SocialAuthService $socialAuthService, RegistrationService $registrationService)
27 $this->middleware('guest')->only(['getRegister', 'postRegister']);
28 $this->socialAuthService = $socialAuthService;
29 $this->registrationService = $registrationService;
33 * Redirect to the relevant social site.
34 * @throws SocialDriverNotConfigured
36 public function login(string $socialDriver)
38 session()->put('social-callback', 'login');
39 return $this->socialAuthService->startLogIn($socialDriver);
43 * Redirect to the social site for authentication intended to register.
44 * @throws SocialDriverNotConfigured
45 * @throws UserRegistrationException
47 public function register(string $socialDriver)
49 $this->registrationService->ensureRegistrationAllowed();
50 session()->put('social-callback', 'register');
51 return $this->socialAuthService->startRegister($socialDriver);
55 * The callback for social login services.
56 * @throws SocialSignInException
57 * @throws SocialDriverNotConfigured
58 * @throws UserRegistrationException
60 public function callback(Request $request, string $socialDriver)
62 if (!session()->has('social-callback')) {
63 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
66 // Check request for error information
67 if ($request->has('error') && $request->has('error_description')) {
68 throw new SocialSignInException(trans('errors.social_login_bad_response', [
69 'socialAccount' => $socialDriver,
70 'error' => $request->get('error_description'),
74 $action = session()->pull('social-callback');
76 // Attempt login or fall-back to register if allowed.
77 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
78 if ($action === 'login') {
80 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
81 } catch (SocialSignInAccountNotUsed $exception) {
82 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
83 return $this->socialRegisterCallback($socialDriver, $socialUser);
89 if ($action === 'register') {
90 return $this->socialRegisterCallback($socialDriver, $socialUser);
93 return redirect()->back();
97 * Detach a social account from a user.
99 public function detach(string $socialDriver)
101 $this->socialAuthService->detachSocialAccount($socialDriver);
102 session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
103 return redirect(user()->getEditUrl());
107 * Register a new user after a registration callback.
108 * @throws UserRegistrationException
110 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
112 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
113 $socialAccount = $this->socialAuthService->fillSocialAccount($socialDriver, $socialUser);
114 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
116 // Create an array of the user data to create a new user instance
118 'name' => $socialUser->getName(),
119 'email' => $socialUser->getEmail(),
120 'password' => Str::random(32)
123 // Take name from email address if empty
124 if (!$userData['name']) {
125 $userData['name'] = explode('@', $userData['email'])[0];
128 $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
129 auth()->login($user);
131 $this->showSuccessNotification(trans('auth.register_success'));
132 return redirect('/');