public static void ImportZipFile(string consumerKey, string secretKey, string filePath, string sisApiBaseUrl)
        {
            byte[] fileArray = File.ReadAllBytes(filePath);

            //oauth default parameters
            NameValueCollection postParams = initiatePostParams(consumerKey);

            //oauth_body_hash
            postParams["oauth_body_hash"] = computeHashFromBytes(fileArray);

            //calculate signature based on above parameters and put that as POST param (multi-part)
            postParams["oauth_signature"] = OAuthUtility.GenerateSignature("POST", new Uri(sisApiBaseUrl), postParams, secretKey);

            //send request
            byte[] result = null;
            using (var stream = File.Open(filePath, FileMode.Open))
            {
                var files = new[]
                {
                    new UploadFile
                    {
                        Name        = "upload_file",
                        Filename    = Path.GetFileName(filePath),
                        ContentType = "application/zip",
                        Stream      = stream
                    }
                };

                result = FileUploadHelper.UploadFiles(sisApiBaseUrl, files, postParams);
            }

            //this value is "import_id"
            Console.WriteLine(Encoding.Default.GetString(result));
        }
        /// <summary>
        /// Create an HttpWebRequest for a content-item service message. This is experimental.
        /// </summary>
        /// <param name="url">The content-item service URL.</param>
        /// <param name="consumerKey">The OAuth consumer key to use to sign the request.</param>
        /// <param name="consumerSecret">The OAuth consumer secret to use to sign the request.</param>
        /// <param name="response">The ContentItemsServiceResponse to POST.</param>
        /// <returns>The HttpWebRequest that will POST the message.</returns>
        private static HttpWebRequest CreateContentItemsRequest(
            string url, string consumerKey, string consumerSecret,
            ContentItemsServiceResponse response)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.Method      = "POST";
            webRequest.ContentType = LtiConstants.ContentItemsMediaType;

            var parameters = new NameValueCollection();

            parameters.AddParameter(OAuthConstants.ConsumerKeyParameter, consumerKey);
            parameters.AddParameter(OAuthConstants.NonceParameter, Guid.NewGuid().ToString());
            parameters.AddParameter(OAuthConstants.SignatureMethodParameter, OAuthConstants.SignatureMethodHmacSha1);
            parameters.AddParameter(OAuthConstants.VersionParameter, OAuthConstants.Version10);

            // Calculate the timestamp
            var ts        = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var timestamp = Convert.ToInt64(ts.TotalSeconds);

            parameters.AddParameter(OAuthConstants.TimestampParameter, timestamp);

            // Calculate the body hash
            using (var sha1 = new SHA1CryptoServiceProvider())
            {
                var json = JsonConvert.SerializeObject(response, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });
                var content = Encoding.Unicode.GetBytes(json);
                webRequest.ContentLength = content.Length;
                using (var stream = webRequest.GetRequestStream())
                {
                    stream.Write(content, 0, content.Length);
                }

                var hash   = sha1.ComputeHash(content);
                var hash64 = Convert.ToBase64String(hash);
                parameters.AddParameter(OAuthConstants.BodyHashParameter, hash64);
            }

            // Calculate the signature
            var signature = OAuthUtility.GenerateSignature(webRequest.Method, webRequest.RequestUri, parameters,
                                                           consumerSecret);

            parameters.AddParameter(OAuthConstants.SignatureParameter, signature);

            // Build the Authorization header
            var authorization = new StringBuilder(OAuthConstants.AuthScheme).Append(" ");

            foreach (var key in parameters.AllKeys)
            {
                authorization.AppendFormat("{0}=\"{1}\",", key, HttpUtility.UrlEncode(parameters[key]));
            }
            webRequest.Headers["Authorization"] = authorization.ToString(0, authorization.Length - 1);

            return(webRequest);
        }
示例#3
0
        private static HttpWebRequest CreateLtiOutcomesRequest(imsx_POXEnvelopeType imsxEnvelope, string url, string consumerKey, string consumerSecret)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.Method      = "POST";
            webRequest.ContentType = "application/xml";

            var parameters = new NameValueCollection();

            parameters.AddParameter(OAuthConstants.ConsumerKeyParameter, consumerKey);
            parameters.AddParameter(OAuthConstants.NonceParameter, Guid.NewGuid().ToString());
            parameters.AddParameter(OAuthConstants.SignatureMethodParameter, OAuthConstants.SignatureMethodHmacSha1);
            parameters.AddParameter(OAuthConstants.VersionParameter, OAuthConstants.Version10);

            // Calculate the timestamp
            var ts        = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var timestamp = Convert.ToInt64(ts.TotalSeconds);

            parameters.AddParameter(OAuthConstants.TimestampParameter, timestamp);

            // Calculate the body hash
            using (var ms = new MemoryStream())
                using (var sha1 = new SHA1CryptoServiceProvider())
                {
                    ImsxRequestSerializer.Serialize(ms, imsxEnvelope);
                    ms.Position = 0;
                    ms.CopyTo(webRequest.GetRequestStream());

                    var hash   = sha1.ComputeHash(ms.ToArray());
                    var hash64 = Convert.ToBase64String(hash);
                    parameters.AddParameter(OAuthConstants.BodyHashParameter, hash64);
                }

            // Calculate the signature
            var signature = OAuthUtility.GenerateSignature(webRequest.Method, webRequest.RequestUri, parameters,
                                                           consumerSecret);

            parameters.AddParameter(OAuthConstants.SignatureParameter, signature);

            // Build the Authorization header
            var authorization = new StringBuilder(OAuthConstants.AuthScheme).Append(" ");

            foreach (var key in parameters.AllKeys)
            {
                authorization.AppendFormat("{0}=\"{1}\",", key, HttpUtility.UrlEncode(parameters[key]));
            }
            webRequest.Headers["Authorization"] = authorization.ToString(0, authorization.Length - 1);

            return(webRequest);
        }
示例#4
0
        private static void SignRequest(WebRequest request, byte[] body, string consumerKey, string consumerSecret)
        {
            if (body == null)
            {
                body = new byte[0];
            }
            if (body.Length > 0 && request.ContentLength != body.Length)
            {
                throw new ArgumentException("body length does not match request.ContentLength", "body");
            }

            var parameters = new NameValueCollection();

            parameters.AddParameter(OAuthConstants.ConsumerKeyParameter, consumerKey);
            parameters.AddParameter(OAuthConstants.NonceParameter, Guid.NewGuid().ToString());
            parameters.AddParameter(OAuthConstants.SignatureMethodParameter, OAuthConstants.SignatureMethodHmacSha1);
            parameters.AddParameter(OAuthConstants.VersionParameter, OAuthConstants.Version10);

            // Calculate the timestamp
            var ts        = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var timestamp = Convert.ToInt64(ts.TotalSeconds);

            parameters.AddParameter(OAuthConstants.TimestampParameter, timestamp);

            // Calculate the body hash
            using (var sha1 = new SHA1CryptoServiceProvider())
            {
                var hash   = sha1.ComputeHash(body);
                var hash64 = Convert.ToBase64String(hash);
                parameters.AddParameter(OAuthConstants.BodyHashParameter, hash64);
            }

            // Calculate the signature
            var signature = OAuthUtility.GenerateSignature(request.Method, request.RequestUri, parameters,
                                                           consumerSecret);

            parameters.AddParameter(OAuthConstants.SignatureParameter, signature);

            // Build the Authorization header
            var authorization = new StringBuilder(OAuthConstants.AuthScheme).Append(" ");

            foreach (var key in parameters.AllKeys)
            {
                authorization.AppendFormat("{0}=\"{1}\",", key, WebUtility.UrlEncode(parameters[key]));
            }
            request.Headers["Authorization"] = authorization.ToString(0, authorization.Length - 1);
        }
示例#5
0
        /***********************************************************/
        //  This creates the OAuthHelper using the input HttpRequest.
        /***********************************************************/
        public OAuthHelper(HttpRequest request) : base(request.Form)
        {
            if (request != null)
            {
                ClientUrl = request.Host.ToString();

                /***********************************************************/
                //  This is supposed to verify that this is a valid token
                //  from canvas but it doesn't seem to work properly and
                //  it could be fixed or scrapped in the future.
                /***********************************************************/
                ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"] != null ? ConfigurationManager.AppSettings["consumerSecret"] : Guid.NewGuid().ToString();

                var requestFormDictionary = ToDictionary(request.Form);
                IsSignatureVerified = (OAuthUtility.GenerateSignature(request.Method, new Uri(request.GetEncodedUrl()), requestFormDictionary, ConsumerSecret) == this.oauth_signature);
            }
        }
示例#6
0
        private static HttpRequestMessage CreateLtiOutcomesRequest(imsx_POXEnvelopeType imsxEnvelope, string url, string consumerKey, string consumerSecret)
        {
            var webRequest = new HttpRequestMessage(HttpMethod.Post, url);

            var parameters = new NameValueCollection();
            parameters.AddParameter(OAuthConstants.ConsumerKeyParameter, consumerKey);
            parameters.AddParameter(OAuthConstants.NonceParameter, Guid.NewGuid().ToString());
            parameters.AddParameter(OAuthConstants.SignatureMethodParameter, OAuthConstants.SignatureMethodHmacSha1);
            parameters.AddParameter(OAuthConstants.VersionParameter, OAuthConstants.Version10);

            // Calculate the timestamp
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var timestamp = Convert.ToInt64(ts.TotalSeconds);
            parameters.AddParameter(OAuthConstants.TimestampParameter, timestamp);

            // Calculate the body hash
            var ms = new MemoryStream();
            using (var sha1 = PlatformSpecific.GetSha1Provider())
            {
                ImsxRequestSerializer.Serialize(ms, imsxEnvelope);
                ms.Position = 0;
                webRequest.Content = new StreamContent(ms);
                webRequest.Content.Headers.ContentType = HttpContentType.Xml;

                var hash = sha1.ComputeHash(ms.ToArray());
                var hash64 = Convert.ToBase64String(hash);
                parameters.AddParameter(OAuthConstants.BodyHashParameter, hash64);
            }

            // Calculate the signature
            var signature = OAuthUtility.GenerateSignature(webRequest.Method.Method.ToUpper(), webRequest.RequestUri, parameters,
                consumerSecret);
            parameters.AddParameter(OAuthConstants.SignatureParameter, signature);

            // Build the Authorization header
            var authorization = new StringBuilder(OAuthConstants.AuthScheme).Append(" ");
            foreach (var key in parameters.AllKeys)
            {
                authorization.AppendFormat("{0}=\"{1}\",", key, WebUtility.UrlEncode(parameters[key]));
            }
            webRequest.Headers.Add(OAuthConstants.AuthorizationHeader, authorization.ToString(0, authorization.Length - 1));

            return webRequest;
        }
        private static string GetSignature(HttpRequestMessage request, byte[] body, string nonce, string timestamp, string consumerKey, string consumerSecret)
        {
            var parameters = new NameValueCollection();

            parameters.AddParameter(OAuthConstants.ConsumerKeyParameter, consumerKey);
            parameters.AddParameter(OAuthConstants.NonceParameter, nonce);
            parameters.AddParameter(OAuthConstants.SignatureMethodParameter, OAuthConstants.SignatureMethodHmacSha1);
            parameters.AddParameter(OAuthConstants.VersionParameter, OAuthConstants.Version10);
            parameters.AddParameter(OAuthConstants.TimestampParameter, timestamp);

            // Calculate the body hash
            using (var sha1 = new SHA1CryptoServiceProvider())
            {
                var hash   = sha1.ComputeHash(body);
                var hash64 = Convert.ToBase64String(hash);
                parameters.AddParameter(OAuthConstants.BodyHashParameter, hash64);
            }

            // Calculate the signature
            var signature = OAuthUtility.GenerateSignature(request.Method.ToString(), request.RequestUri, parameters,
                                                           consumerSecret);

            return(signature);
        }
示例#8
0
 public static string GenerateOAuthSignature(this HttpRequestBase request, string consumerSecret)
 {
     return(OAuthUtility.GenerateSignature(request.HttpMethod, request.Url, request.UnvalidatedParameters(), consumerSecret));
 }