public string SignRequestUrl()
        {
            var endpointBuilder = new UriBuilder("wss", Host, 443, "mqtt");

            var signer = new AWS4SignerForQueryParameterAuth
            {
                EndpointUri = endpointBuilder.Uri,
                HttpMethod  = "GET",
                Service     = "iotdevicegateway",
                Region      = this.Region
            };

            var headers = new Dictionary <string, string>();

            var authorization = signer.ComputeSignature(headers,
                                                        string.Empty,
                                                        AWS4SignerBase.EMPTY_BODY_SHA256,
                                                        this.AccessKey,
                                                        this.SecretKey);

            var signedRequestBuilder = new UriBuilder(endpointBuilder.Uri);

            signedRequestBuilder.Query = authorization;

            return(signedRequestBuilder.Uri.AbsoluteUri);
        }
コード例 #2
0
        static void WebsocketTest()
        {
            logger = new MqttNetLogger();
            logger.LogMessagePublished += LoggerOnLogMessagePublished;
            var endpoint    = Endpoint;
            var queryParams = new StringBuilder();
            var expiresOn   = DateTime.UtcNow.AddDays(2);
            var period      = Convert.ToInt64((expiresOn.ToUniversalTime() - DateTime.UtcNow).TotalSeconds);

            queryParams.AppendFormat("{0}={1}", AWS4SignerBase.X_Amz_Expires, HttpHelpers.UrlEncode(period.ToString()));

            var headers = new Dictionary <string, string> {
            };
            var signer  = new AWS4SignerForQueryParameterAuth()
            {
                EndpointUri = new Uri(endpoint),
                HttpMethod  = "GET",
                Region      = "us-east-1",
                Service     = "iotdevicegateway"
            };

            var authorization = signer.ComputeSignature(headers,
                                                        queryParams.ToString(),
                                                        AWS4SignerBase.EMPTY_BODY_SHA256,
                                                        ACCESS_KEY,
                                                        SECRET_KEY);

            var urlBuilder = new StringBuilder(endpoint.ToString());

            urlBuilder.AppendFormat("?{0}", queryParams.ToString());
            urlBuilder.AppendFormat("&{0}", authorization);
            var presignedUrl = urlBuilder.ToString();

            Task.Run(() => ConnectWs(presignedUrl));
        }
        private void DoRetrieve(string awsKey, string awsSecret, string awsToken, string messageId)
        {
            var retrievePath = "message";
            var uri          = new Uri(string.Format("https://{0}/{1}", BuildPath(retrievePath), messageId));
            var bodyJson     = "";

            // Empty hash
            var contentHash       = AWS4SignerBase.EMPTY_BODY_SHA256;
            var contentHashString = contentHash;

            var headers = new Dictionary <string, string> {
                { AWS4SignerBase.X_Amz_Content_SHA256, contentHashString },
                { "content-length", bodyJson.Length.ToString() },
                { "content-type", "application/json" },
                { AWS4SignerBase.X_Amz_Security_Token, awsToken }
            };

            var signer = new AWS4SignerForQueryParameterAuth {
                EndpointUri = uri,
                HttpMethod  = "GET",
                Service     = "execute-api",
                Region      = ApiGatewayRegionField.text
            };

            var authorization = signer.ComputeSignature(
                headers,
                string.Empty,
                contentHashString,
                awsKey,
                awsSecret);

            headers.Add("Authorization", authorization);
            var payloadBytes = Encoding.UTF8.GetBytes(bodyJson);

            headers.Remove("Host");
            WWW www = new WWW(uri.AbsoluteUri, null, headers);

            Send(www, (resp => {
                if (string.IsNullOrEmpty(www.error))
                {
                    Log(www.text);
                }
                else
                {
                    Log("Error: " + www.error);
                }
            }));
        }
コード例 #4
0
        /// <summary>
        /// Runs a http request to the specified endpoint
        /// </summary>
        /// <param name="endpointUri">
        /// "https://service.chime.aws.amazon.com/meetings"
        /// </param>
        /// <param name="requestType">
        /// "POST"
        /// </param>
        /// <param name="body">
        /// new { ClientRequestToken = "75341496-0878-440c-9db1-a7006c25a39f", MediaRegion = "us-east-1" }
        /// </param>
        /// <param name="service">
        /// // service = "chime"
        /// </param>
        /// <param name="region">
        /// "us-east-1"
        /// </param>
        /// <param name="queryParameters">
        /// ""
        /// </param>
        public static async Task <HttpWebResponse> Run(string endpointUri, string requestType, string service, string region, object body, string queryParameters)
        {
            var    uri            = new Uri(endpointUri);
            var    headers        = new Dictionary <string, string>();
            var    authorization  = string.Empty;
            string serializedBody = null;

            switch (requestType)
            {
            case Http.Post:
                // precompute hash of the body content
                serializedBody = JsonConvert.SerializeObject(body);
                var contentHash       = AWS4SignerBase.CanonicalRequestHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(serializedBody));
                var contentHashString = AWS4SignerBase.ToHexString(contentHash, true);

                headers.Add(AWS4SignerBase.X_Amz_Content_SHA256, contentHashString);
                headers.Add("content-length", serializedBody.Length.ToString());
                headers.Add("content-type", "text/json");

                var postSigner = new AWS4SignerForAuthorizationHeader
                {
                    EndpointUri = uri,
                    HttpMethod  = Http.Post,
                    Service     = service,
                    Region      = region
                };

                authorization = postSigner.ComputeSignature(headers, queryParameters, contentHashString, Settings.AWSAccessKey, Settings.AWSSecretKey);

                // express authorization for this as a header
                headers.Add("Authorization", authorization);

                return(await HttpHelpers.InvokeHttpRequest(uri, Http.Post, headers, serializedBody));

            case Http.Get:
                headers.Add(AWS4SignerBase.X_Amz_Content_SHA256, Settings.EMPTY_BODY_SHA256);
                headers.Add("content-type", "text/plain");

                var getSigner = new AWS4SignerForQueryParameterAuth
                {
                    EndpointUri = uri,
                    HttpMethod  = Http.Get,
                    Service     = service,
                    Region      = region
                };

                authorization = getSigner.ComputeSignature(headers, queryParameters, Settings.EMPTY_BODY_SHA256, Settings.AWSAccessKey, Settings.AWSSecretKey);

                var urlBuilder = new StringBuilder(endpointUri);
                if (!string.IsNullOrEmpty(queryParameters))
                {
                    urlBuilder.AppendFormat($"?{queryParameters}");
                    urlBuilder.AppendFormat($"&{authorization}");
                }
                else
                {
                    urlBuilder.AppendFormat($"?{authorization}");
                }

                var presignedUrl = urlBuilder.ToString();

                return(await HttpHelpers.InvokeHttpRequest(new Uri(presignedUrl), Http.Get, headers, serializedBody));

            default: throw new Exception("Unknown request type");
            }
        }