]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/SocialController.php
Started moving MFA and email confirmation to new login flow
[bookstack] / app / Http / Controllers / Auth / SocialController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
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;
16
17 class SocialController extends Controller
18 {
19     protected $socialAuthService;
20     protected $registrationService;
21     protected $loginService;
22
23     /**
24      * SocialController constructor.
25      */
26     public function __construct(
27         SocialAuthService $socialAuthService,
28         RegistrationService $registrationService,
29         LoginService $loginService
30     )
31     {
32         $this->middleware('guest')->only(['getRegister', 'postRegister']);
33         $this->socialAuthService = $socialAuthService;
34         $this->registrationService = $registrationService;
35         $this->loginService = $loginService;
36     }
37
38     /**
39      * Redirect to the relevant social site.
40      *
41      * @throws SocialDriverNotConfigured
42      */
43     public function login(string $socialDriver)
44     {
45         session()->put('social-callback', 'login');
46
47         return $this->socialAuthService->startLogIn($socialDriver);
48     }
49
50     /**
51      * Redirect to the social site for authentication intended to register.
52      *
53      * @throws SocialDriverNotConfigured
54      * @throws UserRegistrationException
55      */
56     public function register(string $socialDriver)
57     {
58         $this->registrationService->ensureRegistrationAllowed();
59         session()->put('social-callback', 'register');
60
61         return $this->socialAuthService->startRegister($socialDriver);
62     }
63
64     /**
65      * The callback for social login services.
66      *
67      * @throws SocialSignInException
68      * @throws SocialDriverNotConfigured
69      * @throws UserRegistrationException
70      */
71     public function callback(Request $request, string $socialDriver)
72     {
73         if (!session()->has('social-callback')) {
74             throw new SocialSignInException(trans('errors.social_no_action_defined'), '/login');
75         }
76
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'),
82             ]), '/login');
83         }
84
85         $action = session()->pull('social-callback');
86
87         // Attempt login or fall-back to register if allowed.
88         $socialUser = $this->socialAuthService->getSocialUser($socialDriver);
89         if ($action === 'login') {
90             try {
91                 return $this->socialAuthService->handleLoginCallback($socialDriver, $socialUser);
92             } catch (SocialSignInAccountNotUsed $exception) {
93                 if ($this->socialAuthService->driverAutoRegisterEnabled($socialDriver)) {
94                     return $this->socialRegisterCallback($socialDriver, $socialUser);
95                 }
96
97                 throw $exception;
98             }
99         }
100
101         if ($action === 'register') {
102             return $this->socialRegisterCallback($socialDriver, $socialUser);
103         }
104
105         return redirect()->back();
106     }
107
108     /**
109      * Detach a social account from a user.
110      */
111     public function detach(string $socialDriver)
112     {
113         $this->socialAuthService->detachSocialAccount($socialDriver);
114         session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));
115
116         return redirect(user()->getEditUrl());
117     }
118
119     /**
120      * Register a new user after a registration callback.
121      *
122      * @throws UserRegistrationException
123      */
124     protected function socialRegisterCallback(string $socialDriver, SocialUser $socialUser)
125     {
126         $socialUser = $this->socialAuthService->handleRegistrationCallback($socialDriver, $socialUser);
127         $socialAccount = $this->socialAuthService->newSocialAccount($socialDriver, $socialUser);
128         $emailVerified = $this->socialAuthService->driverAutoConfirmEmailEnabled($socialDriver);
129
130         // Create an array of the user data to create a new user instance
131         $userData = [
132             'name'     => $socialUser->getName(),
133             'email'    => $socialUser->getEmail(),
134             'password' => Str::random(32),
135         ];
136
137         // Take name from email address if empty
138         if (!$userData['name']) {
139             $userData['name'] = explode('@', $userData['email'])[0];
140         }
141
142         $user = $this->registrationService->registerUser($userData, $socialAccount, $emailVerified);
143         $this->loginService->login($user, $socialDriver);
144
145         $this->showSuccessNotification(trans('auth.register_success'));
146         return redirect('/');
147     }
148 }