update remote access example
This commit is contained in:
parent
ab21671481
commit
d2aa9394aa
|
@ -1,29 +1,49 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var app = express();
|
|
||||||
var basicAuth = require('express-basic-auth');
|
var basicAuth = require('express-basic-auth');
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
var secret = crypto.randomBytes(16).toString('hex');
|
|
||||||
var serveIndex = require('serve-index');
|
var serveIndex = require('serve-index');
|
||||||
|
|
||||||
var rootIndex = serveIndex('/', { hidden: true, icons: true, view: 'details' });
|
var rootIndex = serveIndex('/', { hidden: true, icons: true, view: 'details' });
|
||||||
var rootFs = express.static('/', { dotfiles: 'allow', redirect: true, index: false });
|
var rootFs = express.static('/', { dotfiles: 'allow', redirect: true, index: false });
|
||||||
|
|
||||||
var userIndex = serveIndex(require('os').homedir(), { hidden: true, icons: true, view: 'details' });
|
var userIndex = serveIndex(require('os').homedir(), { hidden: true, icons: true, view: 'details' });
|
||||||
var userFs = express.static(require('os').homedir(), { dotfiles: 'allow', redirect: true, index: false });
|
var userFs = express.static(require('os').homedir(), { dotfiles: 'allow', redirect: true, index: false });
|
||||||
|
|
||||||
|
var app = express();
|
||||||
|
var realm = 'Login Required';
|
||||||
|
var secret = crypto.randomBytes(16).toString('hex');
|
||||||
|
|
||||||
var myAuth = basicAuth({
|
var myAuth = basicAuth({
|
||||||
users: { 'root': secret, 'user': secret }
|
users: { 'root': secret, 'user': secret }
|
||||||
|
, challenge: true
|
||||||
|
, realm: realm
|
||||||
|
, unauthorizedResponse: function (/*req*/) {
|
||||||
|
return 'Unauthorized <a href="/">Home</a>';
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/', function (req, res) {
|
app.get('/', function (req, res) {
|
||||||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||||
res.end('<form action=\"/browse\" method=\"get\"><button type=\"submit\">Login to view Files</button></form>');
|
res.end(
|
||||||
|
'<a href="/browse/">View Files</a>'
|
||||||
|
+ ' | '
|
||||||
|
+ '<a href="/logout/">Logout</a>'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
app.use('/logout', function (req, res) {
|
||||||
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||||
|
res.setHeader('WWW-Authenticate', 'Basic realm="' + realm + '"');
|
||||||
|
res.status(401);
|
||||||
|
res.statusCode = 401;
|
||||||
|
//res.setHeader('Location', '/');
|
||||||
|
res.end('Logged out | <a href="/">Home</a>');
|
||||||
});
|
});
|
||||||
app.use('/browse', function (req, res, next) {
|
app.use('/browse', function (req, res, next) {
|
||||||
console.log('trying to auth browse');
|
|
||||||
myAuth(req, res, next);
|
myAuth(req, res, next);
|
||||||
});
|
});
|
||||||
app.use('/browse', function (req, res, next) {
|
app.use('/browse', function (req, res, next) {
|
||||||
console.log('trying to get browse', req.url);
|
|
||||||
if ('root' === req.auth.user) { rootFs(req, res, function () { rootIndex(req, res, next); }); return; }
|
if ('root' === req.auth.user) { rootFs(req, res, function () { rootIndex(req, res, next); }); return; }
|
||||||
if ('user' === req.auth.user) { userFs(req, res, function () { userIndex(req, res, next); }); return; }
|
if ('user' === req.auth.user) { userFs(req, res, function () { userIndex(req, res, next); }); return; }
|
||||||
res.end('Sad Panda');
|
res.end('Sad Panda');
|
||||||
|
|
|
@ -16,7 +16,9 @@
|
||||||
"redirect-https": "^1.1.5"
|
"redirect-https": "^1.1.5"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"express": "^4.16.3"
|
"express": "^4.16.3",
|
||||||
|
"express-basic-auth": "^1.1.5",
|
||||||
|
"serve-index": "^1.9.1"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "node examples/serve.js"
|
"test": "node examples/serve.js"
|
||||||
|
|
Loading…
Reference in New Issue