3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Auth\Access\LoginService;
6 use BookStack\Auth\Access\RegistrationService;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Exceptions\SocialDriverNotConfigured;
9 use BookStack\Exceptions\SocialSignInAccountNotUsed;
10 use BookStack\Exceptions\SocialSignInException;
11 use BookStack\Exceptions\UserRegistrationException;
12 use BookStack\Http\Controllers\Controller;
13 use Illuminate\Http\Request;
14 use Illuminate\Support\Str;
15 use Laravel\Socialite\Contracts\User as SocialUser;
17 class SocialController extends Controller
19 protected $socialAuthService;
20 protected $registrationService;
21 protected $loginService;
24 * SocialController constructor.
26 public function __construct(
27 SocialAuthService $socialAuthService,
28 RegistrationService $registrationService,
29 LoginService $loginService
32 $this->middleware('guest')->only(['getRegister', 'postRegister']);
33 $this->socialAuthService = $socialAuthService;
34 $this->registrationService = $registrationService;
35 $this->loginService = $loginService;
39 * Redirect to the relevant social site.
41 * @throws SocialDriverNotConfigured
43 public function login(string $socialDriver)
45 session()->put('social-callback', 'login');
47 return $this->socialAuthService->startLogIn($socialDriver);
51 * Redirect to the social site for authentication intended to register.
53 * @throws SocialDriverNotConfigured
54 * @throws UserRegistrationException
56 public function register(string $socialDriver)
58 $this->registrationService->ensureRegistrationAllowed();
59 session()->put('social-callback', 'register');
61 return $this->socialAuthService->startRegister($socialDriver);
65 * The callback for social login services.
67 * @throws SocialSignInException
68 * @throws SocialDriverNotConfigured
69 * @throws UserRegistrationException
71 public function callback(Request $request, string $socialDriver)
73 if (!session()->has('social-callback')) {
74 throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
77 // Check request for error information
78 if ($request->has('error') && $request->has('error_description')) {
79 throw new SocialSignInException(trans('errors.social_login_bad_response', [
80 'socialAccount' => $socialDriver,
81 'error' => $request->get('error_description'),
85 $action = session()->pull('social-callback');
87 // Attempt login or fall-back to register if allowed.
88 $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
89 if ($action === 'login') {
91 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
92 } catch (SocialSignInAccountNotUsed $exception) {
93 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
94 return $this->socialRegisterCallback($socialDriver, $socialUser);
101 if ($action === 'register') {
102 return $this->socialRegisterCallback($socialDriver, $socialUser);
105 return redirect()->back();
109 * Detach a social account from a user.
111 public function detach(string $socialDriver)
113 $this->socialAuthService->detachSocialAccount($socialDriver);
114 session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
116 return redirect(user()->getEditUrl());
120 * Register a new user after a registration callback.
122 * @throws UserRegistrationException
124 protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
126 $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
127 $socialAccount = $this->socialAuthService->newSocialAccount($socialDriver, $socialUser);
128 $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
130 // Create an array of the user data to create a new user instance
132 'name' => $socialUser->getName(),
133 'email' => $socialUser->getEmail(),
134 'password' => Str::random(32),
137 // Take name from email address if empty
138 if (!$userData['name']) {
139 $userData['name'] = explode('@', $userData['email'])[0];
142 $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
143 $this->showSuccessNotification(trans('auth.register_success'));
144 $this->loginService->login($user, $socialDriver);
146 return redirect('/');