Refactor auth session management into separate function
This commit is contained in:
parent
bba6e8618a
commit
01da46b81e
3 changed files with 26 additions and 32 deletions
|
|
@ -5,15 +5,11 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/internal/models"
|
||||
"github.com/Bronku/iroon/internal/store"
|
||||
)
|
||||
|
||||
//go:embed templates/wrongPassword.html
|
||||
var wrongPassword string
|
||||
|
||||
type Authenticator struct {
|
||||
sessions map[string]models.Token
|
||||
s *store.Store
|
||||
|
|
@ -24,7 +20,7 @@ func New(s *store.Store) *Authenticator {
|
|||
var err error
|
||||
out.s = s
|
||||
out.sessions, err = s.GetSessions()
|
||||
fmt.Println(out.sessions)
|
||||
//fmt.Println(out.sessions)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
@ -34,25 +30,11 @@ func New(s *store.Store) *Authenticator {
|
|||
func (a *Authenticator) ensureAuth(in http.Handler) http.Handler {
|
||||
fmt.Println("ensureAuth called")
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := r.Cookie("token")
|
||||
_, err := a.getSession(r)
|
||||
if err != nil {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
value, ok := a.sessions[c.Value]
|
||||
if !ok {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if time.Since(value.Expiration) > 0 {
|
||||
delete(a.sessions, c.Value)
|
||||
err := a.s.CleanSessions()
|
||||
if err != nil {
|
||||
fmt.Println("error cleaning the sessions", err)
|
||||
}
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
in.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,34 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/Bronku/iroon/internal/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 {
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
<header>
|
||||
<h1>Wrong Password</h1>
|
||||
</header>
|
||||
<main>
|
||||
<form method="post">
|
||||
<label>Login</label>
|
||||
<input type="text" name="login" />
|
||||
<label>Password</label>
|
||||
<input type="password" name="password" />
|
||||
<button type="submit">login</button>
|
||||
</form>
|
||||
</main>
|
||||
Loading…
Add table
Add a link
Reference in a new issue