2022-07-28 15:00:43 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-08-10 13:53:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-07-28 15:00:43 +00:00
|
|
|
"net/http"
|
2023-04-06 19:42:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2023-04-03 18:27:15 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/penahub_common/jwt_adapter"
|
2022-12-31 14:46:28 +00:00
|
|
|
"penahub.gitlab.yandexcloud.net/backend/templategen/amo"
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/templategen/dal"
|
|
|
|
"penahub.gitlab.yandexcloud.net/backend/templategen/dal/model"
|
2022-07-28 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Middleware struct {
|
2022-08-22 04:23:47 +00:00
|
|
|
dal *dal.MongoDAL
|
|
|
|
amo *amo.ClientApp
|
|
|
|
logger *zap.Logger
|
|
|
|
AmoAccessMap map[string]string // key - endpoint; value - access (visibility, creation, delete)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 04:23:47 +00:00
|
|
|
func InitMiddleware(dal *dal.MongoDAL, amo *amo.ClientApp, logger *zap.Logger,
|
|
|
|
amoAccessMap map[string]string) *Middleware {
|
|
|
|
return &Middleware{dal: dal, amo: amo, logger: logger, AmoAccessMap: amoAccessMap}
|
2022-08-10 13:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *Middleware) MiddlewareCors(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *Middleware) Logger(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if strings.Contains(r.URL.String(), "/assets/") {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2023-04-03 18:27:15 +00:00
|
|
|
userId := getJwtUser(r)
|
2022-10-11 10:07:29 +00:00
|
|
|
|
|
|
|
amoData := getAmoByJwt(r)
|
|
|
|
if amoData != nil {
|
|
|
|
userId = amoData.UserID
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
mw.logger.Info("HttpRequest",
|
|
|
|
zap.String("URL", r.URL.String()),
|
|
|
|
zap.String("UserID", userId),
|
|
|
|
)
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *Middleware) MiddlewareJwt(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if strings.Contains(r.URL.String(), "/assets/") {
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-06 19:42:16 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
if amoData := getAmoByJwt(r); amoData != nil {
|
|
|
|
ctx = context.WithValue(ctx, "UserID", amoData.UserID)
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check token in cookies
|
2023-04-06 19:42:16 +00:00
|
|
|
c := r.Header.Get(jwt_adapter.DefaultHeaderKey)
|
2022-07-28 15:00:43 +00:00
|
|
|
|
2023-04-06 19:42:16 +00:00
|
|
|
jwt, err := jwt_adapter.Decode(c)
|
2023-04-06 20:21:51 +00:00
|
|
|
if err == nil {
|
2023-04-06 19:42:16 +00:00
|
|
|
ctx = context.WithValue(ctx, "PenaUserID", jwt.GetUserID())
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:53:34 +00:00
|
|
|
func (mw *Middleware) MiddlewareHeaders(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With, X-Auth-Token")
|
|
|
|
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
|
|
|
w.Header().Set("Access-Control-Expose-Headers", "*")
|
|
|
|
w.Header().Set("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE")
|
|
|
|
//w.Header().Add()
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *Middleware) MiddlewareAmoJwt(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-10-11 10:07:29 +00:00
|
|
|
if strings.Contains(r.URL.String(), "/assets/") {
|
2022-08-10 13:53:34 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
authMap := map[string]interface{}{
|
|
|
|
//"/amo": nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
required := false
|
|
|
|
_, ok := authMap[r.URL.String()]
|
|
|
|
if ok {
|
|
|
|
required = true
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:53:34 +00:00
|
|
|
token, err := mw.amo.DecodeJwt(r)
|
|
|
|
if err != nil {
|
2022-10-11 10:07:29 +00:00
|
|
|
if required {
|
|
|
|
mw.reportError(w, err, http.StatusUnauthorized)
|
|
|
|
} else {
|
2022-11-21 18:32:00 +00:00
|
|
|
// mw.logger.Error("ErrAmoJwtAccess", zap.Error(err))
|
2022-10-11 10:07:29 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
}
|
2022-08-10 13:53:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := mw.dal.Amo.GetByAccountID(r.Context(), strconv.FormatInt(token.AccountId, 10))
|
|
|
|
if err != nil {
|
|
|
|
mw.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if data == nil {
|
|
|
|
mw.reportError(w, errors.New("amo account not found"), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 04:23:47 +00:00
|
|
|
// check access rules
|
|
|
|
isAcessGranted := token.IsAdmin
|
|
|
|
|
|
|
|
// Если права не указаны для эндпоинта, то доступно любому юзеру
|
|
|
|
rule, ok := mw.AmoAccessMap[r.URL.String()]
|
|
|
|
if ok {
|
|
|
|
// Если не админ, то проверяем права пользователя
|
|
|
|
if rule == "visibility" && !isAcessGranted {
|
|
|
|
for _, userId := range data.AccessRules.Visibility {
|
|
|
|
if userId == token.UserId {
|
|
|
|
isAcessGranted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule == "creation" && !isAcessGranted {
|
|
|
|
for _, userId := range data.AccessRules.Creation {
|
|
|
|
if userId == token.UserId {
|
|
|
|
isAcessGranted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rule == "delete" && !isAcessGranted {
|
|
|
|
for _, userId := range data.AccessRules.Delete {
|
|
|
|
if userId == token.UserId {
|
|
|
|
isAcessGranted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isAcessGranted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Если прав нет, прерываемся
|
|
|
|
if !isAcessGranted {
|
2022-11-21 18:32:00 +00:00
|
|
|
mw.reportError(w, errors.New("amoUser forbidden"), http.StatusForbidden) // mb: http.StatusForbidden
|
2022-08-22 04:23:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), "amoData", data)
|
|
|
|
ctx = context.WithValue(ctx, "amoIsAdmin", token.IsAdmin)
|
|
|
|
ctx = context.WithValue(ctx, "amoUserID", token.UserId)
|
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *Middleware) MiddlewareAmoPlug(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-11-12 10:24:48 +00:00
|
|
|
if strings.Contains(r.URL.String(), "/assets/") {
|
|
|
|
next.ServeHTTP(w, r)
|
2022-08-22 04:23:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 10:24:48 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
if r.Header.Get("TestAuth") == "75bc853ae763ffc36cad97069682fed1" {
|
2022-11-21 19:15:59 +00:00
|
|
|
data, err := mw.dal.Amo.GetByAccountID(r.Context(), "30228997")
|
2022-11-12 10:24:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
mw.reportError(w, err, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if data == nil {
|
|
|
|
mw.reportError(w, errors.New("amo account not found"), http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, "amoData", data)
|
2022-08-22 04:23:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 13:53:34 +00:00
|
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:27:15 +00:00
|
|
|
func getJwtUser(r *http.Request) string {
|
|
|
|
user, ok := r.Context().Value("UserID").(string)
|
2022-07-28 15:00:43 +00:00
|
|
|
if ok {
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:27:15 +00:00
|
|
|
return ""
|
2022-07-28 15:00:43 +00:00
|
|
|
}
|
2023-04-03 18:27:15 +00:00
|
|
|
|
2022-10-11 10:07:29 +00:00
|
|
|
func getAmoByJwt(r *http.Request) *model.Amo {
|
|
|
|
amoData, ok := r.Context().Value("amoData").(*model.Amo)
|
|
|
|
if ok {
|
|
|
|
return amoData
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-10 13:53:34 +00:00
|
|
|
|
|
|
|
func (mw *Middleware) reportError(w http.ResponseWriter, err error, status int) error {
|
|
|
|
mw.logger.Error("ErrorMiddleware", zap.Error(err))
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
w.WriteHeader(status)
|
|
|
|
return json.NewEncoder(w).Encode(err)
|
|
|
|
}
|