ch12.2
This commit is contained in:
5
cmd/web/context.go
Normal file
5
cmd/web/context.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type contextKey string
|
||||||
|
|
||||||
|
const isAuthenticatedContextKey = contextKey("isAuthenticated")
|
||||||
@@ -42,5 +42,9 @@ func (app *application) decodePostForm(r *http.Request, snippetForm any) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) isAuthenticated(r *http.Request) bool {
|
func (app *application) isAuthenticated(r *http.Request) bool {
|
||||||
return app.sessionManager.Exists(r.Context(), "authenticatedUserID")
|
isAuthenticated, ok := r.Context().Value(isAuthenticatedContextKey).(bool)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return isAuthenticated
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -20,6 +21,17 @@ func secureHeaders(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func noSurf(next http.Handler) http.Handler {
|
||||||
|
csrfHanlder := nosurf.New(next)
|
||||||
|
csrfHanlder.SetBaseCookie(http.Cookie{
|
||||||
|
HttpOnly: true,
|
||||||
|
Path: "/",
|
||||||
|
Secure: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
return csrfHanlder
|
||||||
|
}
|
||||||
|
|
||||||
func (app *application) logRequest(next http.Handler) http.Handler {
|
func (app *application) logRequest(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
app.infoLog.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL.RequestURI())
|
app.infoLog.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL.RequestURI())
|
||||||
@@ -52,13 +64,24 @@ func (app *application) requireAuthentication(next http.Handler) http.Handler {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func noSurf(next http.Handler) http.Handler {
|
func (app *application) authenticate(next http.Handler) http.Handler {
|
||||||
csrfHanlder := nosurf.New(next)
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
csrfHanlder.SetBaseCookie(http.Cookie{
|
id := app.sessionManager.GetInt(r.Context(), "authenticatedUserID")
|
||||||
HttpOnly: true,
|
if id == 0 {
|
||||||
Path: "/",
|
next.ServeHTTP(w, r)
|
||||||
Secure: true,
|
return
|
||||||
})
|
}
|
||||||
|
|
||||||
return csrfHanlder
|
exists, err := app.users.Exists(id)
|
||||||
|
if err != nil {
|
||||||
|
app.serverError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
ctx := context.WithValue(r.Context(), isAuthenticatedContextKey, true)
|
||||||
|
r = r.WithContext(ctx)
|
||||||
|
}
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ func (app *application) routes() http.Handler {
|
|||||||
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
||||||
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
|
router.Handler(http.MethodGet, "/static/*filepath", http.StripPrefix("/static", fileServer))
|
||||||
|
|
||||||
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf)
|
dynamic := alice.New(app.sessionManager.LoadAndSave, noSurf, app.authenticate)
|
||||||
|
|
||||||
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
router.Handler(http.MethodGet, "/", dynamic.ThenFunc(app.home))
|
||||||
router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView))
|
router.Handler(http.MethodGet, "/snippet/view/:id", dynamic.ThenFunc(app.snippetView))
|
||||||
|
|||||||
@@ -69,5 +69,11 @@ func (m *UserModel) Authenticate(email, password string) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *UserModel) Exists(id int) (bool, error) {
|
func (m *UserModel) Exists(id int) (bool, error) {
|
||||||
return false, nil
|
var exists bool
|
||||||
|
|
||||||
|
stmt := `SELECT EXISTS(SELECT true FROM users WHERE id = ?)`
|
||||||
|
|
||||||
|
err := m.DB.QueryRow(stmt, id).Scan(&exists)
|
||||||
|
|
||||||
|
return exists, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user