From fe4e9c4214c481d9e3f28100706e62fc70064aaf Mon Sep 17 00:00:00 2001 From: bel Date: Sat, 17 Jun 2023 09:28:16 -0600 Subject: [PATCH] login --- vicuna-tools.d/main.go | 95 +++++++++++++++++++------- vicuna-tools.d/template.d/chatbot.html | 9 +++ vicuna-tools.d/template.d/login.html | 5 ++ 3 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 vicuna-tools.d/template.d/chatbot.html diff --git a/vicuna-tools.d/main.go b/vicuna-tools.d/main.go index cbde59d..9ac8718 100644 --- a/vicuna-tools.d/main.go +++ b/vicuna-tools.d/main.go @@ -3,6 +3,7 @@ package main import ( "context" _ "embed" + "encoding/base64" "encoding/json" "errors" "flag" @@ -11,22 +12,24 @@ import ( "net/http" "os" "os/signal" + "strings" "sync" "syscall" ) var ( Config struct { - Port int - SessionD string - Debug bool - Semaphore sync.Mutex + Port int + SessionD string + Debug bool } //go:embed template.d/login.html htmlLogin []byte //go:embed template.d/index.html htmlIndex []byte + //go:embed template.d/chatbot.html + htmlChatBot []byte ) func main() { @@ -100,30 +103,46 @@ func listenAndServe(ctx context.Context) { func handle(w http.ResponseWriter, r *http.Request) { cookie, _ := ParseCookie(r) if err := _handle(w, r); err != nil { - log.Printf("%s: %s: %v", cookie.Name, r.URL.Path, err) + log.Printf("%s: %s %s: %v", cookie.Name, r.Method, r.URL.Path, err) } else { - log.Printf("%s: %s", cookie.Name, r.URL.Path) + log.Printf("%s: %s %s", cookie.Name, r.Method, r.URL.Path) } } func _handle(w http.ResponseWriter, r *http.Request) error { - switch r.URL.Path { - case "/login": + first := strings.Split(strings.TrimLeft(r.URL.Path, "/"), "/")[0] + switch first { + case "login": return handleLogin(w, r) - case "/": - return handleUI(w, r) default: - http.NotFound(w, r) - return nil + return handleLoggedIn(w, r) } } func handleLogin(w http.ResponseWriter, r *http.Request) error { - w.Write(htmlLogin) - return nil + switch r.Method { + case http.MethodGet: + w.Write(htmlLogin) + return nil + case http.MethodPost: + err := r.ParseForm() + if err != nil { + return err + } + cookie, err := ParseCookie(r) + if err != nil { + return err + } + cookie.Serialize(w) + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + return nil + default: + http.NotFound(w, r) + return errors.New("not found") + } } -func handleUI(w http.ResponseWriter, r *http.Request) error { +func handleLoggedIn(w http.ResponseWriter, r *http.Request) error { cookie, err := ParseCookie(r) if err != nil { http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) @@ -131,10 +150,17 @@ func handleUI(w http.ResponseWriter, r *http.Request) error { } _ = cookie - Config.Semaphore.Lock() - defer Config.Semaphore.Unlock() - - return errors.New("not impl") + switch r.URL.Path { + case "/": + w.Write(htmlIndex) + return nil + case "/chatbot": + w.Write(htmlChatBot) + return nil + default: + http.NotFound(w, r) + return errors.New("not found: " + r.URL.Path) + } } type Cookie struct { @@ -142,28 +168,47 @@ type Cookie struct { } func ParseCookie(r *http.Request) (Cookie, error) { + if r.URL.Path != "/login" { + return parseCookieFromCookie(r) + } + cookie := Cookie{ + Name: r.PostForm.Get("Name"), + } + return cookie, cookie.Verify() +} + +func parseCookieFromCookie(r *http.Request) (Cookie, error) { cookie, err := r.Cookie("root") if err != nil { return Cookie{}, err } - var result Cookie - if err := json.Unmarshal([]byte(cookie.Value), &result); err != nil { + decoded, err := base64.URLEncoding.DecodeString(cookie.Value) + if err != nil { return Cookie{}, err } - if result.Name == "" { - return Cookie{}, errors.New("incomplete cookie") + var result Cookie + if err := json.Unmarshal(decoded, &result); err != nil { + return Cookie{}, err } - return result, nil + return result, result.Verify() +} + +func (cookie Cookie) Verify() error { + if cookie.Name == "" { + return errors.New("incomplete cookie") + } + return nil } func (cookie Cookie) Serialize(w http.ResponseWriter) { b, _ := json.Marshal(cookie) + encoded := base64.URLEncoding.EncodeToString(b) c := &http.Cookie{ Name: "root", - Value: string(b), + Value: encoded, } http.SetCookie(w, c) } diff --git a/vicuna-tools.d/template.d/chatbot.html b/vicuna-tools.d/template.d/chatbot.html new file mode 100644 index 0000000..d00bfd1 --- /dev/null +++ b/vicuna-tools.d/template.d/chatbot.html @@ -0,0 +1,9 @@ + +
+ +
+ + + + diff --git a/vicuna-tools.d/template.d/login.html b/vicuna-tools.d/template.d/login.html index d00bfd1..a567de6 100644 --- a/vicuna-tools.d/template.d/login.html +++ b/vicuna-tools.d/template.d/login.html @@ -3,6 +3,11 @@ +
+ + + +