interface.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package s3auth
  15. import (
  16. "net/http"
  17. "strings"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. )
  21. type IAccessKeySecretRequest interface {
  22. GetAccessKey() string
  23. Validate() error
  24. ParseRequest(req http.Request, virtualHost bool) error
  25. Verify(secret string) error
  26. Encode() string
  27. }
  28. type SAccessKeyRequest struct {
  29. Algorithm string `json:"algorithm,omitempty"`
  30. AccessKey string `json:"access_key,omitempty"`
  31. Signature string `json:"signature,omitempty"`
  32. Request string `json:"request,omitempty"`
  33. }
  34. func (aksk SAccessKeyRequest) GetAccessKey() string {
  35. return aksk.AccessKey
  36. }
  37. func (aksk SAccessKeyRequest) Validate() error {
  38. if len(aksk.AccessKey) == 0 {
  39. return errors.Error("Missing AWSAccessKeyId")
  40. }
  41. if len(aksk.Signature) == 0 {
  42. return errors.Error("Missing Signature")
  43. }
  44. return nil
  45. }
  46. func decodeAuthHeader(authHeader string) (IAccessKeySecretRequest, error) {
  47. pos := strings.IndexByte(authHeader, ' ')
  48. if pos <= 0 {
  49. return nil, errors.Error("illegal authorization header")
  50. }
  51. algo := authHeader[:pos]
  52. switch algo {
  53. case signV2Algorithm:
  54. req, err := decodeAuthHeaderV2(authHeader[pos+1:])
  55. if err != nil {
  56. return nil, errors.Wrap(err, "decodeAuthHeaderV2")
  57. }
  58. return req, nil
  59. case signV4Algorithm:
  60. req, err := decodeAuthHeaderV4(authHeader[pos+1:])
  61. if err != nil {
  62. return nil, errors.Wrap(err, "decodeAuthHeaderV4")
  63. }
  64. return req, nil
  65. default:
  66. return nil, errors.Error("unsupported signing algorithm")
  67. }
  68. }
  69. func DecodeAccessKeyRequest(req http.Request, virtualHost bool) (IAccessKeySecretRequest, error) {
  70. authHeader := req.Header.Get("Authorization")
  71. if len(authHeader) == 0 {
  72. return nil, errors.Error("missing authorization header")
  73. }
  74. akskReq, err := decodeAuthHeader(authHeader)
  75. if err != nil {
  76. return nil, errors.Wrap(err, "decodeAuthHeader")
  77. }
  78. err = akskReq.ParseRequest(req, virtualHost)
  79. if err != nil {
  80. return nil, errors.Wrap(err, "akskReq.ParseRequest")
  81. }
  82. return akskReq, akskReq.Validate()
  83. }
  84. func Decode(reqStr string) (IAccessKeySecretRequest, error) {
  85. rawReq := SAccessKeyRequest{}
  86. reqJson, err := jsonutils.ParseString(reqStr)
  87. if err != nil {
  88. return nil, errors.Wrap(err, "jsonutils.ParseString")
  89. }
  90. err = reqJson.Unmarshal(&rawReq)
  91. if err != nil {
  92. return nil, errors.Wrap(err, "reqJson.Unmarshal rawReq")
  93. }
  94. var ret IAccessKeySecretRequest
  95. switch rawReq.Algorithm {
  96. case signV2Algorithm:
  97. ret = &SAccessKeyRequestV2{}
  98. case signV4Algorithm:
  99. ret = &SAccessKeyRequestV4{}
  100. default:
  101. return nil, errors.Error("unsupported sign algorithm")
  102. }
  103. err = reqJson.Unmarshal(ret)
  104. if err != nil {
  105. return nil, errors.Wrap(err, "reqJson.Unmarshal")
  106. }
  107. return ret, nil
  108. }