]> gitweb @ CieloNegro.org - Rakka.git/commitdiff
implemented /checkAuth
authorpho <pho@cielonegro.org>
Thu, 10 Jan 2008 10:19:40 +0000 (19:19 +0900)
committerpho <pho@cielonegro.org>
Thu, 10 Jan 2008 10:19:40 +0000 (19:19 +0900)
darcs-hash:20080110101940-62b54-b098f5008f655a3b661d051fde55e08eedd0b8d4.gz

Main.hs
Rakka.cabal
Rakka/Authorization.hs
Rakka/Resource/CheckAuth.hs [new file with mode: 0644]

diff --git a/Main.hs b/Main.hs
index b3313d68fd29ffb456b05d627e500e903d4a2f0d..77c17a16a4b5c212b9268e22a827baf456f2d8a4 100644 (file)
--- a/Main.hs
+++ b/Main.hs
@@ -5,6 +5,7 @@ import           Data.Maybe
 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
@@ -136,10 +137,11 @@ main = withSubversion $
           
 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)
                 ]
 
 
index 17da185be9cc33f76b2e617319cb9fef7cac8b02..da9c403d07375ad74d946c64f3088df6f3f496a0 100644 (file)
@@ -53,6 +53,7 @@ Executable rakka
         Rakka.Environment
         Rakka.Page
         Rakka.Resource
+        Rakka.Resource.CheckAuth
         Rakka.Resource.Index
         Rakka.Resource.JavaScript
         Rakka.Resource.Object
index 8da8afe95a590ca44552084ee92f9a95672d17db..8f32ddfc613420e005e898e24ac08be920cbee0e 100644 (file)
@@ -8,6 +8,7 @@ module Rakka.Authorization
 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)
@@ -42,23 +43,24 @@ mkAuthDB lsdir
                     }
 
 
-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)
diff --git a/Rakka/Resource/CheckAuth.hs b/Rakka/Resource/CheckAuth.hs
new file mode 100644 (file)
index 0000000..8b4d54b
--- /dev/null
@@ -0,0 +1,31 @@
+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
+      }