From 4fd621813978bb999f1b1603f6c5a459d7a15628 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Sat, 11 Apr 2020 14:44:52 -0700 Subject: Slightly better auth handling --- Run/Que/Website.hs | 65 +++++++++++++++++++++++++++++++----------------------- Run/Que/client.py | 24 +++++++++++--------- 2 files changed, 51 insertions(+), 38 deletions(-) (limited to 'Run') diff --git a/Run/Que/Website.hs b/Run/Que/Website.hs index 5e2f4d6..23e3740 100644 --- a/Run/Que/Website.hs +++ b/Run/Que/Website.hs @@ -35,26 +35,33 @@ main :: IO () main = do (src, ns) <- Environment.getArgs >>= \case [src] -> return (src, "_") -- default to _ ns which is special - [src, ns] -> return (src, ns) + [src, ns] -> return (src, Text.pack ns) _ -> Exit.die "usage: que-website [namespace]" + mKey <- getKey ns + putText $ "serving " <> Text.pack src <> " at " <> ns + run mKey ns $ Sources { index = src "index.md" + , client = src "client.py" + , quescripts = src "quescripts.md" + , style = src "style.css" + , apidocs = src "apidocs.md" + , tutorial = src "tutorial.md" + } + +getKey :: Namespace -> IO (Maybe Key) +getKey ns = do home <- Directory.getHomeDirectory - conf <- Text.readFile <| home ".config" "que.conf" - let (Auth _ key) = - either needConf identity - <| Config.parseIniFile conf - <| auth - <| Text.pack ns - putStrLn $ "serving " ++ src ++ " at " ++ ns - run key (Text.pack ns) $ Sources { index = src "index.md" - , client = src "client.py" - , quescripts = src "quescripts.md" - , style = src "style.css" - , apidocs = src "apidocs.md" - , tutorial = src "tutorial.md" - } + let file = home ".config" "que.conf" + exists <- (Directory.doesFileExist file) + unless exists <| panic <| "not found: " <> Text.pack file + conf <- Text.readFile file + print (home ".config" "que.conf") + auth ns + |> Config.parseIniFile conf + |> either errorParsingConf identity + |> return -needConf :: error -needConf = panic "you need a ~/.config/que.conf" +errorParsingConf :: error +errorParsingConf = panic "could not parse ~/.config/que.conf" data Sources = Sources { index :: FilePath @@ -68,14 +75,11 @@ data Sources = Sources type Namespace = Text type Key = Text -data Auth = Auth Namespace Key - -auth :: Text -> Config.IniParser Auth -auth ns = Config.section ns $ do - key <- Config.field "key" - return <| Auth ns key +auth :: Namespace -> Config.IniParser (Maybe Key) +auth "pub" = pure Nothing +auth ns = Config.sectionMb ns <| Config.field "key" -run :: Key -> Text -> Sources -> IO () +run :: Maybe Key -> Text -> Sources -> IO () run key ns Sources {..} = Async.runConcurrently actions >> return () where actions = traverse @@ -103,9 +107,16 @@ run key ns Sources {..} = Async.runConcurrently actions >> return () ] [] --- TODO: recover from 502 errors -serve :: Key -> Namespace -> Text -> ByteString -> IO () -serve key ns path content = runReq defaultHttpConfig $ do +serve :: Maybe Key -> Namespace -> Text -> ByteString -> IO () +serve Nothing "pub" path content = runReq defaultHttpConfig $ do + _ <- req POST + (http "que.run" /: "pub" /: path) + (ReqBodyBs content) + ignoreResponse + mempty + liftIO $ return () +serve Nothing p _ _ = panic <| "no auth key provided for ns: " <> p +serve (Just key) ns path content = runReq defaultHttpConfig $ do let options = header "Authorization" (encodeUtf8 key) <> responseTimeout maxBound _ <- req POST diff --git a/Run/Que/client.py b/Run/Que/client.py index cd24618..4cf5d05 100755 --- a/Run/Que/client.py +++ b/Run/Que/client.py @@ -16,19 +16,22 @@ import urllib.request as request MAX_TIMEOUT = 99999999 # basically never timeout -def auth(): - conf_file = os.path.expanduser("~/.config/que.conf") - if not os.path.exists(conf_file): - sys.exit("you need a ~/.config/que.conf") - cfg = configparser.ConfigParser() - cfg.read(conf_file) - return cfg +def auth(args): + ns = args.target.split("/")[0] + if ns == "pub": + return None + else: + conf_file = os.path.expanduser("~/.config/que.conf") + if not os.path.exists(conf_file): + sys.exit("you need a ~/.config/que.conf") + cfg = configparser.ConfigParser() + cfg.read(conf_file) + return cfg[ns]["key"] def send(args): "Send a message to the que." - ns = args.target.split("/")[0] - key = auth()[ns]["key"] + key = auth(args) data = args.infile.read().encode("utf-8").strip() req = request.Request(f"{args.host}/{args.target}") req.add_header("User-AgenT", "Que/Client") @@ -44,8 +47,7 @@ def send(args): def recv(args): "Receive a message from the que." - ns = args.target.split("/")[0] - key = auth()[ns]["key"] + key = auth(args) def _recv(_req): msg = _req.readline().decode("utf-8").strip() -- cgit v1.2.3