<?php

namespace Buckhill\LaravelSAML\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use LightSaml\Model\Protocol\LogoutRequest;
use LightSaml\Model\Protocol\LogoutResponse;
use function AuthStack\SP\receive;
use function AuthStack\SP\logout;


class SLO extends Controller
{
    public function process(Request $request)
    {
        $message = receive($request);

        if($message instanceof LogoutRequest)
        {
            return $this->logout($message);
        }

        if($message instanceof LogoutResponse)
        {
            return $this->complete($message);
        }
    }

    public function logout(LogoutRequest $message)
    {
        session()->flush();

        logout(config('sso'), env('SAML_IDP'));
    }

    public function complete(LogoutResponse $message)
    {
        return response()->json(['message' => 'Single logout completed']);
    }
}