Skip to content

Commit b16ecff

Browse files
authored
Merge pull request #405 from josephdadams/fix_auth
Fix JWT auth
2 parents 59526f2 + 5370be8 commit b16ecff

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

Diff for: UI/src/app/_services/socket.service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ export class SocketService {
318318
this.socket.on('error', (message: string) => {
319319
console.error(message);
320320
if(message.includes("Access") || message.includes("JWT") || message.includes("jwt")) {
321-
alert(message);
321+
console.error("JWT requested after server reconnection. This should not happen.");
322+
window.location.reload(); //tmp fix while we figure out how to handle server reconnection
322323
}
323324
});
324325

Diff for: src/_helpers/config.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Config } from "../_models/Config";
33
import { ConfigTSLClient } from "../_models/ConfigTSLClient";
44
import fs from "fs-extra";
55
import path from "path";
6+
import { randomBytes } from "crypto";
67
import { clone } from "./clone";
78
import { uuidv4 } from "./uuid";
89
import { addUser } from "./auth";
@@ -20,7 +21,7 @@ const config_file = getConfigFilePath();
2021

2122
export const ConfigDefaults: Config = {
2223
security: {
23-
jwt_private_key: require('crypto').randomBytes(256).toString('base64'),
24+
jwt_private_key: "",
2425
},
2526
users: [],
2627
cloud_destinations: [],
@@ -39,7 +40,7 @@ export const ConfigDefaults: Config = {
3940
],
4041
externalAddress: "http://0.0.0.0:4455/#/tally",
4142
remoteErrorReporting: false,
42-
uuid: uuidv4()
43+
uuid: ""
4344
}
4445

4546
export let currentConfig: Config = clone(ConfigDefaults);
@@ -87,17 +88,17 @@ export function readConfig(): void {
8788
...clone(ConfigDefaults),
8889
...loadedConfig,
8990
};
90-
if(!loadedConfig.users || loadedConfig.users.length === 0) {
91+
if(!loadedConfig.users || typeof loadedConfig.users !== "object" || loadedConfig.users.length === 0) {
9192
logger('Migrating user configs to the new format.', 'info-quiet');
9293
currentConfig.users = [];
9394
addUser({
94-
username: currentConfig.security.username_producer || "producer",
95-
password: currentConfig.security.password_producer || "12345",
95+
username: loadedConfig.security.username_producer || "producer",
96+
password: loadedConfig.security.password_producer || "12345",
9697
roles: "producer"
9798
});
9899
addUser({
99-
username: currentConfig.security.username_settings || "admin",
100-
password: currentConfig.security.password_settings || "12345",
100+
username: loadedConfig.security.username_settings || "admin",
101+
password: loadedConfig.security.password_settings || "12345",
101102
roles: "admin"
102103
});
103104
delete currentConfig.security.username_producer;
@@ -106,12 +107,14 @@ export function readConfig(): void {
106107
delete currentConfig.security.password_settings;
107108
SaveConfig();
108109
}
109-
if(!loadedConfig.uuid) {
110+
if(!loadedConfig.uuid || typeof loadedConfig.uuid !== "string") {
110111
logger('Adding an uuid identifier to this server for using MDNS.', 'info-quiet');
112+
currentConfig.uuid = uuidv4();
111113
SaveConfig(); //uuid added if missing on config save
112114
}
113-
if(!loadedConfig.security.jwt_private_key) {
115+
if(!loadedConfig.security.jwt_private_key || typeof loadedConfig.security.jwt_private_key !== "string") {
114116
logger('Adding a private key for JWT authentication.', 'info-quiet');
117+
currentConfig.security.jwt_private_key = randomBytes(256).toString('base64');
115118
SaveConfig(); //uuid added if missing on config save
116119
}
117120
}

Diff for: src/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,9 @@ function initialSetup() {
249249

250250
socket.on('login', (username: string, password: string) => {
251251
authenticate(username, password).then((result) => {
252-
socket.emit('login_result', true); //old response, for compatibility with old UI clients
253252
socket.emit('login_response', { loginOk: true, message: "", accessToken: result.access_token });
254253
}).catch((error) => {
255-
logger(`User ${username} (ip addr ${ipAddr}) has attempted a login: wrong username or password.`);
254+
logger(`User ${username} (ip addr ${ipAddr}) has attempted a login (${error})`);
256255
//wrong credentials
257256
Promise.all([
258257
limiterConsecutiveFailsByUsernameAndIP.consume(ipAddr),
@@ -264,11 +263,9 @@ function initialSetup() {
264263
if(points < 4) {
265264
message += " Remaining attemps:"+points;
266265
}
267-
socket.emit('login_result', false); //old response, for compatibility with old UI clients
268266
socket.emit('login_response', { loginOk: false, message: message, access_token: "" });
269267
}).catch((error) => {
270268
//rate limits exceeded
271-
socket.emit('login_result', false); //old response, for compatibility with old UI clients
272269
let retrySecs = 1;
273270
try{
274271
retrySecs = Math.round(error.msBeforeNext / 1000) || 1;

0 commit comments

Comments
 (0)