implement DELETE, fix GET

This commit is contained in:
nyaundi brian 2019-11-12 11:04:40 +03:00
parent 7d97e8d4bb
commit eed5063b2d
1 changed files with 50 additions and 22 deletions

View File

@ -9,7 +9,7 @@ var defaults = {
awsSecretAccessKey: 'Tm3dNht5QMKxu7EU44ZLflDeE7sFbqQy3Q+XE+MY', awsSecretAccessKey: 'Tm3dNht5QMKxu7EU44ZLflDeE7sFbqQy3Q+XE+MY',
awsRegion: 'us-east-1', awsRegion: 'us-east-1',
awsBucket: 'hy8', awsBucket: 'hy8',
awsHost:'s3.switch.lxc' awsHost: 's3.switch.lxc'
}; };
module.exports.create = function(config) { module.exports.create = function(config) {
@ -32,14 +32,14 @@ module.exports.create = function(config) {
}, },
set: function(data) { set: function(data) {
// console.log('Add Key Auth URL', data); console.log('Add Key Auth URL');
var ch = data.challenge; var ch = data.challenge;
var signed = aws4.sign({ var signed = aws4.sign({
host: awsHost, host: awsHost,
service: 's3', service: 's3',
region: awsRegion, region: awsRegion,
path: '/' + awsBucket + '/' + ch.identifier.value +'/'+ ch.token, path: '/' + awsBucket + '/' + ch.identifier.value + '/' + ch.token,
headers: { headers: {
'Content-Type': 'text/plain;charset=UTF-8' 'Content-Type': 'text/plain;charset=UTF-8'
@ -54,7 +54,7 @@ module.exports.create = function(config) {
return request({ return request({
// debug: true, // debug: true,
method: 'PUT', method: 'PUT',
url: 'http://' +signed.host+ signed.path, url: 'http://' + signed.host + signed.path,
headers: { 'Content-Type': 'text/plain;charset=UTF-8' }, headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
body: ch.keyAuthorization body: ch.keyAuthorization
@ -62,7 +62,7 @@ module.exports.create = function(config) {
// console.log(resp.statusCode); // console.log(resp.statusCode);
if (200 !== resp.statusCode) { if (200 !== resp.statusCode) {
console.error(resp.statusCode); console.error(resp.statusCode);
console.error(resp.body); // console.error(resp.body);
throw new Error('Could not PUT.'); throw new Error('Could not PUT.');
} }
return true; return true;
@ -73,7 +73,7 @@ module.exports.create = function(config) {
}); });
}, },
get: function(data) { get: function(data) {
// console.log('List Key Auth URL', data); console.log('List Key Auth URL');
var ch = data.challenge; var ch = data.challenge;
@ -81,7 +81,7 @@ module.exports.create = function(config) {
host: awsHost, host: awsHost,
service: 's3', service: 's3',
region: awsRegion, region: awsRegion,
path: '/' + awsBucket + '/' + ch.identifier.value +'/'+ ch.token, path: '/' + awsBucket + '/' + ch.identifier.value + '/' + ch.token,
headers: { headers: {
'Content-Type': 'text/plain;charset=UTF-8' 'Content-Type': 'text/plain;charset=UTF-8'
}, },
@ -94,33 +94,61 @@ module.exports.create = function(config) {
return request({ return request({
// debug: true, // debug: true,
method: 'GET', method: 'GET',
url: 'http://' +signed.host+ signed.path, url: 'http://' + signed.host + signed.path,
headers: { 'Content-Type': 'text/plain;charset=UTF-8' } 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) { }).then(function(resp) {
// console.log(resp.statusCode); // console.log(resp.statusCode);
if (200 !== resp.statusCode) { if (204 !== resp.statusCode) {
console.error(resp.statusCode); console.error(resp.statusCode);
console.error(resp.body); console.error(resp.body);
throw new Error('Could not GET'); throw new Error('Could not DELETE.');
} }
return true;
return {
keyAuthorization: resp.body
};
}) })
.catch(function(err) { .catch(function(err) {
throw err; throw err;
// return null; // return null;
}); });
},
remove: function(data) {
// console.log('Remove Key Auth URL', data);
var ch = data.challenge;
} }
}; };
}; };