aboutsummaryrefslogtreecommitdiffhomepage
path: root/cli
diff options
context:
space:
mode:
authorJohannes Zellner <johannes@nebulon.de>2015-06-27 19:59:20 +0200
committerJohannes Zellner <johannes@nebulon.de>2015-06-27 19:59:20 +0200
commita90a633f030f44bd8142e1d44a8312e952e620bb (patch)
tree9aa8c7842d4942b4d5728159a723b6dc1d16ca76 /cli
parentb72caa6940baba0eb75a7ad9618c57cc771d44cf (diff)
downloadSurfer-a90a633f030f44bd8142e1d44a8312e952e620bb.tar.gz
Surfer-a90a633f030f44bd8142e1d44a8312e952e620bb.tar.zst
Surfer-a90a633f030f44bd8142e1d44a8312e952e620bb.zip
Add auth to client
Diffstat (limited to 'cli')
-rw-r--r--cli/actions.js52
-rw-r--r--cli/config.js4
2 files changed, 47 insertions, 9 deletions
diff --git a/cli/actions.js b/cli/actions.js
index b35b8da..2bc0d7d 100644
--- a/cli/actions.js
+++ b/cli/actions.js
@@ -6,7 +6,8 @@ exports.get = get;
6exports.del = del; 6exports.del = del;
7 7
8var superagent = require('superagent'), 8var superagent = require('superagent'),
9 config = require('./config'), 9 config = require('./config.js'),
10 readlineSync = require('readline-sync'),
10 async = require('async'), 11 async = require('async'),
11 fs = require('fs'), 12 fs = require('fs'),
12 path = require('path'); 13 path = require('path');
@@ -15,12 +16,16 @@ require('colors');
15 16
16var API = '/api/files/'; 17var API = '/api/files/';
17 18
19var gQuery = {};
20
18function checkConfig() { 21function checkConfig() {
19 if (!config.server()) { 22 if (!config.server() || !config.username() || !config.password()) {
20 console.log('You have run "login" first'); 23 console.log('You have run "login" first');
21 process.exit(1); 24 process.exit(1);
22 } 25 }
23 26
27 gQuery = { username: config.username(), password: config.password() };
28
24 console.log('Using server %s', config.server().yellow); 29 console.log('Using server %s', config.server().yellow);
25} 30}
26 31
@@ -43,11 +48,40 @@ function collectFiles(filesOrFolders) {
43 return tmp; 48 return tmp;
44} 49}
45 50
51function checkResponse(error, result) {
52 if (error && error.status === 401) {
53 console.log('Login failed');
54 process.exit(1);
55 } else if (error) {
56 console.log('Error', result ? result.text : error);
57 process.exit(1);
58 }
59}
60
46function login(server) { 61function login(server) {
47 if (server[server.length-1] === '/') server = server.slice(0, -1); 62 if (server[server.length-1] === '/') server = server.slice(0, -1);
48 63
49 console.log('Using server', server); 64 console.log('Using server', server);
50 config.set('server', server); 65
66 var username = readlineSync.question('Username: ', { hideEchoBack: false });
67 var password = readlineSync.question('Password: ', { hideEchoBack: true });
68
69 superagent.get(server + API + '/').query({ username: username, password: password }).end(function (error, result) {
70 console.log(result.status);
71
72 if (result.status === 401) {
73 console.log('Login failed.');
74 process.exit(1);
75 }
76
77 config.set('server', server);
78 config.set('username', username);
79
80 // TODO this is clearly bad and needs fixing
81 config.set('password', password);
82
83 gQuery = { username: username, password: password };
84 });
51} 85}
52 86
53function put(filePath, otherFilePaths, options) { 87function put(filePath, otherFilePaths, options) {
@@ -60,7 +94,7 @@ function put(filePath, otherFilePaths, options) {
60 94
61 console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan); 95 console.log('Uploading file %s -> %s', relativeFilePath.cyan, ((options.destination ? options.destination : '') + '/' + relativeFilePath).cyan);
62 96
63 superagent.put(config.server() + API + relativeFilePath).attach('file', file).end(callback); 97 superagent.put(config.server() + API + relativeFilePath).query(gQuery).attach('file', file).end(callback);
64 }, function (error) { 98 }, function (error) {
65 if (error) { 99 if (error) {
66 console.log('Failed to put file.', error); 100 console.log('Failed to put file.', error);
@@ -74,8 +108,9 @@ function put(filePath, otherFilePaths, options) {
74function get(filePath) { 108function get(filePath) {
75 checkConfig(); 109 checkConfig();
76 110
77 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); 111 superagent.get(config.server() + API + filePath).query(gQuery).end(function (error, result) {
78 superagent.get(config.server() + API + relativeFilePath).end(function (error, result) { 112 if (error && error.status === 401) return console.log('Login failed');
113 if (error && error.status === 404) return console.log('No such file or directory');
79 if (error) return console.log('Failed', result ? result.body : error); 114 if (error) return console.log('Failed', result ? result.body : error);
80 115
81 if (result.body && result.body.entries) { 116 if (result.body && result.body.entries) {
@@ -93,8 +128,9 @@ function del(filePath) {
93 checkConfig(); 128 checkConfig();
94 129
95 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1); 130 var relativeFilePath = path.resolve(filePath).slice(process.cwd().length + 1);
96 superagent.del(config.server() + API + relativeFilePath).end(function (error, result) { 131 superagent.del(config.server() + API + relativeFilePath).query(gQuery).end(function (error, result) {
97 if (error.status === 404) return console.log('No such file or directory'); 132 if (error && error.status === 401) return console.log('Login failed');
133 if (error && error.status === 404) return console.log('No such file or directory');
98 if (error) return console.log('Failed', result ? result.body : error); 134 if (error) return console.log('Failed', result ? result.body : error);
99 console.log('Success', result.body); 135 console.log('Success', result.body);
100 }); 136 });
diff --git a/cli/config.js b/cli/config.js
index a3708b8..68eae5f 100644
--- a/cli/config.js
+++ b/cli/config.js
@@ -15,7 +15,9 @@ exports = module.exports = {
15 has: has, 15 has: has,
16 16
17 // convenience 17 // convenience
18 server: function () { return get('server'); } 18 server: function () { return get('server'); },
19 username: function () { return get('username'); },
20 password: function () { return get('password'); }
19}; 21};
20 22
21var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; 23var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;