diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-12 21:50:56 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-12 21:50:56 -0800 |
| commit | 42f1b4de384bcbbdab3b80d8e5cc53b36fcffd50 (patch) | |
| tree | 3a5aab90607131231ec68367f8cc00425d7dc516 /web/web.go | |
| parent | 9db2500fb340ef304c0f15f4379bc33589df9a63 (diff) | |
| download | neko-42f1b4de384bcbbdab3b80d8e5cc53b36fcffd50.tar.gz neko-42f1b4de384bcbbdab3b80d8e5cc53b36fcffd50.tar.bz2 neko-42f1b4de384bcbbdab3b80d8e5cc53b36fcffd50.zip | |
Implement frontend login logic with >90% coverage
Diffstat (limited to 'web/web.go')
| -rw-r--r-- | web/web.go | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -143,6 +143,43 @@ func serveBoxedFile(w http.ResponseWriter, r *http.Request, filename string) { http.ServeContent(w, r, filename, fi.ModTime(), ui) } +func apiLoginHandler(w http.ResponseWriter, r *http.Request) { + username := r.FormValue("username") + password := r.FormValue("password") + + // support JSON body as well + if username == "" && password == "" { + // try parsing json + /* + type loginReq struct { + Username string `json:"username"` + Password string `json:"password"` + } + // left as todo for now as we can use form data from fetch too + */ + } + + if password == config.Config.DigestPassword { + v, _ := bcrypt.GenerateFromPassword([]byte(password), 0) + c := http.Cookie{Name: AuthCookie, Value: string(v), Path: "/", MaxAge: SecondsInAYear, HttpOnly: false} + http.SetCookie(w, &c) + w.Header().Set("Content-Type", "application/json") + fmt.Fprintf(w, `{"status":"ok"}`) + } else { + http.Error(w, `{"status":"error", "message":"bad login"}`, 401) + } +} + +func apiAuthStatusHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if Authenticated(r) { + fmt.Fprintf(w, `{"status":"ok", "authenticated":true}`) + } else { + w.WriteHeader(http.StatusUnauthorized) + fmt.Fprintf(w, `{"status":"error", "authenticated":false}`) + } +} + func Serve() { box := rice.MustFindBox("../static") staticFileServer := http.StripPrefix("/static/", http.FileServer(box.HTTPBox())) @@ -167,6 +204,8 @@ func Serve() { http.HandleFunc("/login/", loginHandler) http.HandleFunc("/logout/", logoutHandler) + http.HandleFunc("/api/login", apiLoginHandler) + http.HandleFunc("/api/auth", apiAuthStatusHandler) http.HandleFunc("/", AuthWrap(indexHandler)) |
