From 22ab0dd01fd2d389959ce33652f3c9b2fab2b20b Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Fri, 26 Apr 2019 12:26:33 -0400 Subject: Add functionality for obtaining auth credentials --- brisket.hy | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/brisket.hy b/brisket.hy index 7e14926..11d0e59 100644 --- a/brisket.hy +++ b/brisket.hy @@ -64,12 +64,31 @@ The lookup order is: (get (get config "Server") "Host")))) (defn get-user-info [host] + "Decode *auth-source* and return the line containing HOST." (let [process (Popen ["gpg" "-q" "-d" *auth-source*] :stdout PIPE) - output (first (.communicate process))] - (for [line output] + output (.decode (first (.communicate process)))] + (for [line (.split output "\n")] (when (.startswith line (.format "machine {}" host)) (return line))))) +(defn get-username [host] + "Return the username for HOST as specified by *auth-source*." + (let [info (get-user-info host) + start-index (.find info "login")] + (unless (= -1 start-index) + (let [sliced (.split (.join "" (drop start-index info)))] + (if (> (len sliced) 1) + (.strip (nth sliced 1) "\"")))))) + +(defn get-password [host] + "Return the password for HOST as specified by *auth-source*." + (let [info (get-user-info host) + start-index (.find info "password")] + (unless (= -1 start-index) + (let [sliced (.split (.join "" (drop start-index info)))] + (if (> (len sliced) 1) + (.strip (nth sliced 1) "\"")))))) + (defn err [&rest args] "Displays ARGS to stdout." (.write stderr (+ (first args) "\n") #* (rest args))) -- cgit v1.3