Try to make it work better
This commit is contained in:
parent
8dd4fca18e
commit
b7b5f7610a
|
@ -10,6 +10,8 @@ This checks those boxes.
|
||||||
|
|
||||||
## Installation with Traefik
|
## 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:
|
I only use docker swarm. You'd do something like the following:
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
|
|
|
@ -23,17 +23,21 @@ var loginHtml []byte
|
||||||
var successHtml []byte
|
var successHtml []byte
|
||||||
|
|
||||||
func rootHandler(w http.ResponseWriter, req *http.Request) {
|
func rootHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
mechanism := "unauthenticated"
|
||||||
authenticated := false
|
authenticated := false
|
||||||
if _, passwd, _ := req.BasicAuth(); passwd == password {
|
if _, passwd, _ := req.BasicAuth(); passwd == password {
|
||||||
authenticated = true
|
authenticated = true
|
||||||
|
mechanism = "HTTP-Basic"
|
||||||
}
|
}
|
||||||
if req.FormValue("passwd") == password {
|
if req.FormValue("passwd") == password {
|
||||||
authenticated = true
|
authenticated = true
|
||||||
|
mechanism = "Form"
|
||||||
}
|
}
|
||||||
if cookie, err := req.Cookie(CookieName); err == nil {
|
if cookie, err := req.Cookie(CookieName); err == nil {
|
||||||
t, _ := token.ParseString(cookie.Value)
|
t, _ := token.ParseString(cookie.Value)
|
||||||
if t.Valid(secret) {
|
if t.Valid(secret) {
|
||||||
authenticated = true
|
authenticated = true
|
||||||
|
mechanism = "Cookie"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +46,7 @@ func rootHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
if clientIP == "" {
|
if clientIP == "" {
|
||||||
clientIP = req.RemoteAddr
|
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 {
|
if authenticated {
|
||||||
t := token.New(secret, time.Now().Add(lifespan))
|
t := token.New(secret, time.Now().Add(lifespan))
|
||||||
|
|
|
@ -1,28 +1,56 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Login</title>
|
<title>Login</title>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<link rel="icon" href="data:,">
|
<link rel="icon" href="data:,">
|
||||||
<style>
|
<style>
|
||||||
html {
|
html {
|
||||||
font-family: sans-serif;
|
font-family: sans-serif;
|
||||||
color: white;
|
color: white;
|
||||||
background: seagreen linear-gradient(315deg, rgba(255,255,255,0.4), transparent);
|
background: seagreen linear-gradient(315deg, rgba(255,255,255,0.4), transparent);
|
||||||
}
|
}
|
||||||
body {
|
body {
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
}
|
}
|
||||||
div {
|
div {
|
||||||
margin: 1em;
|
margin: 1em;
|
||||||
}
|
}
|
||||||
</style>
|
#error {
|
||||||
</head>
|
color: red;
|
||||||
<body>
|
}
|
||||||
<h1>Log In</h1>
|
</style>
|
||||||
<form method="POST">
|
<script>
|
||||||
<div>Password: <input type="password" name="passwd"></div>
|
function error(msg) {
|
||||||
<div><input type="submit"></div>
|
document.querySelector("#error").textContent = msg
|
||||||
</form>
|
}
|
||||||
</body>
|
|
||||||
</html>
|
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>
|
||||||
|
|
Loading…
Reference in New Issue