Skip to content

Commit 60b18f6

Browse files
committed
Add basic authentication.
1 parent 51841a8 commit 60b18f6

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

server/configuration.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import {isNullOrUndefined} from "util";
22

33
const configurations = {
4-
port: 6101,
5-
internalApiBase: "/api/v1/internal/",
6-
graphQLHostname: "pipeline-api",
7-
graphQLPort: 6001,
8-
graphQlEndpoint: "/graphql",
9-
thumbsHostname: "pipeline-api",
10-
thumbsPort: 6001,
11-
thumbsPath: "/thumbnail",
12-
buildVersion: 5,
13-
isActivePipeline: true
4+
port: 6101,
5+
internalApiBase: "/api/v1/internal/",
6+
graphQLHostname: "pipeline-api",
7+
graphQLPort: 6001,
8+
graphQlEndpoint: "/graphql",
9+
thumbsHostname: "pipeline-api",
10+
thumbsPort: 6001,
11+
thumbsPath: "/thumbnail",
12+
authRequired: true,
13+
authUser: "mouselight",
14+
authPassword: "auth_secret", // always override this, but in the event env is not set, don't leave completely open.
15+
buildVersion: 5,
16+
isActivePipeline: true
1417
};
1518

1619
function loadServerOptions() {
@@ -22,6 +25,9 @@ function loadServerOptions() {
2225
options.thumbsHostname = process.env.PIPELINE_THUMBS_HOST || process.env.PIPELINE_API_HOST || options.thumbsHostname;
2326
options.thumbsPort = parseInt(process.env.PIPELINE_THUMBS_PORT) || parseInt(process.env.PIPELINE_API_PORT) || options.thumbsPort;
2427
options.thumbsPath = process.env.PIPELINE_THUMBS_PATH || options.thumbsPath;
28+
options.authRequired = process.env.PIPELINE_AUTH_REQUIRED !== "false";
29+
options.authUser = process.env.PIPELINE_AUTH_USER || options.authUser;
30+
options.authPassword = process.env.PIPELINE_AUTH_PASS || options.authPassword;
2531

2632
if (!isNullOrUndefined(process.env.PIPELINE_IS_ACTIVE) && process.env.PIPELINE_IS_ACTIVE.length > 0) {
2733
options.isActivePipeline = parseInt(process.env.PIPELINE_IS_ACTIVE) > 0;

server/pipelineClientServer.ts

+34-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as os from "os";
33
const express = require("express");
44
import * as http from "http";
55
import * as proxy from "express-http-proxy";
6-
6+
const passport = require("passport");
7+
const DigestStrategy = require("passport-http").DigestStrategy;
78

89
const debug = require("debug")("pipeline-api:server");
910

@@ -27,6 +28,28 @@ const hostname = process.env.NODE_ENV == "production" ? os.hostname() : "localho
2728

2829
let socketIoPortOffset: number = 0;
2930

31+
passport.use(new DigestStrategy({qop: 'auth'},
32+
function (username: any, done: any) {
33+
if (username === Configuration.authUser) {
34+
return done(null, {id: 1, name: username}, Configuration.authPassword);
35+
} else {
36+
return done("Invalid user", null);
37+
}
38+
},
39+
function (params: any, done: any) {
40+
// validate nonce as necessary
41+
done(null, true)
42+
}
43+
));
44+
45+
passport.serializeUser(function (user: any, done: any) {
46+
done(null, user.id);
47+
});
48+
49+
passport.deserializeUser(function (id: any, done: any) {
50+
done(null, {id: 1, name: Configuration.authUser});
51+
});
52+
3053
startExpressServer();
3154

3255
function startExpressServer() {
@@ -48,14 +71,20 @@ function startExpressServer() {
4871

4972
app = express();
5073

74+
if (Configuration.authRequired) {
75+
app.use(passport.initialize());
76+
77+
app.get("/", passport.authenticate('digest', {session: false}), (request: any, response: any, next: any) => {
78+
next();
79+
});
80+
}
81+
5182
app.use(express.static(rootPath));
5283

5384
app.post("/graphql", proxy(apiUri + "/graphql"));
5485

5586
app.use("/thumbnail", proxy(apiUri + "/thumbnail"));
5687

57-
// app.use("/thumbnailData", proxy(apiUri + "/thumbnailData"));
58-
5988
app.use(`${Configuration.internalApiBase}serverConfiguration`, serverConfiguration);
6089

6190
app.use("/", (req, res) => {
@@ -126,12 +155,9 @@ function devServer() {
126155
},
127156
"/thumbnail": {
128157
target: apiUri
129-
},
130-
// "/thumbnailData": {
131-
// target: apiUri
132-
// }
158+
}
133159
},
134-
contentBase: path.resolve(path.join(__dirname, "..", "public")),
160+
contentBase: path.resolve(path.join(__dirname, "..")),
135161
disableHostCheck: true,
136162
publicPath: webpackConfig.output.publicPath,
137163
// hot: true,

webpack.dev.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22

3-
const dist = path.join(__dirname, "public");
3+
const dist = path.join(__dirname);
44

55
module.exports = {
66
entry: [

0 commit comments

Comments
 (0)