2020-05-05 07:06:19 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Services\Saml;
|
2021-06-10 13:15:52 -07:00
|
|
|
use Illuminate\Http\Request;
|
2020-05-05 07:06:19 -07:00
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This controller provides the endpoint for SAML communication and metadata.
|
|
|
|
*
|
|
|
|
* @author Johnson Yi <jyi.dev@outlook.com>
|
|
|
|
*
|
|
|
|
* @since 5.0.0
|
|
|
|
*/
|
|
|
|
class SamlController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Saml
|
|
|
|
*/
|
|
|
|
protected $saml;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new authentication controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Saml $saml)
|
|
|
|
{
|
|
|
|
$this->saml = $saml;
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
$this->middleware('guest', ['except' => ['metadata', 'sls']]);
|
2020-05-05 07:06:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return SAML SP metadata for Snipe-IT
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* /saml/metadata
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @author Johnson Yi <jyi.dev@outlook.com>
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function metadata(Request $request)
|
|
|
|
{
|
2020-05-06 02:50:50 -07:00
|
|
|
$metadata = $this->saml->getSPMetadata();
|
2020-05-05 07:06:19 -07:00
|
|
|
|
2020-05-06 02:50:50 -07:00
|
|
|
if (empty($metadata)) {
|
2022-05-10 12:07:07 -07:00
|
|
|
\Log::debug('SAML metadata is empty - return a 403');
|
2020-05-06 02:50:50 -07:00
|
|
|
return response()->view('errors.403', [], 403);
|
2020-05-05 07:06:19 -07:00
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2020-11-20 18:54:25 -08:00
|
|
|
return response()->streamDownload(function () use ($metadata) {
|
|
|
|
echo $metadata;
|
|
|
|
}, 'snipe-it-metadata.xml', ['Content-Type' => 'text/xml']);
|
2020-05-05 07:06:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begin the SP-Initiated SSO by sending AuthN to the IdP.
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* /login/saml
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @author Johnson Yi <jyi.dev@outlook.com>
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function login(Request $request)
|
|
|
|
{
|
|
|
|
$auth = $this->saml->getAuth();
|
2021-06-10 13:15:52 -07:00
|
|
|
$ssoUrl = $auth->login(null, [], false, false, false, false);
|
|
|
|
|
2020-05-05 07:06:19 -07:00
|
|
|
return redirect()->away($ssoUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives, parses the assertion from IdP and flashes SAML data
|
|
|
|
* back to the LoginController for authentication.
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* /saml/acs
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @author Johnson Yi <jyi.dev@outlook.com>
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function acs(Request $request)
|
|
|
|
{
|
|
|
|
$saml = $this->saml;
|
|
|
|
$auth = $saml->getAuth();
|
|
|
|
$auth->processResponse();
|
|
|
|
$errors = $auth->getErrors();
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
if (! empty($errors)) {
|
|
|
|
Log::error('There was an error with SAML ACS: '.implode(', ', $errors));
|
|
|
|
Log::error('Reason: '.$auth->getLastErrorReason());
|
|
|
|
|
2020-05-05 07:06:19 -07:00
|
|
|
return redirect()->route('login')->with('error', trans('auth/message.signin.error'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$samlData = $saml->extractData();
|
|
|
|
|
|
|
|
return redirect()->route('login')->with('saml_login', $samlData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives LogoutRequest/LogoutResponse from IdP and flashes
|
|
|
|
* back to the LoginController for logging out.
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-12-04 02:54:04 -08:00
|
|
|
* /saml/sls
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @author Johnson Yi <jyi.dev@outlook.com>
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @since 5.0.0
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2021-06-10 13:15:52 -07:00
|
|
|
*
|
2020-05-05 07:06:19 -07:00
|
|
|
* @return Redirect
|
|
|
|
*/
|
|
|
|
public function sls(Request $request)
|
|
|
|
{
|
|
|
|
$auth = $this->saml->getAuth();
|
2021-02-23 14:53:55 -08:00
|
|
|
$retrieveParametersFromServer = $this->saml->getSetting('retrieveParametersFromServer', false);
|
|
|
|
$sloUrl = $auth->processSLO(true, null, $retrieveParametersFromServer, null, true);
|
2020-05-05 07:06:19 -07:00
|
|
|
$errors = $auth->getErrors();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
|
|
|
if (! empty($errors)) {
|
|
|
|
Log::error('There was an error with SAML SLS: '.implode(', ', $errors));
|
|
|
|
Log::error('Reason: '.$auth->getLastErrorReason());
|
|
|
|
|
2020-05-05 07:06:19 -07:00
|
|
|
return view('errors.403');
|
|
|
|
}
|
|
|
|
|
2022-05-14 04:59:34 -07:00
|
|
|
return redirect()->route('logout')->with(['saml_logout' => true,'saml_slo_redirect_url' => $sloUrl]);
|
2020-05-05 07:06:19 -07:00
|
|
|
}
|
|
|
|
}
|