2023-08-14 15:34:41 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
2024-04-16 08:20:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-08-14 15:34:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// keyWrapper is wrapper for context keys.
|
|
|
|
type keyWrapper string
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
// boxKey is an ID used to store Box in a context.
|
|
|
|
var boxKey = keyWrapper("__context_box_key")
|
2023-08-14 15:34:41 +00:00
|
|
|
|
|
|
|
// GetBoxData extracts accessbox.Box from context.
|
|
|
|
func GetBoxData(ctx context.Context) (*accessbox.Box, error) {
|
2024-04-16 08:20:35 +00:00
|
|
|
data, ok := ctx.Value(boxKey).(*Box)
|
2023-08-14 15:34:41 +00:00
|
|
|
if !ok || data == nil {
|
2024-04-16 08:20:35 +00:00
|
|
|
return nil, fmt.Errorf("couldn't get box from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.AccessBox == nil {
|
2023-08-14 15:34:41 +00:00
|
|
|
return nil, fmt.Errorf("couldn't get box data from context")
|
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
box := data.AccessBox
|
2023-08-14 15:34:41 +00:00
|
|
|
if box.Gate == nil {
|
|
|
|
box.Gate = &accessbox.GateData{}
|
|
|
|
}
|
|
|
|
return box, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthHeaders extracts auth.AuthHeader from context.
|
2023-10-05 08:05:21 +00:00
|
|
|
func GetAuthHeaders(ctx context.Context) (*AuthHeader, error) {
|
2024-04-16 08:20:35 +00:00
|
|
|
data, ok := ctx.Value(boxKey).(*Box)
|
|
|
|
if !ok || data == nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get box from context")
|
2023-08-14 15:34:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
return data.AuthHeaders, nil
|
2023-08-14 15:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetClientTime extracts time.Time from context.
|
|
|
|
func GetClientTime(ctx context.Context) (time.Time, error) {
|
2024-04-16 08:20:35 +00:00
|
|
|
data, ok := ctx.Value(boxKey).(*Box)
|
|
|
|
if !ok || data == nil {
|
|
|
|
return time.Time{}, fmt.Errorf("couldn't get box from context")
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.ClientTime.IsZero() {
|
2023-08-14 15:34:41 +00:00
|
|
|
return time.Time{}, fmt.Errorf("couldn't get client time from context")
|
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
return data.ClientTime, nil
|
2023-08-14 15:34:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
// GetAccessBoxAttrs extracts []object.Attribute from context.
|
|
|
|
func GetAccessBoxAttrs(ctx context.Context) ([]object.Attribute, error) {
|
|
|
|
data, ok := ctx.Value(boxKey).(*Box)
|
|
|
|
if !ok || data == nil {
|
|
|
|
return nil, fmt.Errorf("couldn't get box from context")
|
|
|
|
}
|
2023-08-14 15:34:41 +00:00
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
return data.Attributes, nil
|
2023-08-14 15:34:41 +00:00
|
|
|
}
|
|
|
|
|
2024-04-16 08:20:35 +00:00
|
|
|
// SetBox sets Box in the context.
|
|
|
|
func SetBox(ctx context.Context, box *Box) context.Context {
|
|
|
|
return context.WithValue(ctx, boxKey, box)
|
2023-08-14 15:34:41 +00:00
|
|
|
}
|