2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-08-06 15:23:01 +00:00
|
|
|
"context"
|
2021-08-30 19:44:53 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2021-05-20 10:14:17 +00:00
|
|
|
"errors"
|
2020-08-19 13:28:17 +00:00
|
|
|
"fmt"
|
2020-11-27 12:31:39 +00:00
|
|
|
"io"
|
2021-08-30 19:44:53 +00:00
|
|
|
"mime/multipart"
|
2020-07-16 15:33:47 +00:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2020-07-20 23:43:40 +00:00
|
|
|
"strings"
|
2020-08-06 15:23:01 +00:00
|
|
|
"time"
|
2020-07-15 13:48:25 +00:00
|
|
|
|
2022-12-14 16:58:00 +00:00
|
|
|
v4 "github.com/TrueCloudLab/frostfs-s3-gw/api/auth/signer/v4"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
apiErrors "github.com/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
"github.com/TrueCloudLab/frostfs-s3-gw/creds/tokens"
|
|
|
|
oid "github.com/TrueCloudLab/frostfs-sdk-go/object/id"
|
2020-10-13 09:33:33 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-07-15 13:48:25 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 11:16:11 +00:00
|
|
|
// authorizationFieldRegexp -- is regexp for credentials with Base58 encoded cid and oid and '0' (zero) as delimiter.
|
2021-08-09 09:35:42 +00:00
|
|
|
var authorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request,\s*SignedHeaders=(?P<signed_header_fields>.+),\s*Signature=(?P<v4_signature>.+)`)
|
2020-07-16 15:33:47 +00:00
|
|
|
|
2021-08-30 19:44:53 +00:00
|
|
|
// postPolicyCredentialRegexp -- is regexp for credentials when uploading file using POST with policy.
|
|
|
|
var postPolicyCredentialRegexp = regexp.MustCompile(`(?P<access_key_id>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request`)
|
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
type (
|
2021-05-13 20:25:31 +00:00
|
|
|
// Center is a user authentication interface.
|
2020-11-24 06:59:01 +00:00
|
|
|
Center interface {
|
2022-11-08 09:12:55 +00:00
|
|
|
Authenticate(request *http.Request) (*Box, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Box contains access box and additional info.
|
|
|
|
Box struct {
|
|
|
|
AccessBox *accessbox.Box
|
|
|
|
ClientTime time.Time
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
2020-07-20 17:23:16 +00:00
|
|
|
|
2020-11-24 06:59:01 +00:00
|
|
|
center struct {
|
2022-08-31 22:43:36 +00:00
|
|
|
reg *RegexpSubmatcher
|
|
|
|
postReg *RegexpSubmatcher
|
|
|
|
cli tokens.Credentials
|
|
|
|
allowedAccessKeyIDPrefixes []string // empty slice means all access key ids are allowed
|
2020-07-21 10:21:03 +00:00
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
|
2020-11-27 12:31:39 +00:00
|
|
|
prs int
|
2021-08-09 09:35:42 +00:00
|
|
|
|
|
|
|
authHeader struct {
|
|
|
|
AccessKeyID string
|
|
|
|
Service string
|
|
|
|
Region string
|
|
|
|
SignatureV4 string
|
|
|
|
SignedFields []string
|
|
|
|
Date string
|
2022-06-14 15:15:29 +00:00
|
|
|
IsPresigned bool
|
|
|
|
Expiration time.Duration
|
2021-08-09 09:35:42 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
accessKeyPartsNum = 2
|
|
|
|
authHeaderPartsNum = 6
|
2021-08-30 19:44:53 +00:00
|
|
|
maxFormSizeMemory = 50 * 1048576 // 50 MB
|
2022-06-14 15:15:29 +00:00
|
|
|
|
|
|
|
AmzAlgorithm = "X-Amz-Algorithm"
|
|
|
|
AmzCredential = "X-Amz-Credential"
|
|
|
|
AmzSignature = "X-Amz-Signature"
|
|
|
|
AmzSignedHeaders = "X-Amz-SignedHeaders"
|
|
|
|
AmzExpires = "X-Amz-Expires"
|
|
|
|
AmzDate = "X-Amz-Date"
|
|
|
|
AuthorizationHdr = "Authorization"
|
|
|
|
ContentTypeHdr = "Content-Type"
|
2020-10-13 09:33:33 +00:00
|
|
|
)
|
|
|
|
|
2021-06-15 15:50:00 +00:00
|
|
|
// ErrNoAuthorizationHeader is returned for unauthenticated requests.
|
2021-06-11 16:29:55 +00:00
|
|
|
var ErrNoAuthorizationHeader = errors.New("no authorization header")
|
|
|
|
|
2020-11-27 12:31:39 +00:00
|
|
|
func (p prs) Read(_ []byte) (n int, err error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p prs) Seek(_ int64, _ int) (int64, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ io.ReadSeeker = prs(0)
|
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
// New creates an instance of AuthCenter.
|
2022-08-31 22:43:36 +00:00
|
|
|
func New(neoFS tokens.NeoFS, key *keys.PrivateKey, prefixes []string, config *cache.Config) Center {
|
2020-11-24 06:59:01 +00:00
|
|
|
return ¢er{
|
2022-08-31 22:43:36 +00:00
|
|
|
cli: tokens.New(neoFS, key, config),
|
|
|
|
reg: NewRegexpMatcher(authorizationFieldRegexp),
|
|
|
|
postReg: NewRegexpMatcher(postPolicyCredentialRegexp),
|
|
|
|
allowedAccessKeyIDPrefixes: prefixes,
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 09:35:42 +00:00
|
|
|
func (c *center) parseAuthHeader(header string) (*authHeader, error) {
|
2022-08-24 13:12:05 +00:00
|
|
|
submatches := c.reg.GetSubmatches(header)
|
2021-08-09 09:35:42 +00:00
|
|
|
if len(submatches) != authHeaderPartsNum {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrAuthorizationHeaderMalformed)
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := strings.Split(submatches["access_key_id"], "0")
|
|
|
|
if len(accessKey) != accessKeyPartsNum {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidAccessKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
signedFields := strings.Split(submatches["signed_header_fields"], ";")
|
|
|
|
|
|
|
|
return &authHeader{
|
|
|
|
AccessKeyID: submatches["access_key_id"],
|
|
|
|
Service: submatches["service"],
|
|
|
|
Region: submatches["region"],
|
|
|
|
SignatureV4: submatches["v4_signature"],
|
|
|
|
SignedFields: signedFields,
|
|
|
|
Date: submatches["date"],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:37:03 +00:00
|
|
|
func (a *authHeader) getAddress() (oid.Address, error) {
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
if err := addr.DecodeString(strings.ReplaceAll(a.AccessKeyID, "0", "/")); err != nil {
|
2022-06-27 09:37:03 +00:00
|
|
|
return addr, apiErrors.GetAPIError(apiErrors.ErrInvalidAccessKeyID)
|
2021-08-09 09:35:42 +00:00
|
|
|
}
|
2022-06-27 09:37:03 +00:00
|
|
|
return addr, nil
|
2021-08-09 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
func (c *center) Authenticate(r *http.Request) (*Box, error) {
|
2022-06-14 15:15:29 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
authHdr *authHeader
|
|
|
|
signatureDateTimeStr string
|
2022-11-08 09:12:55 +00:00
|
|
|
needClientTime bool
|
2022-06-14 15:15:29 +00:00
|
|
|
)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
queryValues := r.URL.Query()
|
|
|
|
if queryValues.Get(AmzAlgorithm) == "AWS4-HMAC-SHA256" {
|
|
|
|
creds := strings.Split(queryValues.Get(AmzCredential), "/")
|
|
|
|
if len(creds) != 5 || creds[4] != "aws4_request" {
|
|
|
|
return nil, fmt.Errorf("bad X-Amz-Credential")
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2022-06-14 15:15:29 +00:00
|
|
|
authHdr = &authHeader{
|
|
|
|
AccessKeyID: creds[0],
|
|
|
|
Service: creds[3],
|
|
|
|
Region: creds[2],
|
|
|
|
SignatureV4: queryValues.Get(AmzSignature),
|
|
|
|
SignedFields: queryValues[AmzSignedHeaders],
|
|
|
|
Date: creds[1],
|
|
|
|
IsPresigned: true,
|
|
|
|
}
|
|
|
|
authHdr.Expiration, err = time.ParseDuration(queryValues.Get(AmzExpires) + "s")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse X-Amz-Expires: %w", err)
|
|
|
|
}
|
|
|
|
signatureDateTimeStr = queryValues.Get(AmzDate)
|
|
|
|
} else {
|
|
|
|
authHeaderField := r.Header[AuthorizationHdr]
|
|
|
|
if len(authHeaderField) != 1 {
|
|
|
|
if strings.HasPrefix(r.Header.Get(ContentTypeHdr), "multipart/form-data") {
|
|
|
|
return c.checkFormData(r)
|
|
|
|
}
|
|
|
|
return nil, ErrNoAuthorizationHeader
|
|
|
|
}
|
|
|
|
authHdr, err = c.parseAuthHeader(authHeaderField[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signatureDateTimeStr = r.Header.Get(AmzDate)
|
2022-11-08 09:12:55 +00:00
|
|
|
needClientTime = true
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
signatureDateTime, err := time.Parse("20060102T150405Z", signatureDateTimeStr)
|
2020-08-06 15:23:01 +00:00
|
|
|
if err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse x-amz-date header field: %w", err)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-08-31 22:43:36 +00:00
|
|
|
if err := c.checkAccessKeyID(authHdr.AccessKeyID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
addr, err := authHdr.getAddress()
|
2021-08-09 09:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 09:37:03 +00:00
|
|
|
box, err := c.cli.GetBox(r.Context(), addr)
|
2020-07-24 14:03:02 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get box: %w", err)
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
clonedRequest := cloneRequest(r, authHdr)
|
|
|
|
if err = c.checkSign(authHdr, box, clonedRequest, signatureDateTime); err != nil {
|
2021-08-09 09:35:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
result := &Box{AccessBox: box}
|
|
|
|
if needClientTime {
|
|
|
|
result.ClientTime = signatureDateTime
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2021-08-09 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 22:43:36 +00:00
|
|
|
func (c center) checkAccessKeyID(accessKeyID string) error {
|
|
|
|
if len(c.allowedAccessKeyIDPrefixes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range c.allowedAccessKeyIDPrefixes {
|
|
|
|
if strings.HasPrefix(accessKeyID, prefix) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiErrors.GetAPIError(apiErrors.ErrAccessDenied)
|
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
func (c *center) checkFormData(r *http.Request) (*Box, error) {
|
2021-08-30 19:44:53 +00:00
|
|
|
if err := r.ParseMultipartForm(maxFormSizeMemory); err != nil {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidArgument)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := prepareForm(r.MultipartForm); err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse form: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := MultipartFormValue(r, "policy")
|
|
|
|
if policy == "" {
|
|
|
|
return nil, ErrNoAuthorizationHeader
|
|
|
|
}
|
|
|
|
|
2022-08-24 13:12:05 +00:00
|
|
|
submatches := c.postReg.GetSubmatches(MultipartFormValue(r, "x-amz-credential"))
|
2021-08-30 19:44:53 +00:00
|
|
|
if len(submatches) != 4 {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrAuthorizationHeaderMalformed)
|
|
|
|
}
|
|
|
|
|
|
|
|
signatureDateTime, err := time.Parse("20060102T150405Z", MultipartFormValue(r, "x-amz-date"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse x-amz-date field: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-25 17:25:43 +00:00
|
|
|
var addr oid.Address
|
|
|
|
if err = addr.DecodeString(strings.ReplaceAll(submatches["access_key_id"], "0", "/")); err != nil {
|
2021-08-30 19:44:53 +00:00
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrInvalidAccessKeyID)
|
|
|
|
}
|
|
|
|
|
2022-02-08 16:54:04 +00:00
|
|
|
box, err := c.cli.GetBox(r.Context(), addr)
|
2021-08-30 19:44:53 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("get box: %w", err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
secret := box.Gate.AccessKey
|
|
|
|
service, region := submatches["service"], submatches["region"]
|
|
|
|
|
|
|
|
signature := signStr(secret, service, region, signatureDateTime, policy)
|
|
|
|
if signature != MultipartFormValue(r, "x-amz-signature") {
|
|
|
|
return nil, apiErrors.GetAPIError(apiErrors.ErrSignatureDoesNotMatch)
|
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
return &Box{AccessBox: box}, nil
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 09:35:42 +00:00
|
|
|
func cloneRequest(r *http.Request, authHeader *authHeader) *http.Request {
|
2020-11-24 06:59:01 +00:00
|
|
|
otherRequest := r.Clone(context.TODO())
|
2021-02-01 16:59:42 +00:00
|
|
|
otherRequest.Header = make(http.Header)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-02-01 16:59:42 +00:00
|
|
|
for key, val := range r.Header {
|
2021-08-09 09:35:42 +00:00
|
|
|
for _, name := range authHeader.SignedFields {
|
2021-02-01 16:59:42 +00:00
|
|
|
if strings.EqualFold(key, name) {
|
|
|
|
otherRequest.Header[key] = val
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
if authHeader.IsPresigned {
|
|
|
|
otherQuery := otherRequest.URL.Query()
|
|
|
|
otherQuery.Del(AmzSignature)
|
|
|
|
otherRequest.URL.RawQuery = otherQuery.Encode()
|
|
|
|
}
|
|
|
|
|
2021-08-09 09:35:42 +00:00
|
|
|
return otherRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *center) checkSign(authHeader *authHeader, box *accessbox.Box, request *http.Request, signatureDateTime time.Time) error {
|
|
|
|
awsCreds := credentials.NewStaticCredentials(authHeader.AccessKeyID, box.Gate.AccessKey, "")
|
2020-08-06 15:23:01 +00:00
|
|
|
signer := v4.NewSigner(awsCreds)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
var signature string
|
|
|
|
if authHeader.IsPresigned {
|
|
|
|
now := time.Now()
|
|
|
|
if signatureDateTime.Add(authHeader.Expiration).Before(now) {
|
|
|
|
return apiErrors.GetAPIError(apiErrors.ErrExpiredPresignRequest)
|
|
|
|
}
|
|
|
|
if now.Before(signatureDateTime) {
|
|
|
|
return apiErrors.GetAPIError(apiErrors.ErrBadRequest)
|
|
|
|
}
|
|
|
|
if _, err := signer.Presign(request, nil, authHeader.Service, authHeader.Region, authHeader.Expiration, signatureDateTime); err != nil {
|
|
|
|
return fmt.Errorf("failed to pre-sign temporary HTTP request: %w", err)
|
|
|
|
}
|
|
|
|
signature = request.URL.Query().Get(AmzSignature)
|
|
|
|
} else {
|
|
|
|
signer.DisableURIPathEscaping = true
|
|
|
|
if _, err := signer.Sign(request, nil, authHeader.Service, authHeader.Region, signatureDateTime); err != nil {
|
|
|
|
return fmt.Errorf("failed to sign temporary HTTP request: %w", err)
|
|
|
|
}
|
2022-08-24 13:12:05 +00:00
|
|
|
signature = c.reg.GetSubmatches(request.Header.Get(AuthorizationHdr))["v4_signature"]
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
if authHeader.SignatureV4 != signature {
|
2021-08-09 09:35:42 +00:00
|
|
|
return apiErrors.GetAPIError(apiErrors.ErrSignatureDoesNotMatch)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-08-09 09:35:42 +00:00
|
|
|
return nil
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
|
|
|
|
func signStr(secret, service, region string, t time.Time, strToSign string) string {
|
|
|
|
creds := deriveKey(secret, service, region, t)
|
|
|
|
signature := hmacSHA256(creds, []byte(strToSign))
|
|
|
|
return hex.EncodeToString(signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deriveKey(secret, service, region string, t time.Time) []byte {
|
|
|
|
hmacDate := hmacSHA256([]byte("AWS4"+secret), []byte(t.UTC().Format("20060102")))
|
|
|
|
hmacRegion := hmacSHA256(hmacDate, []byte(region))
|
|
|
|
hmacService := hmacSHA256(hmacRegion, []byte(service))
|
|
|
|
return hmacSHA256(hmacService, []byte("aws4_request"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func hmacSHA256(key []byte, data []byte) []byte {
|
|
|
|
hash := hmac.New(sha256.New, key)
|
|
|
|
hash.Write(data)
|
|
|
|
return hash.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// MultipartFormValue gets value by key from multipart form.
|
2021-08-30 19:44:53 +00:00
|
|
|
func MultipartFormValue(r *http.Request, key string) string {
|
|
|
|
if r.MultipartForm == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if vs := r.MultipartForm.Value[key]; len(vs) > 0 {
|
|
|
|
return vs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareForm(form *multipart.Form) error {
|
|
|
|
var oldKeysValue []string
|
|
|
|
var oldKeysFile []string
|
|
|
|
|
|
|
|
for k, v := range form.Value {
|
|
|
|
lowerKey := strings.ToLower(k)
|
|
|
|
if lowerKey != k {
|
|
|
|
form.Value[lowerKey] = v
|
|
|
|
oldKeysValue = append(oldKeysValue, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, k := range oldKeysValue {
|
|
|
|
delete(form.Value, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range form.File {
|
|
|
|
lowerKey := strings.ToLower(k)
|
|
|
|
if lowerKey != "file" {
|
|
|
|
oldKeysFile = append(oldKeysFile, k)
|
|
|
|
if len(v) > 0 {
|
|
|
|
field, err := v[0].Open()
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("file header open: %w", err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := io.ReadAll(field)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("read field: %w", err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
form.Value[lowerKey] = []string{string(data)}
|
|
|
|
}
|
|
|
|
} else if lowerKey != k {
|
|
|
|
form.File[lowerKey] = v
|
|
|
|
oldKeysFile = append(oldKeysFile, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, k := range oldKeysFile {
|
|
|
|
delete(form.File, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|