Try to make it work better

This commit is contained in:
Neale Pickett 2022-03-05 17:46:12 -07:00
parent 8dd4fca18e
commit b7b5f7610a
3 changed files with 61 additions and 27 deletions

View File

@ -10,6 +10,8 @@ This checks those boxes.
## Installation with Traefik
You need to have traefik forward the Path `/` to this application.
I only use docker swarm. You'd do something like the following:
```yaml

View File

@ -23,17 +23,21 @@ var loginHtml []byte
var successHtml []byte
func rootHandler(w http.ResponseWriter, req *http.Request) {
mechanism := "unauthenticated"
authenticated := false
if _, passwd, _ := req.BasicAuth(); passwd == password {
authenticated = true
mechanism = "HTTP-Basic"
}
if req.FormValue("passwd") == password {
authenticated = true
mechanism = "Form"
}
if cookie, err := req.Cookie(CookieName); err == nil {
t, _ := token.ParseString(cookie.Value)
if t.Valid(secret) {
authenticated = true
mechanism = "Cookie"
}
}
@ -42,7 +46,7 @@ func rootHandler(w http.ResponseWriter, req *http.Request) {
if clientIP == "" {
clientIP = req.RemoteAddr
}
log.Println(clientIP, req.Method, req.URL, "authenticated:", authenticated)
log.Printf("%s %s %s [%s]", clientIP, req.Method, req.URL, mechanism)
if authenticated {
t := token.New(secret, time.Now().Add(lifespan))

View File

@ -1,28 +1,56 @@
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {
font-family: sans-serif;
color: white;
background: seagreen linear-gradient(315deg, rgba(255,255,255,0.4), transparent);
}
body {
height: 100vh;
}
div {
margin: 1em;
}
</style>
</head>
<body>
<h1>Log In</h1>
<form method="POST">
<div>Password: <input type="password" name="passwd"></div>
<div><input type="submit"></div>
</form>
</body>
</html>
<head>
<title>Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {
font-family: sans-serif;
color: white;
background: seagreen linear-gradient(315deg, rgba(255,255,255,0.4), transparent);
}
body {
height: 100vh;
}
div {
margin: 1em;
}
#error {
color: red;
}
</style>
<script>
function error(msg) {
document.querySelector("#error").textContent = msg
}
async function login(evt) {
evt.preventDefault()
let req = await fetch(evt.target.action, {
method: evt.target.method,
body: new FormData(evt.target),
credentials: "same-origin",
})
if (! req.ok) {
error(req.statusText || "Authentication failed")
return
}
window.location = window.location
}
function init() {
document.querySelector("form").addEventListener("submit", login)
}
window.addEventListener("load", init)
</script>
</head>
<body>
<h1>Log In</h1>
<form action="/" method="post">
<div>Password: <input type="password" name="passwd"></div>
<div><input type="submit" value="Log In"></div>
</form>
<div id="error"></div>
</body>
</html>