Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Commit dde765c

Browse files
committed
Looks like the conversion is complete?
1 parent 7993e40 commit dde765c

14 files changed

+67
-52
lines changed

app/config.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import log from "./logger";
1+
import { log } from "./logger";
22

33
// TODO: Clean up ENV management into something less crappy.
44
// or use a 3rd party node ENV manager module.
5-
let DEFAULT_URL = "http://localhost:3000";
5+
export let DEFAULT_URL = "http://localhost:3000";
6+
export let webAppUrl;
7+
export let httpPort = 3002;
8+
export let mqttPort = 1883;
69

7-
let webAppUrl;
810
if (process.env.WEB_API_URL) {
911
webAppUrl = process.env.WEB_API_URL;
1012
} else {
1113
webAppUrl = DEFAULT_URL;
1214
log("You did not set WEB_API_URL. Defaulting to " + DEFAULT_URL);
1315
}
1416

15-
module.exports = {
16-
webAppUrl: webAppUrl,
17-
httpPort: 3002,
18-
mqttPort: 1883
19-
}

app/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Server } from "mosca";
2-
import { conf } from "./config";
2+
import * as conf from "./config";
33
import { onReady } from "./on_ready";
44
import { maybeEnableSSL } from "./security/maybe_enable_ssl";
55
import { authorizePublish } from "./security/authorize_publish";

app/logger.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (args: any) {
1+
export function log(args: any) {
22
if (!process.env.DISABLE_LOGS) {
33
console.log.apply(this, arguments);
44
}

app/mosca.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare module "mosca" {
2+
export class Server extends EventEmitter {
3+
constructor(opts: any);
4+
public toString: () => string;
5+
public subscribe: (topic, callback, done) => any;
6+
public publish: (packet, client, callback) => any;
7+
public authenticate: (client, username, password, callback) => any;
8+
public on: any;
9+
public authorizePublish: any;
10+
public authorizeSubscribe: any;
11+
}
12+
}

app/on_ready.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { authorizePublish } from "./security/authorize_publish";
33
import { authorizeSubscribe } from "./security/authorize_subscribe";
44
import { log } from "./logger";
55

6-
module.exports = (server) => () => {
6+
export let onReady = (server) => () => {
77
log("Server online");
88
startLogging(server);
99
startSecurity(server);
@@ -24,8 +24,8 @@ function startLogging(server) {
2424
"subscribed",
2525
"unsubscribed",
2626
"error"
27-
].forEach(function (event) {
28-
server.on(event, function () {
27+
].forEach(function(event) {
28+
server.on(event, function() {
2929
log("" + event + " event.");
3030
});
3131
});

app/security/authenticate.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import { fetchToken } from "./fetch_token";
33
import { verifyToken } from "./verify_token";
44
import { log } from "../logger";
5+
import Axios, { Promise as AxiosPromise } from "axios";
56

6-
function determineAuthStrategy(username, password) {
7+
type tokenHandler = (u: string, pass: string) => AxiosPromise<any>;
8+
function determineAuthStrategy(username: string, password: string): tokenHandler {
79
// Really long password? Probably a JWT.
810
if (password.length > 100) {
911
return verifyToken;
@@ -12,7 +14,7 @@ function determineAuthStrategy(username, password) {
1214
}
1315
};
1416

15-
export default function (client, username, password, callback) {
17+
export function authenticate(client, username: string, password: string, callback) {
1618
password = (password || "").toString();
1719
username = username || "";
1820
let auth = determineAuthStrategy(username, password);

app/security/authorize_publish.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { canUseTopic } from "./can_use_topic";
22

3-
export default function authorizePublish(client, topic, payload, callback) {
3+
export function authorizePublish(client, topic, payload, callback) {
44
callback(null, canUseTopic(client, topic));
55
}

app/security/authorize_subscribe.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { canUseTopic } from "./can_use_topic";
22

3-
export default function authorizeSubscribe(client, topic, callback) {
4-
callback(null, canUseTopic(client, topic));
3+
export function authorizeSubscribe(client, topic, callback) {
4+
callback(null, canUseTopic(client, topic));
55
}

app/security/can_use_topic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { log } from "../logger";
33
// If a user has a bot of id XYZ, then they may access any topic
44
// following pattern bot/XYZ/#
55

6-
export default function (client, topic) {
6+
export function canUseTopic(client, topic) {
77
let hasBot = topic && client && client.permissions && client.permissions.bot;
88
if (!hasBot) {
99
log("Tried to access topic " + (topic || "???") + " but no bot/topic provided.");

app/security/fetch_token.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import { post } from "axios";
1+
import Axios from "axios";
22
import { webAppUrl } from "../config";
33
import { log } from "../logger";
4+
import * as config from "../config";
45

56
const TOKEN_URL = "" + config.webAppUrl + '/api/tokens';
67

7-
export default function (password, username) {
8-
let params = {
9-
user: {
10-
email: username,
11-
password: password
12-
}
13-
};
14-
let email = params.user.email;
15-
let e = new Error(
16-
"Login credentials are too short. Use a JSON web token as password");
17-
return Promise.reject(e);
8+
export function fetchToken(password, username) {
9+
let params = {
10+
user: {
11+
email: username,
12+
password: password
13+
}
14+
};
15+
let email = params.user.email;
16+
let e = new Error(
17+
"Login credentials are too short. Use a JSON web token as password");
18+
return Promise.reject(e);
1819
}

app/security/maybe_enable_ssl.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let SSL_CERT = SSL_DIR + "cert.pem";
88
let SSL_KEY = SSL_DIR + "privkey.pem";
99
import { log } from "../logger";
1010

11-
export default function maybeEnableSSL(config) {
11+
export function maybeEnableSSL(config) {
1212
if (SSL) {
1313
config.secure = config.secure || {};
1414
config.secure.port = SSL_MQTT_PORT;

app/security/verify_token.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import { webAppUrl } from "../config";
2-
import { get } from "axios";
2+
import Axios from "axios";
33
import * as jwt from "jsonwebtoken";
44
import { log } from "../logger";
55

66
let url = webAppUrl + "/api/public_key";
77

88
function keyOk(resp) {
9-
log("Downloaded certificate from " + url);
10-
return new Buffer(resp.data, 'utf8');
9+
log("Downloaded certificate from " + url);
10+
return new Buffer(resp.data, 'utf8');
1111
}
1212

1313
function no(error) {
14-
log("Unable to download certificate from " + url);
15-
log("Is the FarmBot API running?");
16-
process.exit();
14+
log("Unable to download certificate from " + url);
15+
log("Is the FarmBot API running?");
16+
process.exit();
1717
}
1818

19-
let getCertificate = get(url).then(keyOk, no);
19+
let getCertificate = Axios.get(url).then(keyOk, no);
2020

21-
export default function verifyToken(token) {
22-
function no(error) {
23-
log("Unable to verify token " + url);
24-
}
21+
export function verifyToken(token) {
22+
function no(error) {
23+
log("Unable to verify token " + url);
24+
}
2525

26-
function ok(cert) {
27-
log("Did fetch certifiacte. Will verify token with certificate.");
28-
return jwt.verify(token, cert, { algorithms: ['RS256'] });
29-
}
30-
log("Will fetch certificate...")
31-
return getCertificate.then(ok, no)
26+
function ok(cert) {
27+
log("Did fetch certifiacte. Will verify token with certificate.");
28+
return jwt.verify(token, cert, { algorithms: ['RS256'] });
29+
}
30+
log("Will fetch certificate...")
31+
return getCertificate.then(ok, no)
3232
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
"@types/axios": "^0.9.34",
2424
"@types/jsonwebtoken": "^7.1.33",
2525
"@types/mqtt": "0.0.32",
26+
"@types/node": "0.0.2",
2627
"axios": "^0.15.2",
2728
"jsonwebtoken": "^7.1.9",
2829
"mosca": "^2.2.0",
2930
"mqtt": "^1.7.4"
3031
},
3132
"devDependencies": {
32-
"@types/node": "0.0.2",
3333
"istanbul": "^0.4.4",
3434
"jasmine": "^2.4.1",
3535
"nodemon": "^1.11.0",
3636
"ts-node": "^1.7.0",
3737
"typescript": "^2.0.10"
3838
}
39-
}
39+
}

tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"module": "commonjs",
88
"target": "es5",
99
"noImplicitAny": false,
10-
"sourceMap": false
10+
"sourceMap": false,
11+
"types": [
12+
"node"
13+
]
1114
},
1215
"exclude": [
1316
"node_modules"

0 commit comments

Comments
 (0)