Add session management for user authentication
This commit is contained in:
parent
e235e3d8a8
commit
ab662bd4b8
3 changed files with 45 additions and 4 deletions
28
auth/auth.go
28
auth/auth.go
|
|
@ -4,6 +4,7 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed login.html
|
//go:embed login.html
|
||||||
|
|
@ -13,6 +14,11 @@ var loginPage string
|
||||||
var wrongPassword string
|
var wrongPassword string
|
||||||
|
|
||||||
type Authenticator struct {
|
type Authenticator struct {
|
||||||
|
sessions map[string]token
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() Authenticator {
|
||||||
|
return Authenticator{sessions: make(map[string]token)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
|
func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -34,9 +40,16 @@ func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprint(w, wrongPassword)
|
fmt.Fprint(w, wrongPassword)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
key := generateKey()
|
||||||
|
a.sessions[key] = token{userName: login, created: time.Now(), lastAccess: time.Now()}
|
||||||
c := http.Cookie{
|
c := http.Cookie{
|
||||||
Name: "token",
|
Name: "token",
|
||||||
Value: "good",
|
Value: key,
|
||||||
|
HttpOnly: true,
|
||||||
|
// #todo (in prod) uncomment this line
|
||||||
|
//Secure: true,
|
||||||
|
SameSite: http.SameSiteStrictMode,
|
||||||
|
Path: "/",
|
||||||
}
|
}
|
||||||
http.SetCookie(w, &c)
|
http.SetCookie(w, &c)
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
|
|
@ -48,11 +61,18 @@ func (a *Authenticator) Authenticate(in http.HandlerFunc) http.HandlerFunc {
|
||||||
a.login(w, r)
|
a.login(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if _, err := r.Cookie("token"); err != nil {
|
c, err := r.Cookie("token")
|
||||||
fmt.Println("user not logged in")
|
if err != nil {
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
value, ok := a.sessions[c.Value]
|
||||||
|
if !ok || time.Since(value.lastAccess) > time.Hour*24 || time.Since(value.lastAccess) > time.Hour*240 {
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
value.lastAccess = time.Now()
|
||||||
|
a.sessions[c.Value] = value
|
||||||
in(w, r)
|
in(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
auth/token.go
Normal file
19
auth/token.go
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type token struct {
|
||||||
|
userName string
|
||||||
|
created time.Time
|
||||||
|
lastAccess time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateKey() string {
|
||||||
|
key := [32]byte{}
|
||||||
|
rand.Read(key[:])
|
||||||
|
return base64.StdEncoding.EncodeToString(key[:])
|
||||||
|
}
|
||||||
2
main.go
2
main.go
|
|
@ -25,6 +25,8 @@ func main() {
|
||||||
log.Fatal("can't open the databse", err)
|
log.Fatal("can't open the databse", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a = auth.New()
|
||||||
|
|
||||||
http.HandleFunc("GET /order/", logger(a.Authenticate(h.form)))
|
http.HandleFunc("GET /order/", logger(a.Authenticate(h.form)))
|
||||||
http.HandleFunc("GET /", logger(a.Authenticate(h.index)))
|
http.HandleFunc("GET /", logger(a.Authenticate(h.index)))
|
||||||
http.HandleFunc("POST /", logger(a.Authenticate(h.addOrder)))
|
http.HandleFunc("POST /", logger(a.Authenticate(h.addOrder)))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue