REST and other improvements #27

Merged
amigan merged 6 commits from rest into trunk 2024-11-06 20:50:41 -05:00
4 changed files with 38 additions and 9 deletions
Showing only changes of commit c1a258f63c - Show all commits

View file

@ -57,7 +57,7 @@ func main() {
loginForm.Add("username", *username)
loginForm.Add("password", *password)
loginReq, err := http.NewRequest("POST", "http"+secureSuffix()+"://"+*addr+"/login", strings.NewReader(loginForm.Encode()))
loginReq, err := http.NewRequest("POST", "http"+secureSuffix()+"://"+*addr+"/api/login", strings.NewReader(loginForm.Encode()))
if err != nil {
log.Fatal(err)
}

View file

@ -4,7 +4,10 @@ import (
"errors"
"net/http"
_ "embed"
"dynatron.me/x/stillbox/pkg/config"
"github.com/go-chi/chi/v5"
"github.com/go-chi/jwtauth/v5"
)
@ -66,3 +69,20 @@ func ErrorResponse(w http.ResponseWriter, err error) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func (a *Auth) PublicRoutes(r chi.Router) {
r.Post("/api/login", a.routeAuth)
r.Get("/api/login", a.routeLogin)
}
func (a *Auth) PrivateRoutes(r chi.Router) {
r.Get("/refresh", a.routeRefresh)
}
//go:embed login.html
var loginPage []byte
func (a *Auth) routeLogin(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
_, _ = w.Write(loginPage)
}

View file

@ -110,14 +110,6 @@ func (a *Auth) newToken(uid int32) string {
return tokenString
}
func (a *Auth) PublicRoutes(r chi.Router) {
r.Post("/login", a.routeAuth)
}
func (a *Auth) PrivateRoutes(r chi.Router) {
r.Get("/refresh", a.routeRefresh)
}
func (a *Auth) allowInsecureCookie(r *http.Request) bool {
host := strings.Split(r.Host, ":")
v, has := a.cfg.AllowInsecure[host[0]]

17
pkg/auth/login.html Normal file
View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div>
<form action="/login" method="POST">
<label for="username">Username: </label>
<input type="text" name="username" />
<label for="password">Password: </label>
<input type="password" name="password" />
<input type="submit" value="Login" />
</form>
</div>
</body>
</html>