redirecting auth code back to client

This commit is contained in:
David Mosbach 2023-12-23 04:53:56 +01:00
parent 390876223e
commit fc77ea3e22
4 changed files with 23 additions and 4 deletions

View File

@ -32,6 +32,7 @@ library
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, bytestring
, containers , containers
, http-client , http-client
, servant , servant
@ -56,6 +57,7 @@ executable oauth2-mock-server-exe
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, bytestring
, containers , containers
, http-client , http-client
, oauth2-mock-server , oauth2-mock-server
@ -82,6 +84,7 @@ test-suite oauth2-mock-server-test
build-depends: build-depends:
aeson aeson
, base >=4.7 && <5 , base >=4.7 && <5
, bytestring
, containers , containers
, http-client , http-client
, oauth2-mock-server , oauth2-mock-server

View File

@ -32,6 +32,7 @@ dependencies:
- stm - stm
- time - time
- transformers - transformers
- bytestring
ghc-options: ghc-options:
- -Wall - -Wall

View File

@ -33,7 +33,7 @@ genUnencryptedCode client url expiration state = do
now <- getCurrentTime now <- getCurrentTime
let let
expiresAt = expiration `addUTCTime` now expiresAt = expiration `addUTCTime` now
simpleCode = client <> url <> show now <> show expiresAt simpleCode = filter (/= ' ') $ client <> url <> show now <> show expiresAt
success <- atomically . stateTVar state $ \s -> success <- atomically . stateTVar state $ \s ->
let mEntry = M.lookup simpleCode s.activeCodes let mEntry = M.lookup simpleCode s.activeCodes
in in

View File

@ -17,8 +17,11 @@ import Control.Monad.IO.Class
import Control.Monad.Trans.Reader import Control.Monad.Trans.Reader
import Data.Aeson import Data.Aeson
import Data.List (find) import Data.ByteString (ByteString (..))
import Data.Text hiding (elem, find, head, map, words) import Data.List (find, elemIndex)
import Data.Maybe (fromMaybe)
import Data.String (IsString (..))
import Data.Text hiding (elem, find, head, length, map, null, splitAt, tail, words)
import Data.Text.Encoding (decodeUtf8) import Data.Text.Encoding (decodeUtf8)
import qualified Data.Map.Strict as Map import qualified Data.Map.Strict as Map
@ -97,7 +100,19 @@ authServer = handleAuth
mAuthCode <- asks (genUnencryptedCode client url 600) >>= liftIO mAuthCode <- asks (genUnencryptedCode client url 600) >>= liftIO
liftIO $ print mAuthCode liftIO $ print mAuthCode
liftIO . putStrLn $ "user: " ++ show u ++ " | scopes: " ++ show (map (showScope @user @userData) scopes') liftIO . putStrLn $ "user: " ++ show u ++ " | scopes: " ++ show (map (showScope @user @userData) scopes')
return uData -- return uData
redirect $ url `withCode` mAuthCode
redirect :: Maybe ByteString -> AuthHandler userData
redirect (Just url) = throwError err302 { errHeaders = [("Location", url)]}
redirect Nothing = throwError err500 { errBody = "Could not generate authorisation code."}
withCode :: String -> Maybe String -> Maybe ByteString
withCode url Nothing = Nothing
withCode url (Just code) =
let qPos = fromMaybe (length url) $ elemIndex '?' url
(pre, post) = splitAt qPos url
post' = if not (null post) then '&' : tail post else post
in Just . fromString $ pre ++ "?authorization_code=" ++ code ++ post'
exampleAuthServer :: AuthServer (Auth User (Map.Map Text Text)) exampleAuthServer :: AuthServer (Auth User (Map.Map Text Text))
exampleAuthServer = authServer exampleAuthServer = authServer