simplified middleware interface
This commit is contained in:
parent
5bcaa4c89f
commit
b7822c1b50
46
auth/auth.go
46
auth/auth.go
@ -10,20 +10,18 @@ const UserCtxKey = "user"
|
||||
|
||||
// Middleware adds authentication handling
|
||||
func Middleware(userLookup func(int) any) sushi.Middleware {
|
||||
return func(next sushi.Handler) sushi.Handler {
|
||||
return func(ctx sushi.Ctx, params []string) {
|
||||
sess := session.GetCurrentSession(ctx)
|
||||
if sess != nil && sess.UserID > 0 && userLookup != nil {
|
||||
user := userLookup(sess.UserID)
|
||||
if user != nil {
|
||||
ctx.SetUserValue(UserCtxKey, user)
|
||||
} else {
|
||||
sess.SetUserID(0)
|
||||
session.StoreSession(sess)
|
||||
}
|
||||
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||
sess := session.GetCurrentSession(ctx)
|
||||
if sess != nil && sess.UserID > 0 && userLookup != nil {
|
||||
user := userLookup(sess.UserID)
|
||||
if user != nil {
|
||||
ctx.SetUserValue(UserCtxKey, user)
|
||||
} else {
|
||||
sess.SetUserID(0)
|
||||
session.StoreSession(sess)
|
||||
}
|
||||
next(ctx, params)
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,14 +32,12 @@ func RequireAuth(redirectPath ...string) sushi.Middleware {
|
||||
redirect = redirectPath[0]
|
||||
}
|
||||
|
||||
return func(next sushi.Handler) sushi.Handler {
|
||||
return func(ctx sushi.Ctx, params []string) {
|
||||
if !IsAuthenticated(ctx) {
|
||||
ctx.Redirect(redirect, fasthttp.StatusFound)
|
||||
return
|
||||
}
|
||||
next(ctx, params)
|
||||
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||
if !IsAuthenticated(ctx) {
|
||||
ctx.Redirect(redirect, fasthttp.StatusFound)
|
||||
return
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
@ -52,14 +48,12 @@ func RequireGuest(redirectPath ...string) sushi.Middleware {
|
||||
redirect = redirectPath[0]
|
||||
}
|
||||
|
||||
return func(next sushi.Handler) sushi.Handler {
|
||||
return func(ctx sushi.Ctx, params []string) {
|
||||
if IsAuthenticated(ctx) {
|
||||
ctx.Redirect(redirect, fasthttp.StatusFound)
|
||||
return
|
||||
}
|
||||
next(ctx, params)
|
||||
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||
if IsAuthenticated(ctx) {
|
||||
ctx.Redirect(redirect, fasthttp.StatusFound)
|
||||
return
|
||||
}
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
|
22
csrf/csrf.go
22
csrf/csrf.go
@ -119,20 +119,18 @@ func ValidateFormCSRFToken(ctx sushi.Ctx) bool {
|
||||
|
||||
// Middleware returns middleware that automatically validates CSRF tokens
|
||||
func Middleware() sushi.Middleware {
|
||||
return func(next sushi.Handler) sushi.Handler {
|
||||
return func(ctx sushi.Ctx, params []string) {
|
||||
method := string(ctx.Method())
|
||||
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||
method := string(ctx.Method())
|
||||
|
||||
if method == "POST" || method == "PUT" || method == "PATCH" || method == "DELETE" {
|
||||
if !ValidateFormCSRFToken(ctx) {
|
||||
GenerateCSRFToken(ctx)
|
||||
currentPath := string(ctx.Path())
|
||||
ctx.Redirect(currentPath, 302)
|
||||
return
|
||||
}
|
||||
if method == "POST" || method == "PUT" || method == "PATCH" || method == "DELETE" {
|
||||
if !ValidateFormCSRFToken(ctx) {
|
||||
GenerateCSRFToken(ctx)
|
||||
currentPath := string(ctx.Path())
|
||||
ctx.Redirect(currentPath, 302)
|
||||
return
|
||||
}
|
||||
|
||||
next(ctx, params)
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
23
router.go
23
router.go
@ -141,10 +141,27 @@ func (r *Router) methodNode(method string) *node {
|
||||
}
|
||||
|
||||
func applyMiddleware(h Handler, mw []Middleware) Handler {
|
||||
for i := len(mw) - 1; i >= 0; i-- {
|
||||
h = mw[i](h)
|
||||
if len(mw) == 0 {
|
||||
return h
|
||||
}
|
||||
|
||||
return func(ctx Ctx, params []string) {
|
||||
var index int
|
||||
var next func()
|
||||
|
||||
next = func() {
|
||||
if index >= len(mw) {
|
||||
h(ctx, params)
|
||||
return
|
||||
}
|
||||
|
||||
currentMW := mw[index]
|
||||
index++
|
||||
currentMW(ctx, params, next)
|
||||
}
|
||||
|
||||
next()
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func readSegment(path string, start int) (segment string, end int, hasMore bool) {
|
||||
|
@ -4,27 +4,25 @@ import sushi "git.sharkk.net/Sharkk/Sushi"
|
||||
|
||||
// Middleware provides session handling
|
||||
func Middleware() sushi.Middleware {
|
||||
return func(next sushi.Handler) sushi.Handler {
|
||||
return func(ctx sushi.Ctx, params []string) {
|
||||
sessionID := sushi.GetCookie(ctx, SessionCookieName)
|
||||
var sess *Session
|
||||
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||
sessionID := sushi.GetCookie(ctx, SessionCookieName)
|
||||
var sess *Session
|
||||
|
||||
if sessionID != "" {
|
||||
if existingSess, exists := GetSession(sessionID); exists {
|
||||
sess = existingSess
|
||||
sess.Touch()
|
||||
StoreSession(sess)
|
||||
SetSessionCookie(ctx, sessionID)
|
||||
}
|
||||
if sessionID != "" {
|
||||
if existingSess, exists := GetSession(sessionID); exists {
|
||||
sess = existingSess
|
||||
sess.Touch()
|
||||
StoreSession(sess)
|
||||
SetSessionCookie(ctx, sessionID)
|
||||
}
|
||||
|
||||
if sess == nil {
|
||||
sess = CreateSession(0) // Guest session
|
||||
SetSessionCookie(ctx, sess.ID)
|
||||
}
|
||||
|
||||
ctx.SetUserValue(SessionCtxKey, sess)
|
||||
next(ctx, params)
|
||||
}
|
||||
|
||||
if sess == nil {
|
||||
sess = CreateSession(0) // Guest session
|
||||
SetSessionCookie(ctx, sess.ID)
|
||||
}
|
||||
|
||||
ctx.SetUserValue(SessionCtxKey, sess)
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
@ -11,12 +11,10 @@ const RequestTimerKey = "request_start_time"
|
||||
|
||||
// Middleware adds request timing functionality
|
||||
func Middleware() sushi.Middleware {
|
||||
return func(next sushi.Handler) sushi.Handler {
|
||||
return func(ctx sushi.Ctx, params []string) {
|
||||
startTime := time.Now()
|
||||
ctx.SetUserValue(RequestTimerKey, startTime)
|
||||
next(ctx, params)
|
||||
}
|
||||
return func(ctx sushi.Ctx, params []string, next func()) {
|
||||
startTime := time.Now()
|
||||
ctx.SetUserValue(RequestTimerKey, startTime)
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user