From eed5063b2d2e2d9bf87a37368640194717ef11b1 Mon Sep 17 00:00:00 2001 From: nyaundi brian Date: Tue, 12 Nov 2019 11:04:40 +0300 Subject: [PATCH] implement DELETE, fix GET --- lib/index.js | 72 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/lib/index.js b/lib/index.js index 04e2c73..8b1b8ce 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,7 +9,7 @@ var defaults = { awsSecretAccessKey: 'Tm3dNht5QMKxu7EU44ZLflDeE7sFbqQy3Q+XE+MY', awsRegion: 'us-east-1', awsBucket: 'hy8', - awsHost:'s3.switch.lxc' + awsHost: 's3.switch.lxc' }; module.exports.create = function(config) { @@ -32,14 +32,14 @@ module.exports.create = function(config) { }, set: function(data) { - // console.log('Add Key Auth URL', data); + console.log('Add Key Auth URL'); var ch = data.challenge; var signed = aws4.sign({ host: awsHost, service: 's3', region: awsRegion, - path: '/' + awsBucket + '/' + ch.identifier.value +'/'+ ch.token, + path: '/' + awsBucket + '/' + ch.identifier.value + '/' + ch.token, headers: { 'Content-Type': 'text/plain;charset=UTF-8' @@ -54,7 +54,7 @@ module.exports.create = function(config) { return request({ // debug: true, method: 'PUT', - url: 'http://' +signed.host+ signed.path, + url: 'http://' + signed.host + signed.path, headers: { 'Content-Type': 'text/plain;charset=UTF-8' }, body: ch.keyAuthorization @@ -62,7 +62,7 @@ module.exports.create = function(config) { // console.log(resp.statusCode); if (200 !== resp.statusCode) { console.error(resp.statusCode); - console.error(resp.body); + // console.error(resp.body); throw new Error('Could not PUT.'); } return true; @@ -73,7 +73,7 @@ module.exports.create = function(config) { }); }, get: function(data) { - // console.log('List Key Auth URL', data); + console.log('List Key Auth URL'); var ch = data.challenge; @@ -81,7 +81,7 @@ module.exports.create = function(config) { host: awsHost, service: 's3', region: awsRegion, - path: '/' + awsBucket + '/' + ch.identifier.value +'/'+ ch.token, + path: '/' + awsBucket + '/' + ch.identifier.value + '/' + ch.token, headers: { 'Content-Type': 'text/plain;charset=UTF-8' }, @@ -94,33 +94,61 @@ module.exports.create = function(config) { return request({ // debug: true, method: 'GET', - url: 'http://' +signed.host+ signed.path, + url: 'http://' + signed.host + signed.path, headers: { 'Content-Type': 'text/plain;charset=UTF-8' } + }).then(function(resp) { + if (200 === resp.statusCode) { + return { + keyAuthorization: resp.body + }; + }else if (404 === resp.statusCode){ + return null; + } + // wrong sign returns 403 + console.error(resp.statusCode); + // console.error(resp.body); + throw new Error('Could not GET'); + }) + .catch(function(err) { + throw err; + // return null; + }); + + }, + + remove: function(data) { + console.log('Remove Key Auth URL'); + var ch = data.challenge; + + var signed = aws4.sign({ + host: awsHost, + service: 's3', + region: awsRegion, + path: '/' + awsBucket + '/' + ch.identifier.value + '/' + ch.token, + method: 'DELETE', + signQuery: true + }, + AWSCredentials + ); + console.log(signed); + return request({ + // debug: true, + method: 'DELETE', + url: 'http://' + signed.host + signed.path }).then(function(resp) { // console.log(resp.statusCode); - if (200 !== resp.statusCode) { + if (204 !== resp.statusCode) { console.error(resp.statusCode); console.error(resp.body); - throw new Error('Could not GET'); + throw new Error('Could not DELETE.'); } - - return { - keyAuthorization: resp.body - }; - + return true; }) .catch(function(err) { throw err; // return null; }); - }, - - remove: function(data) { - // console.log('Remove Key Auth URL', data); - var ch = data.challenge; - - } }; };