+ public function __construct(RegistrationService $registrationService, LoginService $loginService)
+ {
+ $this->config = config('saml2');
+ $this->registrationService = $registrationService;
+ $this->loginService = $loginService;
+ }
+
+ /**
+ * Initiate a login flow.
+ *
+ * @throws Error
+ */
+ public function login(): array
+ {
+ $toolKit = $this->getToolkit();
+ $returnRoute = url('/saml2/acs');
+
+ return [
+ 'url' => $toolKit->login($returnRoute, [], false, false, true),
+ 'id' => $toolKit->getLastRequestID(),
+ ];
+ }
+
+ /**
+ * Initiate a logout flow.
+ *
+ * @throws Error
+ */
+ public function logout(): array
+ {
+ $toolKit = $this->getToolkit();
+ $returnRoute = url('/');
+
+ try {
+ $email = auth()->user()['email'];
+ $nameIdFormat = env('SAML2_SP_NAME_ID_Format', null);
+ $nameIdSPNameQualifier = env('SAML2_SP_NAME_ID_SP_NAME_QUALIFIER', null);
+
+ $url = $toolKit->logout($returnRoute, [], $email, null, true, $nameIdFormat, null, $nameIdSPNameQualifier);
+ $id = $toolKit->getLastRequestID();
+ } catch (Error $error) {
+ if ($error->getCode() !== Error::SAML_SINGLE_LOGOUT_NOT_SUPPORTED) {
+ throw $error;
+ }
+
+ $this->actionLogout();
+ $url = '/';
+ $id = null;
+ }
+
+ return ['url' => $url, 'id' => $id];
+ }
+
+ /**
+ * Process the ACS response from the idp and return the
+ * matching, or new if registration active, user matched to the idp.
+ * Returns null if not authenticated.
+ *
+ * @throws Error
+ * @throws SamlException
+ * @throws ValidationError
+ * @throws JsonDebugException
+ * @throws UserRegistrationException
+ */
+ public function processAcsResponse(?string $requestId): ?User
+ {
+ $toolkit = $this->getToolkit();
+ $toolkit->processResponse($requestId);
+ $errors = $toolkit->getErrors();
+
+ if (!empty($errors)) {
+ throw new Error(
+ 'Invalid ACS Response: ' . implode(', ', $errors)
+ );
+ }
+
+ if (!$toolkit->isAuthenticated()) {
+ return null;
+ }
+
+ $attrs = $toolkit->getAttributes();
+ $id = $toolkit->getNameId();
+
+ return $this->processLoginCallback($id, $attrs);
+ }
+
+ /**
+ * Process a response for the single logout service.
+ *
+ * @throws Error
+ */
+ public function processSlsResponse(?string $requestId): ?string
+ {
+ $toolkit = $this->getToolkit();
+ $retrieveParametersFromServer = env('SAML2_RETRIEVE_PARAMETERS_FROM_SERVER', false);
+
+ $redirect = $toolkit->processSLO(true, $requestId, $retrieveParametersFromServer, null, true);
+
+ $errors = $toolkit->getErrors();
+
+ if (!empty($errors)) {
+ throw new Error(
+ 'Invalid SLS Response: ' . implode(', ', $errors)
+ );
+ }
+
+ $this->actionLogout();
+
+ return $redirect;
+ }
+
+ /**
+ * Do the required actions to log a user out.
+ */
+ protected function actionLogout()