3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\Oidc\OidcException;
6 use BookStack\Access\Oidc\OidcService;
7 use BookStack\Http\Controller;
8 use Illuminate\Http\Request;
10 class OidcController extends Controller
12 protected OidcService $oidcService;
14 public function __construct(OidcService $oidcService)
16 $this->oidcService = $oidcService;
17 $this->middleware('guard:oidc');
21 * Start the authorization login flow via OIDC.
23 public function login()
26 $loginDetails = $this->oidcService->login();
27 } catch (OidcException $exception) {
28 $this->showErrorNotification($exception->getMessage());
30 return redirect('/login');
33 session()->flash('oidc_state', $loginDetails['state']);
35 return redirect($loginDetails['url']);
39 * Authorization flow redirect callback.
40 * Processes authorization response from the OIDC Authorization Server.
42 public function callback(Request $request)
44 $storedState = session()->pull('oidc_state');
45 $responseState = $request->query('state');
47 if ($storedState !== $responseState) {
48 $this->showErrorNotification(trans('errors.oidc_fail_authed', ['system' => config('oidc.name')]));
50 return redirect('/login');
54 $this->oidcService->processAuthorizeResponse($request->query('code'));
55 } catch (OidcException $oidcException) {
56 $this->showErrorNotification($oidcException->getMessage());
58 return redirect('/login');
61 return redirect()->intended();
65 * Log the user out then start the OIDC RP-initiated logout process.
67 public function logout()
69 return redirect($this->oidcService->logout());