about summary refs log tree commit diff
path: root/src/LoginAttempts.hs
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-07-28T20·33+0100
committerWilliam Carroll <wpcarro@gmail.com>2020-07-28T20·33+0100
commitcf6c8799ab86278c827d4236a7a89163c61c29b9 (patch)
treefe34a5b41f63a44ff961af2a85a27e625220291d /src/LoginAttempts.hs
parentf051b0be0bc360c949b3b1913f13c4856ae317ca (diff)
Restrict users from multiple failed login attempts
I'm not resetting the failed LoginAttempt count, which is a low priority for
now, but necessary eventually.
Diffstat (limited to 'src/LoginAttempts.hs')
-rw-r--r--src/LoginAttempts.hs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/LoginAttempts.hs b/src/LoginAttempts.hs
new file mode 100644
index 000000000000..a7e950da7412
--- /dev/null
+++ b/src/LoginAttempts.hs
@@ -0,0 +1,29 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+--------------------------------------------------------------------------------
+module LoginAttempts where
+--------------------------------------------------------------------------------
+import Database.SQLite.Simple
+
+import qualified Types as T
+--------------------------------------------------------------------------------
+
+reset :: FilePath -> T.Username -> IO ()
+reset dbFile username = withConnection dbFile $ \conn ->
+  execute conn "UPDATE LoginAttempts SET numAttempts = 0 WHERE username = ?"
+    (Only username)
+
+-- | Attempt to return the number of failed login attempts for
+-- `username`. Returns a Maybe in case `username` doesn't exist.
+forUsername :: FilePath -> T.Username -> IO (Maybe Integer)
+forUsername dbFile username = withConnection dbFile $ \conn -> do
+  res <- query conn "SELECT (numAttempts) FROM LoginAttempts WHERE username = ?"
+    (Only username)
+  case res of
+    [T.LoginAttempt{..}] -> pure (Just loginAttemptNumAttempts)
+    _  -> pure Nothing
+
+increment :: FilePath -> T.Username -> IO ()
+increment dbFile username = withConnection dbFile $ \conn ->
+  execute conn "UPDATE LoginAttempts SET numAttempts = numAttempts + 1 WHERE username = ?"
+    (Only username)