fradrive/src/Auth/OAuth2.hs
2023-12-03 15:06:39 +00:00

60 lines
1.7 KiB
Haskell

-- SPDX-FileCopyrightText: 2023 David Mosbach <david.mosbach@uniworx.de>
--
-- SPDX-License-Identifier: AGPL-3.0-or-later
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Auth.OAuth2
( AzureUserException(..)
, oauth2MockServer
, mockPluginName
) where
import Data.Text
import Import.NoFoundation
import Yesod.Auth.OAuth2
import Yesod.Auth.OAuth2.Prelude
data AzureUserException = AzureUserError
| AzureUserNoResult
| AzureUserAmbiguous -- TODO
deriving (Show, Eq, Generic)
instance Exception AzureUserException
----------------------------------------
---- OAuth2 development auth plugin ----
----------------------------------------
mockPluginName :: Text
mockPluginName = "uniworx_dev"
newtype UserID = UserID Text
instance FromJSON UserID where
parseJSON = withObject "UserID" $ \o ->
UserID <$> o .: "id"
oauth2MockServer :: YesodAuth m => AuthPlugin m
oauth2MockServer =
let oa = OAuth2
{ oauth2ClientId = "uniworx"
, oauth2ClientSecret = Just "shh"
, oauth2AuthorizeEndpoint = fromString $ mockServerURL <> "/authorize"
, oauth2TokenEndpoint = fromString $ mockServerURL <> "/token"
, oauth2RedirectUri = Nothing
}
mockServerURL = "0.0.0.0/"
profileSrc = fromString $ mockServerURL <> "/foo"
in authOAuth2 mockPluginName oa $ \manager token -> do
(UserID userID, userResponse) <- authGetProfile mockPluginName manager token profileSrc
return Creds
{ credsPlugin = mockPluginName
, credsIdent = userID
, credsExtra = setExtra token userResponse
}