mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-01 19:32:27 +08:00
46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"encoding/json"
|
|
. "github.com/mickael-kerjean/nuage/server/common"
|
|
"net/http"
|
|
)
|
|
|
|
func LoggedInOnly(fn func(App, http.ResponseWriter, *http.Request)) func(ctx App, res http.ResponseWriter, req *http.Request) {
|
|
return func(ctx App, res http.ResponseWriter, req *http.Request) {
|
|
if ctx.Backend == nil || ctx.Session == nil {
|
|
SendErrorResult(res, NewError("Forbidden", 403))
|
|
return
|
|
}
|
|
|
|
fn(ctx, res, req)
|
|
}
|
|
}
|
|
|
|
func AdminOnly(fn func(App, http.ResponseWriter, *http.Request)) func(ctx App, res http.ResponseWriter, req *http.Request) {
|
|
return func(ctx App, res http.ResponseWriter, req *http.Request) {
|
|
if admin := Config.Get("auth.admin").String(); admin != "" {
|
|
c, err := req.Cookie(COOKIE_NAME_ADMIN);
|
|
if err != nil {
|
|
SendErrorResult(res, ErrPermissionDenied)
|
|
return
|
|
}
|
|
|
|
str, err := DecryptString(SECRET_KEY, c.Value);
|
|
if err != nil {
|
|
SendErrorResult(res, ErrPermissionDenied)
|
|
return
|
|
}
|
|
token := AdminToken{}
|
|
json.Unmarshal([]byte(str), &token)
|
|
|
|
if token.IsValid() == false || token.IsAdmin() == false {
|
|
SendErrorResult(res, ErrPermissionDenied)
|
|
return
|
|
}
|
|
}
|
|
|
|
fn(ctx, res, req)
|
|
}
|
|
}
|