github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/obs/authV2.go (about)

     1  // Copyright 2019 Huawei Technologies Co.,Ltd.
     2  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
     3  // this file except in compliance with the License.  You may obtain a copy of the
     4  // License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software distributed
     9  // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    10  // CONDITIONS OF ANY KIND, either express or implied.  See the License for the
    11  // specific language governing permissions and limitations under the License.
    12  
    13  package obs
    14  
    15  import (
    16  	"strings"
    17  )
    18  
    19  func getV2StringToSign(method, canonicalizedURL string, headers map[string][]string, isObs bool) string {
    20  	stringToSign := strings.Join([]string{method, "\n", attachHeaders(headers, isObs), "\n", canonicalizedURL}, "")
    21  
    22  	var isSecurityToken bool
    23  	var securityToken []string
    24  	if isObs {
    25  		securityToken, isSecurityToken = headers[HEADER_STS_TOKEN_OBS]
    26  	} else {
    27  		securityToken, isSecurityToken = headers[HEADER_STS_TOKEN_AMZ]
    28  	}
    29  	var query []string
    30  	if !isSecurityToken {
    31  		parmas := strings.Split(canonicalizedURL, "?")
    32  		if len(parmas) > 1 {
    33  			query = strings.Split(parmas[1], "&")
    34  			for _, value := range query {
    35  				if strings.HasPrefix(value, HEADER_STS_TOKEN_AMZ+"=") || strings.HasPrefix(value, HEADER_STS_TOKEN_OBS+"=") {
    36  					if value[len(HEADER_STS_TOKEN_AMZ)+1:] != "" {
    37  						securityToken = []string{value[len(HEADER_STS_TOKEN_AMZ)+1:]}
    38  						isSecurityToken = true
    39  					}
    40  				}
    41  			}
    42  		}
    43  	}
    44  	logStringToSign := stringToSign
    45  	if isSecurityToken && len(securityToken) > 0 {
    46  		logStringToSign = strings.Replace(logStringToSign, securityToken[0], "******", -1)
    47  	}
    48  	doLog(LEVEL_DEBUG, "The v2 auth stringToSign:\n%s", logStringToSign)
    49  	return stringToSign
    50  }
    51  
    52  func v2Auth(ak, sk, method, canonicalizedURL string, headers map[string][]string, isObs bool) map[string]string {
    53  	stringToSign := getV2StringToSign(method, canonicalizedURL, headers, isObs)
    54  	return map[string]string{"Signature": Base64Encode(HmacSha1([]byte(sk), []byte(stringToSign)))}
    55  }