3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\Oidc\OidcException;
6 use BookStack\Access\Oidc\OidcService;
7 use BookStack\Http\Controllers\Controller;
8 use Illuminate\Http\Request;
10 class OidcController extends Controller
12 protected OidcService $oidcService;
15 * OpenIdController constructor.
17 public function __construct(OidcService $oidcService)
19 $this->oidcService = $oidcService;
20 $this->middleware('guard:oidc');
24 * Start the authorization login flow via OIDC.
26 public function login()
29 $loginDetails = $this->oidcService->login();
30 } catch (OidcException $exception) {
31 $this->showErrorNotification($exception->getMessage());
33 return redirect('/login');
36 session()->flash('oidc_state', $loginDetails['state']);
38 return redirect($loginDetails['url']);
42 * Authorization flow redirect callback.
43 * Processes authorization response from the OIDC Authorization Server.
45 public function callback(Request $request)
47 $storedState = session()->pull('oidc_state');
48 $responseState = $request->query('state');
50 if ($storedState !== $responseState) {
51 $this->showErrorNotification(trans('errors.oidc_fail_authed', ['system' => config('oidc.name')]));
53 return redirect('/login');
57 $this->oidcService->processAuthorizeResponse($request->query('code'));
58 } catch (OidcException $oidcException) {
59 $this->showErrorNotification($oidcException->getMessage());
61 return redirect('/login');
64 return redirect()->intended();