3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\Saml2Service;
6 use BookStack\Http\Controller;
7 use Illuminate\Http\Request;
8 use Illuminate\Support\Str;
10 class Saml2Controller extends Controller
12 protected Saml2Service $samlService;
15 * Saml2Controller constructor.
17 public function __construct(Saml2Service $samlService)
19 $this->samlService = $samlService;
20 $this->middleware('guard:saml2');
24 * Start the login flow via SAML2.
26 public function login()
28 $loginDetails = $this->samlService->login();
29 session()->flash('saml2_request_id', $loginDetails['id']);
31 return redirect($loginDetails['url']);
35 * Start the logout flow via SAML2.
37 public function logout()
39 $logoutDetails = $this->samlService->logout(auth()->user());
41 if ($logoutDetails['id']) {
42 session()->flash('saml2_logout_request_id', $logoutDetails['id']);
45 return redirect($logoutDetails['url']);
49 * Get the metadata for this SAML2 service provider.
51 public function metadata()
53 $metaData = $this->samlService->metadata();
55 return response()->make($metaData, 200, [
56 'Content-Type' => 'text/xml',
61 * Single logout service.
62 * Handle logout requests and responses.
66 $requestId = session()->pull('saml2_logout_request_id', null);
67 $redirect = $this->samlService->processSlsResponse($requestId) ?? '/';
69 return redirect($redirect);
73 * Assertion Consumer Service start URL. Takes the SAMLResponse from the IDP.
74 * Due to being an external POST request, we likely won't have context of the
75 * current user session due to lax cookies. To work around this we store the
76 * SAMLResponse data and redirect to the processAcs endpoint for the actual
77 * processing of the request with proper context of the user session.
79 public function startAcs(Request $request)
81 $samlResponse = $request->get('SAMLResponse', null);
83 if (empty($samlResponse)) {
84 $this->showErrorNotification(trans('errors.saml_fail_authed', ['system' => config('saml2.name')]));
86 return redirect('/login');
89 $acsId = Str::random(16);
90 $cacheKey = 'saml2_acs:' . $acsId;
91 cache()->set($cacheKey, encrypt($samlResponse), 10);
93 return redirect()->guest('/saml2/acs?id=' . $acsId);
97 * Assertion Consumer Service process endpoint.
98 * Processes the SAML response from the IDP with context of the current session.
99 * Takes the SAML request from the cache, added by the startAcs method above.
101 public function processAcs(Request $request)
103 $acsId = $request->get('id', null);
104 $cacheKey = 'saml2_acs:' . $acsId;
105 $samlResponse = null;
108 $samlResponse = decrypt(cache()->pull($cacheKey));
109 } catch (\Exception $exception) {
111 $requestId = session()->pull('saml2_request_id', null);
113 if (empty($acsId) || empty($samlResponse)) {
114 $this->showErrorNotification(trans('errors.saml_fail_authed', ['system' => config('saml2.name')]));
116 return redirect('/login');
119 $user = $this->samlService->processAcsResponse($requestId, $samlResponse);
120 if (is_null($user)) {
121 $this->showErrorNotification(trans('errors.saml_fail_authed', ['system' => config('saml2.name')]));
123 return redirect('/login');
126 return redirect()->intended();