Example #1
0
        internal Signer GetSigner(HttpContext context)
        {
            switch (context.ChooseAuthType)
            {
            case AuthTypeEnum.V2:
                return(V2Signer.GetInstance());

            case AuthTypeEnum.V4:
                return(V4Signer.GetInstance());

            default:
                return(ObsSigner.GetInstance());
            }
        }
Example #2
0
        private CreateTemporarySignatureResponse CreateV4TemporarySignature(HttpRequest httpRequest, long expires, IHeaders iheaders)
        {
            IDictionary <string, string> dateDict = V4Signer.GetLongDateAndShortDate(httpRequest, iheaders);
            string host = httpRequest.GetHost(this.ObsConfig.Endpoint);

            CommonUtil.AddHeader(httpRequest, Constants.CommonHeaders.Host, host);

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

            foreach (KeyValuePair <string, string> entry in httpRequest.Headers)
            {
                if (string.IsNullOrEmpty(entry.Key))
                {
                    continue;
                }
                tempDict.Add(entry.Key.Trim().ToLower(), entry.Value);
            }

            List <string> signedHeadersList = V4Signer.GetSignedHeaderList(tempDict);
            string        signedHeaders     = V4Signer.GetSignedHeaders(signedHeadersList);

            CommonUtil.AddParam(httpRequest, "X-Amz-Algorithm", V4Signer.Algorithm);
            CommonUtil.AddParam(httpRequest, "X-Amz-Credential", this.sp.Ak + "/" + dateDict["ShortDate"] + V4Signer.ScopeSuffix);
            CommonUtil.AddParam(httpRequest, "X-Amz-Date", dateDict["LongDate"]);
            CommonUtil.AddParam(httpRequest, "X-Amz-Expires", expires.ToString());
            CommonUtil.AddParam(httpRequest, "X-Amz-SignedHeaders", signedHeaders);

            HttpContext context   = new HttpContext(this.sp, this.ObsConfig);
            string      signature = CommonUtil.UrlEncode(V4Signer.GetTemporarySignature(httpRequest, context, iheaders, dateDict, signedHeaders, tempDict, signedHeadersList, null));

            CreateTemporarySignatureResponse response = new CreateTemporarySignatureResponse();

            response.SignUrl = this.ObsConfig.Endpoint.StartsWith("https", StringComparison.OrdinalIgnoreCase) ?
                               "https://" : "http://";

            response.SignUrl += host;

            if (this.ObsConfig.PathStyle && !string.IsNullOrEmpty(httpRequest.BucketName))
            {
                response.SignUrl += "/" + CommonUtil.UrlEncode(httpRequest.BucketName);
            }

            if (!string.IsNullOrEmpty(httpRequest.ObjectKey))
            {
                response.SignUrl += "/" + CommonUtil.UrlEncode(httpRequest.ObjectKey, null, "/");
            }

            bool isFirst = true;

            foreach (KeyValuePair <string, string> entry in httpRequest.Params)
            {
                if (isFirst)
                {
                    response.SignUrl += "?";
                    isFirst           = false;
                }
                else
                {
                    response.SignUrl += "&";
                }
                response.SignUrl += CommonUtil.UrlEncode(entry.Key);
                response.SignUrl += "=";
                response.SignUrl += CommonUtil.UrlEncode(entry.Value);
            }

            if (!string.IsNullOrEmpty(this.sp.Token))
            {
                response.SignUrl += "&" + iheaders.SecurityTokenHeader() + "=" + this.sp.Token;
            }
            response.SignUrl += "&X-Amz-Signature=" + signature;

            foreach (KeyValuePair <string, string> entry in httpRequest.Headers)
            {
                if (!entry.Key.Equals(Constants.CommonHeaders.Date))
                {
                    response.ActualSignedRequestHeaders.Add(entry.Key, entry.Value);
                }
            }

            return(response);
        }