]> BookStack Code Mirror - bookstack/blob - app/Access/Controllers/OidcController.php
Auth: Refactored OIDC RP-logout PR code, Extracted logout
[bookstack] / app / Access / Controllers / OidcController.php
1 <?php
2
3 namespace BookStack\Access\Controllers;
4
5 use BookStack\Access\Oidc\OidcException;
6 use BookStack\Access\Oidc\OidcService;
7 use BookStack\Http\Controller;
8 use Illuminate\Http\Request;
9
10 class OidcController extends Controller
11 {
12     protected OidcService $oidcService;
13
14     public function __construct(OidcService $oidcService)
15     {
16         $this->oidcService = $oidcService;
17         $this->middleware('guard:oidc');
18     }
19
20     /**
21      * Start the authorization login flow via OIDC.
22      */
23     public function login()
24     {
25         try {
26             $loginDetails = $this->oidcService->login();
27         } catch (OidcException $exception) {
28             $this->showErrorNotification($exception->getMessage());
29
30             return redirect('/login');
31         }
32
33         session()->flash('oidc_state', $loginDetails['state']);
34
35         return redirect($loginDetails['url']);
36     }
37
38     /**
39      * Authorization flow redirect callback.
40      * Processes authorization response from the OIDC Authorization Server.
41      */
42     public function callback(Request $request)
43     {
44         $storedState = session()->pull('oidc_state');
45         $responseState = $request->query('state');
46
47         if ($storedState !== $responseState) {
48             $this->showErrorNotification(trans('errors.oidc_fail_authed', ['system' => config('oidc.name')]));
49
50             return redirect('/login');
51         }
52
53         try {
54             $this->oidcService->processAuthorizeResponse($request->query('code'));
55         } catch (OidcException $oidcException) {
56             $this->showErrorNotification($oidcException->getMessage());
57
58             return redirect('/login');
59         }
60
61         return redirect()->intended();
62     }
63
64     /**
65      * Log the user out then start the OIDC RP-initiated logout process.
66      */
67     public function logout()
68     {
69         return redirect($this->oidcService->logout());
70     }
71 }