Skip to content

Instantly share code, notes, and snippets.

@tazjin
Created June 28, 2014 14:18
Show Gist options
  • Save tazjin/e1619615f5c902bf8706 to your computer and use it in GitHub Desktop.
Save tazjin/e1619615f5c902bf8706 to your computer and use it in GitHub Desktop.
Passwords without whitespace!
{-# LANGUAGE OverloadedStrings #-}
import Crypto.BCrypt
import Control.Monad.IO.Class
import Data.Text.Lazy
import qualified Data.Text.Encoding as TE
import Data.Maybe (fromJust) -- oh yeah
import Data.Monoid (mconcat)
import Web.Scotty
{- Allow people to enter passwords but don't allow spaces!
I honestly don't know how this just happens as a side-effect
of something. -}
main :: IO ()
main = scotty 3000 $ do
get "/" $ html "<form method=\"post\"><input type=\"password\" name=\"pw\"></form>"
post "/" $ do
pw <- param "pw"
if isInsecure pw
then do liftIO $ (hash pw >>= storeToDB)
html $ mconcat ["Your password ", pw, " has been successfully stored!"]
else html "Your password is too secure"
-- This is where the magic happens!
isInsecure :: Text -> Bool
isInsecure = not . isInfixOf " "
hash :: Text -> IO Text
hash = fmap (fromStrict . TE.decodeUtf8 . fromJust)
. hashPasswordUsingPolicy slowerBcryptHashingPolicy
. TE.encodeUtf8
. toStrict
-- dummies
storeToDB :: Text -> IO ()
storeToDB hash = putStrLn $ "Stored hash in DB: " ++ unpack hash
name: pwspaces
version: 0.1.0.0
author: Vincent Ambo
maintainer: tazjin@gmail.com
build-type: Simple
cabal-version: >=1.10
executable pwspaces
main-is: Main.hs
default-language: Haskell2010
build-depends: base >=4.7 && <4.8,
scotty,
bcrypt,
text,
transformers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment