aksk.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 mcclient
  15. import (
  16. "context"
  17. "io"
  18. "net/http"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/netutils"
  23. "yunion.io/x/pkg/util/s3auth"
  24. api "yunion.io/x/onecloud/pkg/apis/identity"
  25. )
  26. type SAkskTokenCredential struct {
  27. AccessKeySecret api.SAccessKeySecretInfo
  28. Token TokenCredential
  29. }
  30. func (this *Client) _verifyKeySecret(aksk s3auth.IAccessKeySecretRequest, aCtx SAuthContext) (*SAkskTokenCredential, error) {
  31. input := SAuthenticationInputV3{}
  32. input.Auth.Identity.Methods = []string{api.AUTH_METHOD_AKSK}
  33. input.Auth.Identity.AccessKeyRequest = aksk.Encode()
  34. input.Auth.Context = aCtx
  35. hdr, rbody, err := this.jsonRequest(context.Background(), this.authUrl, "", "POST", "/auth/tokens", nil, jsonutils.Marshal(&input))
  36. if err != nil {
  37. return nil, err
  38. }
  39. tokenId := hdr.Get("X-Subject-Token")
  40. if len(tokenId) == 0 {
  41. return nil, errors.Error("No X-Subject-Token in header")
  42. }
  43. ret := SAkskTokenCredential{}
  44. ret.Token, err = this.unmarshalV3Token(rbody, tokenId)
  45. if err != nil {
  46. return nil, errors.Wrap(err, "unmarshalV3Token")
  47. }
  48. ret.AccessKeySecret = ret.Token.(*TokenCredentialV3).Token.AccessKey
  49. return &ret, nil
  50. }
  51. func (this *Client) VerifyRequest(req http.Request, aksk s3auth.IAccessKeySecretRequest, virtualHost bool) (*SAkskTokenCredential, error) {
  52. cliIp := netutils.GetHttpRequestIp(&req)
  53. aCtx := SAuthContext{
  54. Source: AuthSourceSrv,
  55. Ip: cliIp,
  56. }
  57. token, err := this._verifyKeySecret(aksk, aCtx)
  58. if err != nil {
  59. return nil, errors.Wrap(err, "this._verifyKeySecret")
  60. }
  61. return token, nil
  62. }
  63. func jsonReader(input interface{}) io.Reader {
  64. return strings.NewReader(jsonutils.Marshal(input).String())
  65. }
  66. func (this *Client) AuthenticateByAccessKey(accessKey string, secret string, source string) (TokenCredential, error) {
  67. aCtx := SAuthContext{Source: source}
  68. seedAksk := s3auth.NewV4Request()
  69. seedAksk.AccessKey = accessKey
  70. seedAksk.Location = "cn-beijing"
  71. input := SAuthenticationInputV3{}
  72. input.Auth.Identity.Methods = []string{api.AUTH_METHOD_AKSK}
  73. input.Auth.Identity.AccessKeyRequest = seedAksk.Encode()
  74. input.Auth.Context = aCtx
  75. urlStr := joinUrl(this.authUrl, "/auth/tokens")
  76. req, err := http.NewRequest(http.MethodPost, urlStr, jsonReader(input))
  77. if err != nil {
  78. return nil, errors.Wrap(err, "http.NewRequest")
  79. }
  80. newreq := s3auth.SignV4(*req, seedAksk.AccessKey, secret, seedAksk.Location, jsonReader(input))
  81. aksk, err := s3auth.DecodeAccessKeyRequest(*newreq, false)
  82. if err != nil {
  83. return nil, errors.Wrap(err, "s3auth.DecodeAccessKeyRequest")
  84. }
  85. token, err := this._verifyKeySecret(aksk, aCtx)
  86. if err != nil {
  87. return nil, errors.Wrap(err, "this._verifyKeySecret")
  88. }
  89. return token.Token, nil
  90. }