2017-11-08 03:48:22 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
"github.com/gorilla/sessions"
|
2017-11-08 03:48:22 +00:00
|
|
|
"github.com/kirsle/blog/core/models/users"
|
|
|
|
)
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
type key int
|
|
|
|
|
|
|
|
const (
|
|
|
|
sessionKey key = iota
|
|
|
|
userKey
|
|
|
|
)
|
|
|
|
|
|
|
|
// SessionLoader gets the Gorilla session store and makes it available on the
|
|
|
|
// Request context.
|
|
|
|
func (b *Blog) SessionLoader(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
session, _ := b.store.Get(r, "session")
|
|
|
|
|
|
|
|
log.Debug("REQUEST START: %s %s", r.Method, r.URL.Path)
|
|
|
|
ctx := context.WithValue(r.Context(), sessionKey, session)
|
|
|
|
next(w, r.WithContext(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session returns the current request's session.
|
|
|
|
func (b *Blog) Session(r *http.Request) *sessions.Session {
|
|
|
|
ctx := r.Context()
|
|
|
|
if session, ok := ctx.Value(sessionKey).(*sessions.Session); ok {
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Error(
|
|
|
|
"Session(): didn't find session in request context! Getting it " +
|
|
|
|
"from the session store instead.",
|
|
|
|
)
|
|
|
|
session, _ := b.store.Get(r, "session")
|
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2017-11-08 03:48:22 +00:00
|
|
|
// AuthMiddleware loads the user's authentication state.
|
|
|
|
func (b *Blog) AuthMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
2017-11-15 14:55:15 +00:00
|
|
|
session := b.Session(r)
|
|
|
|
log.Debug("AuthMiddleware() -- session values: %v", session.Values)
|
2017-11-08 03:48:22 +00:00
|
|
|
if loggedIn, ok := session.Values["logged-in"].(bool); ok && loggedIn {
|
|
|
|
// They seem to be logged in. Get their user object.
|
|
|
|
id := session.Values["user-id"].(int)
|
|
|
|
u, err := users.Load(id)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error loading user ID %d from session: %v", id, err)
|
|
|
|
next(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), userKey, u)
|
|
|
|
next(w, r.WithContext(ctx))
|
2017-11-15 14:55:15 +00:00
|
|
|
return
|
2017-11-08 03:48:22 +00:00
|
|
|
}
|
|
|
|
next(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoginRequired is a middleware that requires a logged-in user.
|
|
|
|
func (b *Blog) LoginRequired(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
|
|
|
|
ctx := r.Context()
|
|
|
|
if user, ok := ctx.Value(userKey).(*users.User); ok {
|
|
|
|
if user.ID > 0 {
|
|
|
|
next(w, r)
|
2017-11-15 14:55:15 +00:00
|
|
|
return
|
2017-11-08 03:48:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
log.Info("Redirect away!")
|
2017-11-08 03:48:22 +00:00
|
|
|
b.Redirect(w, "/login?next="+r.URL.Path)
|
|
|
|
}
|