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