2021-10-06 18:05:26 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers\Auth;
|
|
|
|
|
2021-10-12 18:04:28 -04:00
|
|
|
use BookStack\Auth\Access\Oidc\OidcService;
|
2022-02-24 09:16:09 -05:00
|
|
|
use BookStack\Auth\Access\Oidc\OidcException;
|
2021-10-06 18:05:26 -04:00
|
|
|
use BookStack\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2021-10-13 11:51:27 -04:00
|
|
|
class OidcController extends Controller
|
2021-10-06 18:05:26 -04:00
|
|
|
{
|
2022-02-24 09:16:09 -05:00
|
|
|
protected OidcService $oidcService;
|
2021-10-06 18:05:26 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenIdController constructor.
|
|
|
|
*/
|
2021-10-12 18:04:28 -04:00
|
|
|
public function __construct(OidcService $oidcService)
|
2021-10-06 18:05:26 -04:00
|
|
|
{
|
|
|
|
$this->oidcService = $oidcService;
|
|
|
|
$this->middleware('guard:oidc');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the authorization login flow via OIDC.
|
|
|
|
*/
|
|
|
|
public function login()
|
|
|
|
{
|
2022-02-24 09:16:09 -05:00
|
|
|
try {
|
|
|
|
$loginDetails = $this->oidcService->login();
|
|
|
|
} catch (OidcException $exception) {
|
|
|
|
$this->showErrorNotification($exception->getMessage());
|
|
|
|
return redirect('/login');
|
|
|
|
}
|
|
|
|
|
2021-10-06 18:05:26 -04:00
|
|
|
session()->flash('oidc_state', $loginDetails['state']);
|
|
|
|
|
|
|
|
return redirect($loginDetails['url']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-10-13 11:51:27 -04:00
|
|
|
* Authorization flow redirect callback.
|
2021-10-06 18:05:26 -04:00
|
|
|
* Processes authorization response from the OIDC Authorization Server.
|
|
|
|
*/
|
2021-10-13 11:51:27 -04:00
|
|
|
public function callback(Request $request)
|
2021-10-06 18:05:26 -04:00
|
|
|
{
|
|
|
|
$storedState = session()->pull('oidc_state');
|
|
|
|
$responseState = $request->query('state');
|
|
|
|
|
|
|
|
if ($storedState !== $responseState) {
|
|
|
|
$this->showErrorNotification(trans('errors.oidc_fail_authed', ['system' => config('oidc.name')]));
|
2021-10-16 11:01:59 -04:00
|
|
|
|
2021-10-06 18:05:26 -04:00
|
|
|
return redirect('/login');
|
|
|
|
}
|
|
|
|
|
2022-02-24 09:16:09 -05:00
|
|
|
try {
|
|
|
|
$this->oidcService->processAuthorizeResponse($request->query('code'));
|
|
|
|
} catch (OidcException $oidcException) {
|
|
|
|
$this->showErrorNotification($oidcException->getMessage());
|
|
|
|
return redirect('/login');
|
|
|
|
}
|
2021-10-16 11:01:59 -04:00
|
|
|
|
2021-10-06 18:05:26 -04:00
|
|
|
return redirect()->intended();
|
|
|
|
}
|
|
|
|
}
|