aboutsummaryrefslogtreecommitdiffhomepage
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
parentb72caa6940baba0eb75a7ad9618c57cc771d44cf (diff)
downloadSurfer-a90a633f030f44bd8142e1d44a8312e952e620bb.tar.gz
Surfer-a90a633f030f44bd8142e1d44a8312e952e620bb.tar.zst
Surfer-a90a633f030f44bd8142e1d44a8312e952e620bb.zip
Add auth to client
-rw-r--r--cli/actions.js52
-rw-r--r--cli/config.js4
-rw-r--r--src/auth.js24
3 files changed, 64 insertions, 16 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;
diff --git a/src/auth.js b/src/auth.js
index 3d2acce..83d0426 100644
--- a/src/auth.js
+++ b/src/auth.js
@@ -3,20 +3,30 @@
3var passport = require('passport'), 3var passport = require('passport'),
4 LdapStrategy = require('passport-ldapjs').Strategy; 4 LdapStrategy = require('passport-ldapjs').Strategy;
5 5
6passport.serializeUser(function (user, done) {
7 console.log('serializeUser', user);
8 done(null, user.id);
9});
10
11passport.deserializeUser(function (id, done) {
12 console.log('deserializeUser', id);
13 done(null, { id: id });
14});
15
6var LDAP_URL = process.env.LDAP_URL; 16var LDAP_URL = process.env.LDAP_URL;
7var LDAP_USERS_BASE_DN = process.env.LDAP_USERS_BASE_DN; 17var LDAP_USERS_BASE_DN = process.env.LDAP_USERS_BASE_DN;
8 18
9if (LDAP_URL && LDAP_USERS_BASE_DN) { 19if (LDAP_URL && LDAP_USERS_BASE_DN) {
10 console.log('Enable ldap auth'); 20 console.log('Enable ldap auth');
11 21
12 exports.ldap = passport.authenticate('ldap', { 22 exports.ldap = passport.authenticate('ldap');
13 successReturnToOrRedirect: '/',
14 failureRedirect: '/login',
15 failureFlash: true
16 });
17} else { 23} else {
18 exports.ldap = function (req, res, next) { 24 exports.ldap = function (req, res, next) {
19 console.log('ldap auth disabled'); 25 console.log('Disable ldap auth, use developer credentials!');
26
27 if (req.query.username !== 'username') return res.send(401);
28 if (req.query.password !== 'password') return res.send(401);
29
20 next(); 30 next();
21 }; 31 };
22} 32}
@@ -31,7 +41,7 @@ var opts = {
31 attributes: ['displayname', 'username', 'mail', 'uid'], 41 attributes: ['displayname', 'username', 'mail', 'uid'],
32 scope: 'sub' 42 scope: 'sub'
33 }, 43 },
34 uidTag: 'uid', 44 uidTag: 'cn',
35 usernameField: 'username', 45 usernameField: 'username',
36 passwordField: 'password', 46 passwordField: 'password',
37}; 47};