2012-07-26 08:50:09 +00:00
|
|
|
{- Yesod webapp
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-07-26 08:50:09 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings, CPP, RankNTypes #-}
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
module Utility.WebApp where
|
|
|
|
|
|
|
|
import Common
|
2013-01-03 22:50:30 +00:00
|
|
|
import Utility.TempFile
|
|
|
|
import Utility.FileMode
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-08-30 17:05:39 +00:00
|
|
|
import qualified Yesod
|
2012-07-26 08:50:09 +00:00
|
|
|
import qualified Network.Wai as Wai
|
2012-07-26 01:26:13 +00:00
|
|
|
import Network.Wai.Handler.Warp
|
|
|
|
import Network.Wai.Logger
|
|
|
|
import Control.Monad.IO.Class
|
|
|
|
import Network.HTTP.Types
|
|
|
|
import System.Log.Logger
|
|
|
|
import Data.ByteString.Lazy.UTF8
|
2012-07-26 08:50:09 +00:00
|
|
|
import qualified Data.CaseInsensitive as CI
|
2012-07-26 01:26:13 +00:00
|
|
|
import Network.Socket
|
|
|
|
import Control.Exception
|
2012-07-26 07:38:20 +00:00
|
|
|
import Crypto.Random
|
|
|
|
import Data.Digest.Pure.SHA
|
2012-07-26 16:41:20 +00:00
|
|
|
import qualified Web.ClientSession as CS
|
2012-07-26 08:50:09 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2012-08-09 17:33:04 +00:00
|
|
|
import qualified Data.ByteString as B
|
2012-07-26 08:50:09 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import Blaze.ByteString.Builder.Char.Utf8 (fromText)
|
|
|
|
import Blaze.ByteString.Builder (Builder)
|
|
|
|
import Data.Monoid
|
|
|
|
import Control.Arrow ((***))
|
2012-07-27 19:33:24 +00:00
|
|
|
import Control.Concurrent
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
localhost :: String
|
|
|
|
localhost = "localhost"
|
|
|
|
|
2013-01-15 17:34:59 +00:00
|
|
|
{- Command to use to run a web browser. -}
|
|
|
|
browserCommand :: FilePath
|
2012-09-29 18:49:15 +00:00
|
|
|
#ifdef darwin_HOST_OS
|
2013-01-15 17:34:59 +00:00
|
|
|
browserCommand = "open"
|
2012-07-26 01:26:13 +00:00
|
|
|
#else
|
2013-01-15 17:34:59 +00:00
|
|
|
browserCommand = "xdg-open"
|
2012-07-26 01:26:13 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
{- Binds to a socket on localhost, and runs a webapp on it.
|
|
|
|
-
|
2013-01-10 03:17:52 +00:00
|
|
|
- An IO action can also be run, to do something with the address,
|
2012-07-26 01:26:13 +00:00
|
|
|
- such as start a web browser to view the webapp.
|
2012-07-27 19:33:24 +00:00
|
|
|
-}
|
2013-01-10 03:17:52 +00:00
|
|
|
runWebApp :: Wai.Application -> (SockAddr -> IO ()) -> IO ()
|
2012-07-26 01:26:13 +00:00
|
|
|
runWebApp app observer = do
|
|
|
|
sock <- localSocket
|
2012-07-27 19:33:24 +00:00
|
|
|
void $ forkIO $ runSettingsSocket defaultSettings sock app
|
2013-01-10 03:17:52 +00:00
|
|
|
observer =<< getSocketName sock
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
{- Binds to a local socket, selecting any free port.
|
2012-09-18 21:19:41 +00:00
|
|
|
-
|
|
|
|
- Prefers to bind to the ipv4 address rather than the ipv6 address
|
|
|
|
- of localhost, if it's available.
|
2012-07-26 01:26:13 +00:00
|
|
|
-
|
|
|
|
- As a (very weak) form of security, only connections from
|
|
|
|
- localhost are accepted. -}
|
|
|
|
localSocket :: IO Socket
|
|
|
|
localSocket = do
|
|
|
|
addrs <- getAddrInfo (Just hints) (Just localhost) Nothing
|
2012-09-18 21:19:41 +00:00
|
|
|
case (partition (\a -> addrFamily a == AF_INET) addrs) of
|
|
|
|
(v4addr:_, _) -> go v4addr
|
|
|
|
(_, v6addr:_) -> go v6addr
|
|
|
|
_ -> error "unable to bind to a local socket"
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
hints = defaultHints
|
|
|
|
{ addrFlags = [AI_ADDRCONFIG]
|
|
|
|
, addrSocketType = Stream
|
|
|
|
}
|
|
|
|
{- Repeated attempts because bind sometimes fails for an
|
|
|
|
- unknown reason on OSX. -}
|
|
|
|
go addr = go' 100 addr
|
|
|
|
go' :: Int -> AddrInfo -> IO Socket
|
|
|
|
go' 0 _ = error "unable to bind to local socket"
|
|
|
|
go' n addr = do
|
|
|
|
r <- tryIO $ bracketOnError (open addr) sClose (use addr)
|
|
|
|
either (const $ go' (pred n) addr) return r
|
|
|
|
open addr = socket (addrFamily addr) (addrSocketType addr) (addrProtocol addr)
|
|
|
|
use addr sock = do
|
|
|
|
setSocketOption sock ReuseAddr 1
|
|
|
|
bindSocket sock (addrAddress addr)
|
|
|
|
listen sock maxListenQueue
|
|
|
|
return sock
|
2012-07-26 01:26:13 +00:00
|
|
|
|
|
|
|
{- Checks if debugging is actually enabled. -}
|
|
|
|
debugEnabled :: IO Bool
|
|
|
|
debugEnabled = do
|
|
|
|
l <- getRootLogger
|
|
|
|
return $ getLevel l <= Just DEBUG
|
|
|
|
|
|
|
|
{- WAI middleware that logs using System.Log.Logger at debug level.
|
|
|
|
-
|
|
|
|
- Recommend only inserting this middleware when debugging is actually
|
|
|
|
- enabled, as it's not optimised at all.
|
|
|
|
-}
|
2012-07-26 08:50:09 +00:00
|
|
|
httpDebugLogger :: Wai.Middleware
|
2012-07-26 01:26:13 +00:00
|
|
|
httpDebugLogger waiApp req = do
|
|
|
|
logRequest req
|
|
|
|
waiApp req
|
|
|
|
|
2012-07-26 08:50:09 +00:00
|
|
|
logRequest :: MonadIO m => Wai.Request -> m ()
|
2012-07-26 01:26:13 +00:00
|
|
|
logRequest req = do
|
|
|
|
liftIO $ debugM "WebApp" $ unwords
|
2012-07-26 08:50:09 +00:00
|
|
|
[ showSockAddr $ Wai.remoteHost req
|
|
|
|
, frombs $ Wai.requestMethod req
|
|
|
|
, frombs $ Wai.rawPathInfo req
|
|
|
|
--, show $ Wai.httpVersion req
|
2012-07-26 01:26:13 +00:00
|
|
|
--, frombs $ lookupRequestField "referer" req
|
|
|
|
, frombs $ lookupRequestField "user-agent" req
|
|
|
|
]
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
frombs v = toString $ L.fromChunks [v]
|
2012-07-26 01:26:13 +00:00
|
|
|
|
2012-08-09 17:33:04 +00:00
|
|
|
lookupRequestField :: CI.CI B.ByteString -> Wai.Request -> B.ByteString
|
2012-07-26 08:50:09 +00:00
|
|
|
lookupRequestField k req = fromMaybe "" . lookup k $ Wai.requestHeaders req
|
2012-07-26 07:38:20 +00:00
|
|
|
|
2012-07-26 16:41:20 +00:00
|
|
|
{- Rather than storing a session key on disk, use a random key
|
|
|
|
- that will only be valid for this run of the webapp. -}
|
2012-08-30 17:05:39 +00:00
|
|
|
webAppSessionBackend :: Yesod.Yesod y => y -> IO (Maybe (Yesod.SessionBackend y))
|
2012-07-26 16:41:20 +00:00
|
|
|
webAppSessionBackend _ = do
|
|
|
|
g <- newGenIO :: IO SystemRandom
|
|
|
|
case genBytes 96 g of
|
|
|
|
Left e -> error $ "failed to generate random key: " ++ show e
|
|
|
|
Right (s, _) -> case CS.initKey s of
|
|
|
|
Left e -> error $ "failed to initialize key: " ++ show e
|
|
|
|
Right key -> return $ Just $
|
2012-08-30 17:05:39 +00:00
|
|
|
Yesod.clientSessionBackend key 120
|
2012-07-26 16:41:20 +00:00
|
|
|
|
|
|
|
{- Generates a random sha512 string, suitable to be used for an
|
2012-07-26 07:38:20 +00:00
|
|
|
- authentication secret. -}
|
|
|
|
genRandomToken :: IO String
|
|
|
|
genRandomToken = do
|
|
|
|
g <- newGenIO :: IO SystemRandom
|
|
|
|
return $
|
|
|
|
case genBytes 512 g of
|
|
|
|
Left e -> error $ "failed to generate secret token: " ++ show e
|
|
|
|
Right (s, _) -> showDigest $ sha512 $ L.fromChunks [s]
|
2012-07-26 08:50:09 +00:00
|
|
|
|
|
|
|
{- A Yesod isAuthorized method, which checks the auth cgi parameter
|
2012-07-29 16:12:14 +00:00
|
|
|
- against a token extracted from the Yesod application.
|
|
|
|
-
|
|
|
|
- Note that the usual Yesod error page is bypassed on error, to avoid
|
|
|
|
- possibly leaking the auth token in urls on that page!
|
|
|
|
-}
|
2012-08-30 17:05:39 +00:00
|
|
|
checkAuthToken :: forall t sub. (t -> T.Text) -> Yesod.GHandler sub t Yesod.AuthResult
|
2012-07-26 08:50:09 +00:00
|
|
|
checkAuthToken extractToken = do
|
2012-08-30 17:05:39 +00:00
|
|
|
webapp <- Yesod.getYesod
|
|
|
|
req <- Yesod.getRequest
|
|
|
|
let params = Yesod.reqGetParams req
|
2012-07-26 09:13:27 +00:00
|
|
|
if lookup "auth" params == Just (extractToken webapp)
|
2012-08-30 17:05:39 +00:00
|
|
|
then return Yesod.Authorized
|
|
|
|
else Yesod.sendResponseStatus unauthorized401 ()
|
2012-07-26 08:50:09 +00:00
|
|
|
|
|
|
|
{- A Yesod joinPath method, which adds an auth cgi parameter to every
|
|
|
|
- url matching a predicate, containing a token extracted from the
|
|
|
|
- Yesod application.
|
|
|
|
-
|
|
|
|
- A typical predicate would exclude files under /static.
|
|
|
|
-}
|
|
|
|
insertAuthToken :: forall y. (y -> T.Text)
|
|
|
|
-> ([T.Text] -> Bool)
|
|
|
|
-> y
|
|
|
|
-> T.Text
|
|
|
|
-> [T.Text]
|
|
|
|
-> [(T.Text, T.Text)]
|
|
|
|
-> Builder
|
|
|
|
insertAuthToken extractToken predicate webapp root pathbits params =
|
|
|
|
fromText root `mappend` encodePath pathbits' encodedparams
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
pathbits' = if null pathbits then [T.empty] else pathbits
|
|
|
|
encodedparams = map (TE.encodeUtf8 *** go) params'
|
|
|
|
go "" = Nothing
|
|
|
|
go x = Just $ TE.encodeUtf8 x
|
|
|
|
authparam = (T.pack "auth", extractToken webapp)
|
|
|
|
params'
|
|
|
|
| predicate pathbits = authparam:params
|
|
|
|
| otherwise = params
|
2013-01-03 22:50:30 +00:00
|
|
|
|
|
|
|
{- Creates a html shim file that's used to redirect into the webapp,
|
|
|
|
- to avoid exposing the secret token when launching the web browser. -}
|
|
|
|
writeHtmlShim :: String -> String -> FilePath -> IO ()
|
|
|
|
writeHtmlShim title url file = viaTmp writeFileProtected file $ genHtmlShim title url
|
|
|
|
|
|
|
|
{- TODO: generate this static file using Yesod. -}
|
|
|
|
genHtmlShim :: String -> String -> String
|
|
|
|
genHtmlShim title url = unlines
|
|
|
|
[ "<html>"
|
|
|
|
, "<head>"
|
|
|
|
, "<title>"++ title ++ "</title>"
|
|
|
|
, "<meta http-equiv=\"refresh\" content=\"1; URL="++url++"\">"
|
|
|
|
, "<body>"
|
|
|
|
, "<p>"
|
|
|
|
, "<a href=\"" ++ url ++ "\">" ++ title ++ "</a>"
|
|
|
|
, "</p>"
|
|
|
|
, "</body>"
|
|
|
|
, "</html>"
|
|
|
|
]
|