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