60 lines
1.7 KiB
Haskell
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 = "dev-oauth2-mock"
|
|
|
|
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 = "42"
|
|
, oauth2ClientSecret = Just "shhh"
|
|
, oauth2AuthorizeEndpoint = (fromString $ mockServerURL <> "/auth") `withQuery` [scopeParam " " ["ID", "Profile"]]
|
|
, oauth2TokenEndpoint = fromString $ mockServerURL <> "/token"
|
|
, oauth2RedirectUri = Nothing
|
|
}
|
|
mockServerURL = "http://localhost:9443"
|
|
profileSrc = fromString $ mockServerURL <> "/users/me"
|
|
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
|
|
}
|
|
|
|
|