import Network
import Network.HTTP.Lucu
import Rakka.Environment
+import Rakka.Resource.CheckAuth
import Rakka.Resource.Index
import Rakka.Resource.JavaScript
import Rakka.Resource.PageEntity
resTree :: Environment -> ResTree
resTree env
- = mkResTree [ ([] , resIndex env)
- , (["js" ], javaScript )
- , (["object"], resObject env)
- , (["render"], resRender env)
+ = mkResTree [ ([] , resIndex env)
+ , (["checkAuth"], resCheckAuth env)
+ , (["js" ], javaScript )
+ , (["object" ], resObject env)
+ , (["render" ], resRender env)
]
Rakka.Environment
Rakka.Page
Rakka.Resource
+ Rakka.Resource.CheckAuth
Rakka.Resource.Index
Rakka.Resource.JavaScript
Rakka.Resource.Object
import qualified Codec.Binary.Base64 as B64
import qualified Codec.Binary.UTF8.String as UTF8
import Control.Concurrent.STM
+import Control.Monad.Trans
import qualified Data.Digest.SHA1 as SHA1
import Data.Map (Map)
import qualified Data.Map as M hiding (Map)
}
-isValidPair :: AuthDB -> String -> String -> IO Bool
+isValidPair :: MonadIO m => AuthDB -> String -> String -> m Bool
isValidPair adb name pass
= let hash = SHA1.hash (UTF8.encode pass)
in
- atomically $ do m <- readTVar (adbUserMap adb)
- return (M.lookup name m == Just hash)
+ liftIO $ atomically $ do m <- readTVar (adbUserMap adb)
+ return (M.lookup name m == Just hash)
loadUserMap :: FilePath -> IO UserMap
loadUserMap path
= do exist <- doesFileExist path
- if exist then
- readFile path
- >>=
- return . initMap . M.fromList . map decodePair . fromJust . deserializeStringPairs
- else
- return M.empty
+ m <- if exist then
+ readFile path
+ >>=
+ return . M.fromList . map decodePair . fromJust . deserializeStringPairs
+ else
+ return M.empty
+ return (initMap m)
where
decodePair :: (String, String) -> (String, [Word8])
decodePair (name, b64Hash)
--- /dev/null
+module Rakka.Resource.CheckAuth
+ ( resCheckAuth
+ )
+ where
+
+import Network.HTTP.Lucu
+import Rakka.Authorization
+import Rakka.Environment
+
+
+resCheckAuth :: Environment -> ResourceDef
+resCheckAuth env
+ = ResourceDef {
+ resUsesNativeThread = False
+ , resIsGreedy = False
+ , resGet
+ = Just $
+ do authM <- getAuthorization
+ case authM of
+ Just (BasicAuthCredential userID password)
+ -> do valid <- isValidPair (envAuthDB env) userID password
+ if valid then
+ setStatus NoContent
+ else
+ setStatus Forbidden
+ _ -> setStatus Forbidden
+ , resHead = Nothing
+ , resPost = Nothing
+ , resPut = Nothing
+ , resDelete = Nothing
+ }