| 1835 | return con.awsSignatureVersion === '4' ? AwsSignatureV4 : AwsSignatureV2; |
| 1836 | } |
| 1837 | function authorizationMethod(awsRequest) { |
| 1838 | var fileUpload = awsRequest.fileUpload, |
| 1839 | con = fileUpload.con, |
| 1840 | request = awsRequest.request; |
| 1841 | |
| 1842 | function AuthorizationMethod() { |
| 1843 | this.request = request; |
| 1844 | } |
| 1845 | AuthorizationMethod.prototype = Object.create(AuthorizationMethod.prototype); |
| 1846 | AuthorizationMethod.prototype.request = {}; |
| 1847 | AuthorizationMethod.makeSignParamsObject = function (params) { |
| 1848 | var out = {}; |
| 1849 | for (var param in params) { |
| 1850 | if (!params.hasOwnProperty(param)) { continue; } |
| 1851 | if (typeof params[param] === 'function') { |
| 1852 | out[param] = params[param](); |
| 1853 | } else { |
| 1854 | out[param] = params[param]; |
| 1855 | } |
| 1856 | } |
| 1857 | return out; |
| 1858 | }; |
| 1859 | AuthorizationMethod.prototype.authorize = function () { |
| 1860 | return new Promise(function (resolve, reject) { |
| 1861 | var xhr = new XMLHttpRequest(); |
| 1862 | awsRequest.currentXhr = xhr; |
| 1863 | |
| 1864 | var stringToSign = awsRequest.stringToSign(), |
| 1865 | url = [con.signerUrl, '?to_sign=', stringToSign, '&datetime=', request.dateString]; |
| 1866 | if (con.sendCanonicalRequestToSignerUrl) { |
| 1867 | url.push('&canonical_request='); |
| 1868 | url.push(encodeURIComponent(awsRequest.canonicalRequest())); |
| 1869 | } |
| 1870 | url = url.join(""); |
| 1871 | |
| 1872 | var signParams = AuthorizationMethod.makeSignParamsObject(fileUpload.signParams); |
| 1873 | for (var param in signParams) { |
| 1874 | if (!signParams.hasOwnProperty(param)) { continue; } |
| 1875 | url += ('&' + encodeURIComponent(param) + '=' + encodeURIComponent(signParams[param])); |
| 1876 | } |
| 1877 | |
| 1878 | if (con.xhrWithCredentials) { |
| 1879 | xhr.withCredentials = true; |
| 1880 | } |
| 1881 | |
| 1882 | xhr.onreadystatechange = function () { |
| 1883 | if (xhr.readyState === 4) { |
| 1884 | if (xhr.status === 200) { |
| 1885 | awsRequest.signResponse(xhr.response, stringToSign, request.dateString) |
| 1886 | .then(resolve); |
| 1887 | } else { |
| 1888 | if ([401, 403].indexOf(xhr.status) > -1) { |
| 1889 | var reason = "status:" + xhr.status; |
| 1890 | fileUpload.deferredCompletion.reject('Permission denied ' + reason); |
| 1891 | return reject(reason); |
| 1892 | } |
| 1893 | reject("Signature fetch returned status: " + xhr.status); |
| 1894 | } |