-- SPDX-FileCopyrightText: 2024 David Mosbach <david.mosbach@uniworx.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later

module Handler.SingleSignOut
  ( getSOutR
  , getSSOutR
  ) where

import Import
import Auth.OAuth2 (singleSignOut)
import qualified Network.Wai as W


getSOutR :: Handler Html
getSOutR = do
  $logDebugS "\27[31mSOut\27[0m" "Redirect to LogoutR"
  redirect $ AuthR LogoutR

getSSOutR :: Handler Html
getSSOutR = do
  app <- getYesod
  let redir = intercalate "/" . fst . renderRoute $ SOutR
      root = case approot of
        ApprootRequest f -> f app W.defaultRequest
        _ -> error "approt implementation changed"
      url = decodeUtf8 . urlEncode True . encodeUtf8 $ root <> "/" <> redir
  AppSettings{..} <- getsYesod appSettings'
  $logDebugS "\27[31mSSOut\27[0m" "Redirect to auth server"
  if appSingleSignOn then singleSignOut (Just url) else redirect (AuthR LogoutR)

