public void SignUtilsTest()
        {
            var request    = new ServiceRequest();
            var signString = SignUtils.BuildCanonicalString("PUT", "/bucket", request);

            Assert.AreEqual(signString, "PUT\n\n\n/bucket");

            request    = new ServiceRequestMock();
            signString = SignUtils.BuildCanonicalString("PUT", "/bucket", request);
            Assert.AreEqual(signString, "PUT\n\n\n/bucket");
        }
Пример #2
0
        public static void Sign(OssHttpRequestMessage ossHttpRequestMessage, NetworkCredential networkCredential)
        {
            string accessKeyId = networkCredential.UserName;
            string secretAccessKey = networkCredential.Password;
            if (!string.IsNullOrEmpty(secretAccessKey))
            {
                string canonicalString = SignUtils.BuildCanonicalString(ossHttpRequestMessage);
                string signature = ServiceSignature.Create().ComputeSignature(secretAccessKey, canonicalString);
                ossHttpRequestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("OSS", accessKeyId + ":" + signature);

            }
            else
            {
                ossHttpRequestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", accessKeyId);
            }


        }
Пример #3
0
        /// <inheritdoc/>
        public Uri GeneratePresignedUri(GeneratePresignedUriRequest generatePresignedUriRequest)
        {
            ThrowIfNullRequest(generatePresignedUriRequest);

            var creds         = _credsProvider.GetCredentials();
            var accessId      = creds.AccessId;
            var accessKey     = creds.AccessKey;
            var securityToken = creds.SecurityToken;
            var useToken      = creds.UseToken;
            var bucketName    = generatePresignedUriRequest.BucketName;
            var key           = generatePresignedUriRequest.Key;

            const long ticksOf1970 = 621355968000000000;
            var        expires     = ((generatePresignedUriRequest.Expiration.ToUniversalTime().Ticks - ticksOf1970) / 10000000L)
                                     .ToString(CultureInfo.InvariantCulture);
            var resourcePath = OssUtils.MakeResourcePath(key);

            var request = new ServiceRequest();
            var conf    = OssUtils.GetClientConfiguration(_serviceClient);

            request.Endpoint     = OssUtils.MakeBucketEndpoint(_endpoint, bucketName, conf);
            request.ResourcePath = resourcePath;

            switch (generatePresignedUriRequest.Method)
            {
            case SignHttpMethod.Get:
                request.Method = HttpMethod.Get;
                break;

            case SignHttpMethod.Put:
                request.Method = HttpMethod.Put;
                break;

            default:
                throw new ArgumentException("Unsupported http method.");
            }

            request.Headers.Add(HttpHeaders.Date, expires);
            if (!string.IsNullOrEmpty(generatePresignedUriRequest.ContentType))
            {
                request.Headers.Add(HttpHeaders.ContentType, generatePresignedUriRequest.ContentType);
            }
            if (!string.IsNullOrEmpty(generatePresignedUriRequest.ContentMd5))
            {
                request.Headers.Add(HttpHeaders.ContentMd5, generatePresignedUriRequest.ContentMd5);
            }

            foreach (var pair in generatePresignedUriRequest.UserMetadata)
            {
                request.Headers.Add(OssHeaders.OssUserMetaPrefix + pair.Key, pair.Value);
            }

            if (generatePresignedUriRequest.ResponseHeaders != null)
            {
                generatePresignedUriRequest.ResponseHeaders.Populate(request.Parameters);
            }

            foreach (var param in generatePresignedUriRequest.QueryParams)
            {
                request.Parameters.Add(param.Key, param.Value);
            }

            if (useToken)
            {
                request.Parameters.Add(RequestParameters.SECURITY_TOKEN, securityToken);
            }

            var canonicalResource = "/" + (bucketName ?? "") + ((key != null ? "/" + key : ""));
            var httpMethod        = generatePresignedUriRequest.Method.ToString().ToUpperInvariant();

            var canonicalString =
                SignUtils.BuildCanonicalString(httpMethod, canonicalResource, request /*, expires*/);
            var signature = ServiceSignature.Create().ComputeSignature(accessKey, canonicalString);

            IDictionary <string, string> queryParams = new Dictionary <string, string>();

            queryParams.Add(RequestParameters.EXPIRES, expires);
            queryParams.Add(RequestParameters.OSS_ACCESS_KEY_ID, accessId);
            queryParams.Add(RequestParameters.SIGNATURE, signature);
            foreach (var param in request.Parameters)
            {
                queryParams.Add(param.Key, param.Value);
            }

            var queryString = HttpUtils.ConbineQueryString(queryParams);
            var uriString   = request.Endpoint.ToString();

            if (!uriString.EndsWith("/"))
            {
                uriString += "/";
            }
            uriString += resourcePath + "?" + queryString;

            return(new Uri(uriString));
        }