Skip to content

Commit

Permalink
Add Security Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Feb 25, 2024
1 parent 22ed41d commit 277d55c
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion mux/mux.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package mux

import (
"github.com/gorilla/mux"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gorilla/mux"
)

const (
Expand Down Expand Up @@ -89,3 +90,53 @@ func HandleFiles(r *mux.Router, prefix string, static string) {
h := http.StripPrefix(prefix, http.FileServer(http.Dir(dir)))
r.PathPrefix(prefix).Handler(h)
}

func HandleWithSecurity(r *mux.Router, path string, f func(http.ResponseWriter, *http.Request), check func(next http.Handler) http.Handler, authorize func(next http.Handler, privilege string, action int32) http.Handler, menuId string, action int32, methods ...string) *mux.Route {
finalHandler := http.HandlerFunc(f)
funcAuthorize := func(next http.Handler) http.Handler {
return authorize(next, menuId, action)
}
return r.Handle(path, check(funcAuthorize(finalHandler))).Methods(methods...)
}

type SecurityHandler struct {
Check func(next http.Handler) http.Handler
Authorize func(next http.Handler, privilege string, action int32) http.Handler
}
func NewSecurityHandler(check func(next http.Handler) http.Handler, authorize func(next http.Handler, privilege string, action int32) http.Handler) *SecurityHandler {
return &SecurityHandler{Check: check, Authorize: authorize}
}
func (h *SecurityHandler) Handle(r *mux.Router, path string, f func(http.ResponseWriter, *http.Request), menuId string, action int32, methods ...string) *mux.Route {
finalHandler := http.HandlerFunc(f)
funcAuthorize := func(next http.Handler) http.Handler {
return h.Authorize(next, menuId, action)
}
return r.Handle(path, h.Check(funcAuthorize(finalHandler))).Methods(methods...)
}

type CookieHandler struct {
Check func(next http.Handler, skipRefreshTTL bool) http.Handler
Authorize func(next http.Handler, privilege string, action int32) http.Handler
Prefix string
Router *mux.Router
}

func NewCookieHandler(r *mux.Router, check func(next http.Handler, skipRefreshTTL bool) http.Handler, authorize func(next http.Handler, privilege string, action int32) http.Handler, opts...string) *CookieHandler {
ch := &CookieHandler{Router: r, Check: check, Authorize: authorize}
if len(opts) > 0 && len(opts[0]) > 0 {
ch.Router = ch.Router.PathPrefix(opts[0]).Subrouter()
return ch
}
return ch
}
func (h *CookieHandler) CheckPrivilege(path string, f http.HandlerFunc, privilege string, action int32, methods ...string) *mux.Route {
finalHandler := f
authorize := func(next http.Handler) http.Handler {
return h.Authorize(next, privilege, action)
}
return h.Router.Handle(path, h.Check(authorize(finalHandler), false)).Methods(methods...)
}

func (h *CookieHandler) Handle(path string, f http.HandlerFunc, methods ...string) *mux.Route {
return h.Router.Handle(path, h.Check(f, true)).Methods(methods...)
}

0 comments on commit 277d55c

Please sign in to comment.