parent
eba86c07e9
commit
81fc11ee5d
@ -1,191 +1,155 @@ |
|||||||
package signers |
package signers |
||||||
|
|
||||||
//import (
|
import ( |
||||||
// "bytes"
|
"bytes" |
||||||
// "crypto/hmac"
|
"crypto/hmac" |
||||||
// "crypto/sha1"
|
"crypto/sha1" |
||||||
// "encoding/base64"
|
"encoding/base64" |
||||||
// "fmt"
|
"fmt" |
||||||
// "io"
|
"io" |
||||||
// "net/http"
|
"net/http" |
||||||
// "net/url"
|
"net/url" |
||||||
// "sort"
|
"sort" |
||||||
// "strings"
|
"strings" |
||||||
//
|
"time" |
||||||
// "github.com/minio-io/minio/pkg/utils/database"
|
|
||||||
//)
|
"github.com/minio-io/minio/pkg/utils/config" |
||||||
//
|
) |
||||||
//func ValidateAccessKey(key []byte) bool {
|
|
||||||
// for _, char := range key {
|
func SignRequest(user config.User, req *http.Request) { |
||||||
// if isalnum(char) {
|
if date := req.Header.Get("Date"); date == "" { |
||||||
// continue
|
req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat)) |
||||||
// }
|
} |
||||||
// switch char {
|
hm := hmac.New(sha1.New, []byte(user.SecretKey)) |
||||||
// case '-':
|
ss := getStringToSign(req) |
||||||
// case '.':
|
io.WriteString(hm, ss) |
||||||
// case '_':
|
|
||||||
// case '~':
|
authHeader := new(bytes.Buffer) |
||||||
// continue
|
fmt.Fprintf(authHeader, "AWS %s:", user.AccessKey) |
||||||
// default:
|
encoder := base64.NewEncoder(base64.StdEncoding, authHeader) |
||||||
// return false
|
encoder.Write(hm.Sum(nil)) |
||||||
// }
|
encoder.Close() |
||||||
// }
|
req.Header.Set("Authorization", authHeader.String()) |
||||||
// return true
|
} |
||||||
//}
|
|
||||||
//
|
// This package implements verification side of Object API Signature request
|
||||||
//func getAccessID() {
|
func ValidateRequest(user config.User, req *http.Request) (bool, error) { |
||||||
//}
|
if date := req.Header.Get("Date"); date == "" { |
||||||
//
|
return false, fmt.Errorf("Date should be set") |
||||||
//func getSecretID() {
|
} |
||||||
//}
|
hm := hmac.New(sha1.New, []byte(user.SecretKey)) |
||||||
//
|
ss := getStringToSign(req) |
||||||
//// This package implements verification side of Object API Signature request
|
io.WriteString(hm, ss) |
||||||
//func ValidateRequest(req *http.Request) (bool, error) {
|
|
||||||
// if date := req.Header.Get("Date"); date == "" {
|
authHeader := new(bytes.Buffer) |
||||||
// return false, fmt.Errorf("Date should be set")
|
fmt.Fprintf(authHeader, "AWS %s:", user.AccessKey) |
||||||
// }
|
encoder := base64.NewEncoder(base64.StdEncoding, authHeader) |
||||||
// hm := hmac.New(sha1.New, []byte(SecretAccessKey))
|
encoder.Write(hm.Sum(nil)) |
||||||
// ss := getStringToSign(req)
|
encoder.Close() |
||||||
// io.WriteString(hm, ss)
|
|
||||||
// authHeader := new(bytes.Buffer)
|
if req.Header.Get("Authorization") != authHeader.String() { |
||||||
// if req.Header.Get("User-Agent") == "Minio" {
|
return false, fmt.Errorf("Authorization header mismatch") |
||||||
// fmt.Fprintf(authHeader, "MINIO %s:", AccessKey)
|
} |
||||||
// } else {
|
return true, nil |
||||||
// fmt.Fprintf(authHeader, "AWS %s:", AccessKey)
|
} |
||||||
// }
|
|
||||||
// encoder := base64.NewEncoder(base64.StdEncoding, authHeader)
|
// From the Amazon docs:
|
||||||
// encoder.Write(hm.Sum(nil))
|
|
||||||
// defer encoder.Close()
|
|
||||||
// if req.Header.Get("Authorization") != authHeader.String() {
|
|
||||||
// return false, fmt.Errorf("Authorization header mismatch")
|
|
||||||
// }
|
|
||||||
// return true, nil
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// From the Amazon docs:
|
|
||||||
////
|
|
||||||
//// StringToSign = HTTP-Verb + "\n" +
|
|
||||||
//// Content-MD5 + "\n" +
|
|
||||||
//// Content-Type + "\n" +
|
|
||||||
//// Date + "\n" +
|
|
||||||
//// CanonicalizedAmzHeaders +
|
|
||||||
//// CanonicalizedResource;
|
|
||||||
//func getStringToSign(req *http.Request) string {
|
|
||||||
// buf := new(bytes.Buffer)
|
|
||||||
// buf.WriteString(req.Method)
|
|
||||||
// buf.WriteByte('\n')
|
|
||||||
// buf.WriteString(req.Header.Get("Content-MD5"))
|
|
||||||
// buf.WriteByte('\n')
|
|
||||||
// buf.WriteString(req.Header.Get("Content-Type"))
|
|
||||||
// buf.WriteByte('\n')
|
|
||||||
// if req.Header.Get("x-amz-date") == "" {
|
|
||||||
// buf.WriteString(req.Header.Get("Date"))
|
|
||||||
// }
|
|
||||||
// buf.WriteByte('\n')
|
|
||||||
// writeCanonicalizedAmzHeaders(buf, req)
|
|
||||||
// writeCanonicalizedResource(buf, req)
|
|
||||||
// return buf.String()
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func hasPrefixCaseInsensitive(s, pfx string) bool {
|
|
||||||
// if len(pfx) > len(s) {
|
|
||||||
// return false
|
|
||||||
// }
|
|
||||||
// shead := s[:len(pfx)]
|
|
||||||
// if shead == pfx {
|
|
||||||
// return true
|
|
||||||
// }
|
|
||||||
// shead = strings.ToLower(shead)
|
|
||||||
// return shead == pfx || shead == strings.ToLower(pfx)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func writeCanonicalizedAmzHeaders(buf *bytes.Buffer, req *http.Request) {
|
|
||||||
// amzHeaders := make([]string, 0)
|
|
||||||
// vals := make(map[string][]string)
|
|
||||||
// for k, vv := range req.Header {
|
|
||||||
// if hasPrefixCaseInsensitive(k, "x-amz-") {
|
|
||||||
// lk := strings.ToLower(k)
|
|
||||||
// amzHeaders = append(amzHeaders, lk)
|
|
||||||
// vals[lk] = vv
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// sort.Strings(amzHeaders)
|
|
||||||
// for _, k := range amzHeaders {
|
|
||||||
// buf.WriteString(k)
|
|
||||||
// buf.WriteByte(':')
|
|
||||||
// for idx, v := range vals[k] {
|
|
||||||
// if idx > 0 {
|
|
||||||
// buf.WriteByte(',')
|
|
||||||
// }
|
|
||||||
// if strings.Contains(v, "\n") {
|
|
||||||
// // TODO: "Unfold" long headers that
|
|
||||||
// // span multiple lines (as allowed by
|
|
||||||
// // RFC 2616, section 4.2) by replacing
|
|
||||||
// // the folding white-space (including
|
|
||||||
// // new-line) by a single space.
|
|
||||||
// buf.WriteString(v)
|
|
||||||
// } else {
|
|
||||||
// buf.WriteString(v)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// buf.WriteByte('\n')
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//// Must be sorted:
|
|
||||||
//var subResList = []string{"acl", "lifecycle", "location", "logging", "notification", "partNumber", "policy", "requestPayment", "torrent", "uploadId", "uploads", "versionId", "versioning", "versions", "website"}
|
|
||||||
//
|
|
||||||
//// From the Amazon docs:
|
|
||||||
////
|
|
||||||
//// CanonicalizedResource = [ "/" + Bucket ] +
|
|
||||||
//// <HTTP-Request-URI, from the protocol name up to the query string> +
|
|
||||||
//// [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
|
|
||||||
//func writeCanonicalizedResource(buf *bytes.Buffer, req *http.Request) {
|
|
||||||
// bucket := getBucketFromHostname(req)
|
|
||||||
// if bucket != "" {
|
|
||||||
// buf.WriteByte('/')
|
|
||||||
// buf.WriteString(bucket)
|
|
||||||
// }
|
|
||||||
// buf.WriteString(req.URL.Path)
|
|
||||||
// if req.URL.RawQuery != "" {
|
|
||||||
// n := 0
|
|
||||||
// vals, _ := url.ParseQuery(req.URL.RawQuery)
|
|
||||||
// for _, subres := range subResList {
|
|
||||||
// if vv, ok := vals[subres]; ok && len(vv) > 0 {
|
|
||||||
// n++
|
|
||||||
// if n == 1 {
|
|
||||||
// buf.WriteByte('?')
|
|
||||||
// } else {
|
|
||||||
// buf.WriteByte('&')
|
|
||||||
// }
|
|
||||||
// buf.WriteString(subres)
|
|
||||||
// if len(vv[0]) > 0 {
|
|
||||||
// buf.WriteByte('=')
|
|
||||||
// buf.WriteString(url.QueryEscape(vv[0]))
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//
|
//
|
||||||
//// hasDotSuffix reports whether s ends with "." + suffix.
|
// StringToSign = HTTP-Verb + "\n" +
|
||||||
//func hasDotSuffix(s string, suffix string) bool {
|
// Content-MD5 + "\n" +
|
||||||
// return len(s) >= len(suffix)+1 && strings.HasSuffix(s, suffix) && s[len(s)-len(suffix)-1] == '.'
|
// Content-Type + "\n" +
|
||||||
//}
|
// Date + "\n" +
|
||||||
|
// CanonicalizedAmzHeaders +
|
||||||
|
// CanonicalizedResource;
|
||||||
|
func getStringToSign(req *http.Request) string { |
||||||
|
buf := new(bytes.Buffer) |
||||||
|
buf.WriteString(req.Method) |
||||||
|
buf.WriteByte('\n') |
||||||
|
buf.WriteString(req.Header.Get("Content-MD5")) |
||||||
|
buf.WriteByte('\n') |
||||||
|
buf.WriteString(req.Header.Get("Content-Type")) |
||||||
|
buf.WriteByte('\n') |
||||||
|
if req.Header.Get("x-amz-date") == "" { |
||||||
|
buf.WriteString(req.Header.Get("Date")) |
||||||
|
} |
||||||
|
buf.WriteByte('\n') |
||||||
|
writeCanonicalizedAmzHeaders(buf, req) |
||||||
|
writeCanonicalizedResource(buf, req) |
||||||
|
return buf.String() |
||||||
|
} |
||||||
|
|
||||||
|
func hasPrefixCaseInsensitive(s, pfx string) bool { |
||||||
|
if len(pfx) > len(s) { |
||||||
|
return false |
||||||
|
} |
||||||
|
shead := s[:len(pfx)] |
||||||
|
if shead == pfx { |
||||||
|
return true |
||||||
|
} |
||||||
|
shead = strings.ToLower(shead) |
||||||
|
return shead == pfx || shead == strings.ToLower(pfx) |
||||||
|
} |
||||||
|
|
||||||
|
func writeCanonicalizedAmzHeaders(buf *bytes.Buffer, req *http.Request) { |
||||||
|
amzHeaders := make([]string, 0) |
||||||
|
vals := make(map[string][]string) |
||||||
|
for k, vv := range req.Header { |
||||||
|
if hasPrefixCaseInsensitive(k, "x-amz-") { |
||||||
|
lk := strings.ToLower(k) |
||||||
|
amzHeaders = append(amzHeaders, lk) |
||||||
|
vals[lk] = vv |
||||||
|
} |
||||||
|
} |
||||||
|
sort.Strings(amzHeaders) |
||||||
|
for _, k := range amzHeaders { |
||||||
|
buf.WriteString(k) |
||||||
|
buf.WriteByte(':') |
||||||
|
for idx, v := range vals[k] { |
||||||
|
if idx > 0 { |
||||||
|
buf.WriteByte(',') |
||||||
|
} |
||||||
|
if strings.Contains(v, "\n") { |
||||||
|
// TODO: "Unfold" long headers that
|
||||||
|
// span multiple lines (as allowed by
|
||||||
|
// RFC 2616, section 4.2) by replacing
|
||||||
|
// the folding white-space (including
|
||||||
|
// new-line) by a single space.
|
||||||
|
buf.WriteString(v) |
||||||
|
} else { |
||||||
|
buf.WriteString(v) |
||||||
|
} |
||||||
|
} |
||||||
|
buf.WriteByte('\n') |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Must be sorted:
|
||||||
|
var subResList = []string{"acl", "lifecycle", "location", "logging", "notification", "partNumber", "policy", "requestPayment", "torrent", "uploadId", "uploads", "versionId", "versioning", "versions", "website"} |
||||||
|
|
||||||
|
// From the Amazon docs:
|
||||||
//
|
//
|
||||||
//func getBucketFromHostname(req *http.Request) string {
|
// CanonicalizedResource = [ "/" + Bucket ] +
|
||||||
// host := req.Host
|
// <HTTP-Request-URI, from the protocol name up to the query string> +
|
||||||
// if host == "" {
|
// [ sub-resource, if present. For example "?acl", "?location", "?logging", or "?torrent"];
|
||||||
// host = req.URL.Host
|
func writeCanonicalizedResource(buf *bytes.Buffer, req *http.Request) { |
||||||
// }
|
buf.WriteString(req.URL.Path) |
||||||
// if host == "s3.amazonaws.com" {
|
if req.URL.RawQuery != "" { |
||||||
// return ""
|
n := 0 |
||||||
// }
|
vals, _ := url.ParseQuery(req.URL.RawQuery) |
||||||
// if hostSuffix := "s3.amazonaws.com"; hasDotSuffix(host, hostSuffix) {
|
for _, subres := range subResList { |
||||||
// return host[:len(host)-len(hostSuffix)-1]
|
if vv, ok := vals[subres]; ok && len(vv) > 0 { |
||||||
// }
|
n++ |
||||||
// if lastColon := strings.LastIndex(host, ":"); lastColon != -1 {
|
if n == 1 { |
||||||
// return host[:lastColon]
|
buf.WriteByte('?') |
||||||
// }
|
} else { |
||||||
// return host
|
buf.WriteByte('&') |
||||||
//}
|
} |
||||||
|
buf.WriteString(subres) |
||||||
|
if len(vv[0]) > 0 { |
||||||
|
buf.WriteByte('=') |
||||||
|
buf.WriteString(url.QueryEscape(vv[0])) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue