removed auth - wil only be used through a vpn
This commit is contained in:
parent
d69d8c3857
commit
6df75a7ce6
7 changed files with 2 additions and 230 deletions
49
auth/auth.go
49
auth/auth.go
|
|
@ -1,49 +0,0 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/store"
|
||||
)
|
||||
|
||||
type Authenticator struct {
|
||||
sessions map[string]models.Token
|
||||
s *store.Store
|
||||
}
|
||||
|
||||
func New(s *store.Store) *Authenticator {
|
||||
var out Authenticator
|
||||
var err error
|
||||
out.s = s
|
||||
out.sessions, err = s.GetSessions()
|
||||
//fmt.Println(out.sessions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &out
|
||||
}
|
||||
|
||||
func (a *Authenticator) ensureAuth(in http.Handler) http.Handler {
|
||||
fmt.Println("ensureAuth called")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, err := a.getSession(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
in.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *Authenticator) Middleware(in http.Handler) http.Handler {
|
||||
handler := http.NewServeMux()
|
||||
handler.HandleFunc("GET /login", getLogin)
|
||||
handler.HandleFunc("POST /login", a.login)
|
||||
handler.HandleFunc("GET /logout", a.logout)
|
||||
handler.Handle("/", a.ensureAuth(in))
|
||||
return handler
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/crypto"
|
||||
)
|
||||
|
||||
func (a *Authenticator) verifyCredentials(login, password string) error {
|
||||
user, ok := a.s.GetUser(login)
|
||||
if !ok {
|
||||
return errors.New("user with this login doesn't exist")
|
||||
}
|
||||
hash := crypto.PasswordHash(password, user.Salt)
|
||||
if hash == user.Password {
|
||||
return nil
|
||||
}
|
||||
return errors.New("wrong credentials")
|
||||
}
|
||||
17
auth/get.go
17
auth/get.go
|
|
@ -1,17 +0,0 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
_ "embed"
|
||||
)
|
||||
|
||||
//go:embed templates/login.html
|
||||
var loginHTML string
|
||||
|
||||
func getLogin(w http.ResponseWriter, _ *http.Request) {
|
||||
w.Header().Set("content-type", "text/html")
|
||||
fmt.Fprint(w, loginHTML)
|
||||
return
|
||||
}
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||
)
|
||||
|
||||
func (a *Authenticator) getSession(r *http.Request) (models.Token, error) {
|
||||
c, err := r.Cookie("token")
|
||||
if err != nil {
|
||||
return models.Token{}, err
|
||||
}
|
||||
session, ok := a.sessions[c.Value]
|
||||
if !ok {
|
||||
return models.Token{}, errors.New("session not found")
|
||||
}
|
||||
if time.Since(session.Expiration) > 0 {
|
||||
delete(a.sessions, c.Value)
|
||||
err := a.s.CleanSessions()
|
||||
if err != nil {
|
||||
fmt.Println("error cleaning the sessions", err)
|
||||
}
|
||||
return models.Token{}, errors.New("session expired")
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
||||
func (a *Authenticator) login(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
login := r.PostFormValue("login")
|
||||
password := r.PostFormValue("password")
|
||||
if a.verifyCredentials(login, password) != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
cookie, err := a.newSession(login)
|
||||
if err != nil {
|
||||
w.Header().Set("content-type", "text/html")
|
||||
fmt.Fprint(w, "internal server error")
|
||||
return
|
||||
}
|
||||
http.SetCookie(w, &cookie)
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
|
||||
func (a *Authenticator) logout(w http.ResponseWriter, r *http.Request) {
|
||||
var cookie http.Cookie
|
||||
cookie.Name = "token"
|
||||
cookie.Value = "nil"
|
||||
cookie.HttpOnly = true
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Path = "/"
|
||||
http.SetCookie(w, &cookie)
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
c, err := r.Cookie("token")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, ok := a.sessions[c.Value]
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
fmt.Println("removing session")
|
||||
delete(a.sessions, c.Value)
|
||||
err = a.s.RevokeSession(c.Value)
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
<!doctype html>
|
||||
<html lang="pl">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Login — Zamówienia ciast</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
||||
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@20..48,100..700,0..1,0&display=swap" rel="stylesheet">
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body class="login-page">
|
||||
<div class="login-card">
|
||||
<div class="login-icon">
|
||||
<span class="material-symbols-outlined" style="font-size:40px">cake</span>
|
||||
</div>
|
||||
<h1>Zaloguj się</h1>
|
||||
<form method="post">
|
||||
<label>
|
||||
<span>Login</span>
|
||||
<input type="text" name="login" />
|
||||
</label>
|
||||
<label>
|
||||
<span>Hasło</span>
|
||||
<input type="password" name="password" />
|
||||
</label>
|
||||
<button type="submit">Zaloguj się</button>
|
||||
</form>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,30 +0,0 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/crypto"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/models"
|
||||
)
|
||||
|
||||
func (a *Authenticator) newSession(user string) (http.Cookie, error) {
|
||||
key := crypto.GenerateKey()
|
||||
var session models.Token
|
||||
var cookie http.Cookie
|
||||
session.User = user
|
||||
session.Expiration = time.Now().Add(time.Hour * 24)
|
||||
a.sessions[key] = session
|
||||
err := a.s.AddSession(key, user, session.Expiration)
|
||||
if err != nil {
|
||||
return cookie, err
|
||||
}
|
||||
|
||||
cookie.Name = "token"
|
||||
cookie.Value = key
|
||||
cookie.HttpOnly = true
|
||||
cookie.SameSite = http.SameSiteStrictMode
|
||||
cookie.Path = "/"
|
||||
//cookie.Secure = true
|
||||
return cookie, nil
|
||||
}
|
||||
10
main.go
10
main.go
|
|
@ -1,11 +1,9 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/auth"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/logging"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/server"
|
||||
"git.bronku.xyz/bronku/cake-order-tracker/store"
|
||||
|
|
@ -13,14 +11,10 @@ import (
|
|||
|
||||
func main() {
|
||||
s := store.OpenStore("./foo.db")
|
||||
s.AddUser("admin", "secret")
|
||||
defer s.Close()
|
||||
h := server.New(s)
|
||||
defer h.Close()
|
||||
|
||||
var handler http.Handler = h
|
||||
handler = logging.Middleware(handler)
|
||||
handler = auth.New(s).Middleware(handler)
|
||||
fmt.Println("starting server")
|
||||
log.Fatal(http.ListenAndServe(":8080", handler))
|
||||
log.Println("starting server")
|
||||
log.Fatal(http.ListenAndServe(":8080", logging.Middleware(h)))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue