@@ -22,7 +22,7 @@ module AuthSinglePageApp (app, initialServerState) where
22
22
--------------------------------------------------------------------------------
23
23
-- Imports:
24
24
import Control.Monad.Except
25
- import Data.ByteString (ByteString , toStrict )
25
+ import Data.ByteString (ByteString )
26
26
import Data.ByteString.Builder (toLazyByteString )
27
27
import qualified Data.ByteString.Char8 as Char8
28
28
import qualified Data.ByteString.Lazy.Char8 as LChar8
@@ -147,12 +147,12 @@ api = Proxy
147
147
148
148
--------------------------------------------------------------------------------
149
149
app :: MVar ServerState -> Manager -> Provider -> Credentials -> Application
150
- app mvar_serverstate mgr provider creds =
151
- serve api (handlers mvar_serverstate mgr provider creds)
150
+ app mvarServerState mgr provider creds =
151
+ serve api (handlers mvarServerState mgr provider creds)
152
152
153
153
--------------------------------------------------------------------------------
154
154
handlers :: MVar ServerState -> Manager -> Provider -> Credentials -> Server API
155
- handlers mvar_serverstate mgr provider creds =
155
+ handlers mvarServerState mgr provider creds =
156
156
index :<|> login :<|> success :<|> failed :<|> waitForLogin :<|> static
157
157
where
158
158
----------------------------------------------------------------------------
@@ -171,10 +171,10 @@ handlers mvar_serverstate mgr provider creds =
171
171
login = do
172
172
-- If available, request "profile" scope so that we can show user's name
173
173
-- on the page.
174
- let has_profile = case provider & providerDiscovery & scopesSupported of {
174
+ let hasProfile = case provider & providerDiscovery & scopesSupported of {
175
175
Nothing -> False ;
176
176
Just scope -> hasScope scope " profile" }
177
- let scope = if has_profile then openid <> profile else openid
177
+ let scope = if hasProfile then openid <> profile else openid
178
178
let req = defaultAuthenticationRequest scope creds
179
179
r <- liftIO (authenticationRedirect (providerDiscovery provider) req)
180
180
case r of
@@ -207,15 +207,15 @@ handlers mvar_serverstate mgr provider creds =
207
207
-- cookie.
208
208
-- * Send exit signal to close the connection.
209
209
let socketID = decodeUtf8 $ getSocketIDCookie socketIDCookie
210
- serverstate <- liftIO $ readMVar mvar_serverstate
210
+ serverstate <- liftIO $ readMVar mvarServerState
211
211
liftIO $ case Map. lookup socketID serverstate of
212
212
Nothing -> TextIO. putStrLn $
213
213
" No connection for socketID " `append` socketID
214
214
Just (conn, exitSignal) -> do
215
215
WS. sendTextData conn $ afterLoginMessage _token
216
216
putMVar exitSignal ()
217
217
-- Remove this connection from server state.
218
- modifyMVar_ mvar_serverstate (return . Map. delete socketID)
218
+ modifyMVar_ mvarServerState (return . Map. delete socketID)
219
219
220
220
pure . H. docTypeHtml $ do
221
221
H. title " Success!"
@@ -224,14 +224,15 @@ handlers mvar_serverstate mgr provider creds =
224
224
" setTimeout(function() {window.close();}, 1000);"
225
225
where
226
226
afterLoginMessage :: TokenResponse ClaimsSet -> Text
227
- afterLoginMessage tr = the_dict & Aeson. encode & toStrict & decodeUtf8
227
+ afterLoginMessage tr =
228
+ theDict & Aeson. encode & LChar8. toStrict & decodeUtf8
228
229
where
229
- access_tokens = Aeson. toJSON $
230
+ accessTokens = Aeson. toJSON $
230
231
(const Nothing <$> tr :: TokenResponse (Maybe Text ))
231
- id_token = Aeson. toJSON $ idToken tr
232
- the_dict = Map. fromList [
233
- (" access_tokens" , access_tokens ),
234
- (" id_token" , id_token )] :: Map. Map Text Aeson. Value
232
+ idToken_ = Aeson. toJSON $ idToken tr
233
+ theDict = Map. fromList [
234
+ (" access_tokens" , accessTokens ),
235
+ (" id_token" , idToken_ )] :: Map. Map Text Aeson. Value
235
236
236
237
----------------------------------------------------------------------------
237
238
-- Should have been a success, but one or more params are missing.
@@ -249,27 +250,28 @@ handlers mvar_serverstate mgr provider creds =
249
250
waitForLogin = streamData
250
251
where
251
252
streamData :: MonadIO m => WS. Connection -> m ()
252
- streamData conn = do
253
+ streamData conn = liftIO $ do
253
254
-- * Generate and send socket ID to client (to be stored in a cookie).
254
255
-- * Spin off a ping thread that keeps the connection alive.
255
256
-- * Then block until we receive the exit signal from another thread.
256
257
-- Another thread sends the actual login notification to the client.
257
258
-- The connection is automatically closed once this IO action
258
259
-- finishes, which is why a signal must be used to keep it from
259
260
-- finishing prematurely.
260
- socketID <- liftIO $ decodeUtf8 . convertToBase Base64URLUnpadded <$>
261
+ socketID <- decodeUtf8 . convertToBase Base64URLUnpadded <$>
261
262
(getRandomBytes 32 :: IO ByteString )
262
- exitSignal <- liftIO ( newEmptyMVar :: IO (MVar () ))
263
- liftIO $ modifyMVar_ mvar_serverstate $ \ ss ->
263
+ exitSignal <- newEmptyMVar :: IO (MVar () )
264
+ modifyMVar_ mvarServerState $ \ ss ->
264
265
return $ Map. insert socketID (conn, exitSignal) ss
265
- liftIO $ WS. sendTextData conn (socketIDMessage socketID)
266
- liftIO $ WSC. withPingThread conn 30 (return () ) $ do
266
+ WS. sendTextData conn (socketIDMessage socketID)
267
+ WSC. withPingThread conn 30 (return () ) $ do
267
268
takeMVar exitSignal
268
269
269
270
socketIDMessage :: Text -> Text
270
- socketIDMessage socketID = the_dict & Aeson. encode & toStrict & decodeUtf8
271
+ socketIDMessage socketID =
272
+ theDict & Aeson. encode & LChar8. toStrict & decodeUtf8
271
273
where
272
- the_dict = Map. fromList [(" socketID" , socketID)] :: Map. Map Text Text
274
+ theDict = Map. fromList [(" socketID" , socketID)] :: Map. Map Text Text
273
275
274
276
static :: Server Static
275
277
static = serveDirectoryWebApp " static"
0 commit comments