2023-07-05 17:04:52 +03:00
|
|
|
package middleware
|
2020-07-16 18:33:47 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-03-07 17:38:08 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-08-23 14:07:52 +03:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2020-07-16 18:33:47 +03:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-07-05 17:04:52 +03:00
|
|
|
func Auth(center auth.Center, log *zap.Logger) Func {
|
2022-12-27 15:30:11 +03:00
|
|
|
return func(h http.Handler) http.Handler {
|
2020-07-16 18:33:47 +03:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-07-06 12:13:45 +03:00
|
|
|
ctx := r.Context()
|
2021-07-16 15:35:07 +03:00
|
|
|
box, err := center.Authenticate(r)
|
2020-07-16 18:33:47 +03:00
|
|
|
if err != nil {
|
2021-06-11 19:29:55 +03:00
|
|
|
if err == auth.ErrNoAuthorizationHeader {
|
2023-08-23 14:07:52 +03:00
|
|
|
reqLogOrDefault(ctx, log).Debug(logs.CouldntReceiveAccessBoxForGateKeyRandomKeyWillBeUsed)
|
2021-06-11 19:29:55 +03:00
|
|
|
} else {
|
2023-08-23 14:07:52 +03:00
|
|
|
reqLogOrDefault(ctx, log).Error(logs.FailedToPassAuthentication, zap.Error(err))
|
2021-08-09 11:53:58 +03:00
|
|
|
if _, ok := err.(errors.Error); !ok {
|
|
|
|
err = errors.GetAPIError(errors.ErrAccessDenied)
|
|
|
|
}
|
|
|
|
WriteErrorResponse(w, GetReqInfo(r.Context()), err)
|
2021-06-11 19:29:55 +03:00
|
|
|
return
|
|
|
|
}
|
2021-06-11 14:52:03 +03:00
|
|
|
} else {
|
2023-08-14 18:34:41 +03:00
|
|
|
ctx = SetBoxData(ctx, box.AccessBox)
|
2022-11-08 12:12:55 +03:00
|
|
|
if !box.ClientTime.IsZero() {
|
2023-08-14 18:34:41 +03:00
|
|
|
ctx = SetClientTime(ctx, box.ClientTime)
|
2022-11-08 12:12:55 +03:00
|
|
|
}
|
2023-08-14 18:34:41 +03:00
|
|
|
ctx = SetAuthHeaders(ctx, box.AuthHeaders)
|
2021-06-11 14:52:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r.WithContext(ctx))
|
2020-07-16 18:33:47 +03:00
|
|
|
})
|
2022-12-27 15:30:11 +03:00
|
|
|
}
|
2020-07-16 18:33:47 +03:00
|
|
|
}
|