Commits

Luke Plant  committed a608b96

Utility function to return username of logged in user.

  • Participants
  • Parent commits 3495320

Comments (0)

Files changed (1)

File src/Blog/Views.hs

 import Blog.Utils (getTimestamp)
 import Ella.Framework (default404, View)
 import Ella.GenUtils (utf8, with, exactParse)
-import Ella.Param (captureOrDefault)
+import Ella.Param (captureOrDefault, capture)
 import Ella.Request
 import Ella.Response
 import Ella.Utils (addHtml)
     _ -> do
       return $ Just $ standardResponse $ loginPage emptyLoginData Map.empty
 
+-- Authorisation
+
 standardCookie = Cookie { cookieName = ""
                         , cookieValue = ""
                         , cookieExpires = Nothing
      ]
 
 
+timeout = 3600 * 24 * 10 -- 10 days
+
+-- | Return the username if logged in, otherwise Nothing
+--
+-- Relies on secure cookies middleware
+getCredentials :: Request -> IO (Maybe String)
+getCredentials req = do
+  current_ts <- getTimestamp
+  return $ do
+    username <- getCookieVal req "username"
+    timestamp <- getCookieVal req "timestamp" >>= capture
+    if timestamp + timeout > current_ts
+      then Just username
+      else Nothing
+
+
 -- Utilities
 
 getPage req = (getGET req "p") `captureOrDefault` 1 :: Int