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

Commit 0c4e176

Browse files
committed
More touchups
1 parent d270cb8 commit 0c4e176

13 files changed

+56
-42
lines changed

__tests__/security/authenticate.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import "jest";
2-
import { authenticate as auth } from "../../app/security/authenticate";
2+
import { authenticate as auth } from "../../app/authentication/authenticate";
33
import axios from "axios";
44
var EMAIL = "[email protected]";
55
var PASSWORD = "password123";
66
var validJWT;
77

8-
describe("authentication", function() {
9-
beforeAll(function(done) {
8+
describe("authentication", function () {
9+
beforeAll(function (done) {
1010
axios
1111
.post("http://localhost:3000/api/tokens", {
1212
user: {
1313
email: EMAIL,
1414
password: PASSWORD
1515
}
1616
})
17-
.then(function(resp) {
17+
.then(function (resp) {
1818
console.log("=========================");
1919
validJWT = resp.data.token.encoded;
2020
done();
2121
})
22-
.catch(function() {
22+
.catch(function () {
2323
console.log("STAND UP AN API SERVER LOCALLY!");
2424
})
2525
})
2626

27-
xit("logs in and attaches JSON web token to user", function(done) {
27+
xit("logs in and attaches JSON web token to user", function (done) {
2828
var finished = false;
2929
var client: any = {};
30-
var callback = function(_, isAuthorized) {
30+
var callback = function (_, isAuthorized) {
3131
expect(isAuthorized).toBeTruthy();
3232
expect(client.permissions).toBeDefined();
3333
expect(client.permissions.sub).toBe(EMAIL);
@@ -36,10 +36,10 @@ describe("authentication", function() {
3636
auth(client, EMAIL, PASSWORD, callback);
3737
})
3838

39-
it("logs in with a JWT as a password", function(done) {
39+
it("logs in with a JWT as a password", function (done) {
4040
var finished = false;
4141
var client: any = {};
42-
var callback = function(_, isAuthorized) {
42+
var callback = function (_, isAuthorized) {
4343
expect(isAuthorized).toBeTruthy();
4444
expect(client.permissions).toBeDefined();
4545
expect(client.permissions.sub).toBe('[email protected]');

__tests__/security/authorize_publish.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as all from "../../app/security/authorize_publish";
1+
import * as all from "../../app/authorization/authorize_publish";
22

33
describe("foo", () => {
44
it("bars", () => {
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as all from "../../app/security/authorize_subscribe";
1+
import * as all from "../../app/authorization/authorize_subscribe";
22

33
describe("foo", () => {
44
it("bars", () => {
55
expect(true).toBe((true));
66
});
7-
});
7+
});

__tests__/security/can_use_topic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as all from "../../app/security/can_use_topic";
1+
import * as all from "../../app/authorization/can_use_topic";
22

33
describe("foo", () => {
44
it("bars", () => {

app/authentication/authenticate.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// test@test.com password123
2+
import { verifyToken } from "../security/verify_token";
3+
import { log } from "../logger";
4+
import { authenticateOk } from "./authenticate_ok";
5+
import { authenticateNo } from "./authenticate_no";
6+
7+
/** Determine if user is authorized to use the server. */
8+
export function authenticate(client, username: string, password: string, callback) {
9+
log("AUTH START");
10+
let ok = authenticateOk(client, callback, username);
11+
let no = authenticateNo(client, callback, username);
12+
verifyToken(password.toString()).then(ok, no);
13+
}

app/authentication/authenticate_no.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { log } from "../logger";
2+
/** Creates a function that is triggered when a JWT is invalid. */
3+
export function authenticateNo(client, callback, username) {
4+
return function (error) {
5+
log("AUTH FAIL " + username);
6+
log(error.message);
7+
log(error);
8+
client.authError = error;
9+
callback(null, false);
10+
};
11+
}

app/authentication/authenticate_ok.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { log } from "../logger";
2+
/** Creates a function that is triggered when a JWT is invalid. */
3+
export function authenticateOk(client, callback, username) {
4+
return function (permissions) {
5+
log("AUTH OK " + username);
6+
client.permissions = permissions;
7+
callback(null, true);
8+
};
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { canUseTopic } from "./can_use_topic";
22

3+
/** Determines if a user is allowed to publish to a particular topic. */
34
export function authorizePublish(client, topic, payload, callback) {
45
callback(null, canUseTopic(client, topic));
56
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { canUseTopic } from "./can_use_topic";
22

3+
/** Determines if a user is allowed to listen to a particular topic. */
34
export function authorizeSubscribe(client, topic, callback) {
45
callback(null, canUseTopic(client, topic));
56
}

app/security/can_use_topic.ts app/authorization/can_use_topic.ts

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

3-
// If a user has a bot of id XYZ, then they may access any topic
4-
// following pattern bot/XYZ/#
5-
3+
/** If a user has a bot of id XYZ, then they may access any topic
4+
* following pattern bot/XYZ/# */
65
export function canUseTopic(client, topic) {
76
let hasBot = topic &&
87
client &&
98
client.permissions &&
109
client.permissions.bot;
1110
if (!hasBot) {
12-
log("Tried to access topic " + (topic || "???") + " but no bot/topic provided.");
11+
log("Tried to access topic " +
12+
(topic || "???") +
13+
" but no bot/topic provided.");
1314
return false;
1415
};
1516
let botID = client.permissions.bot;

app/on_ready.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { authenticate } from "./security/authenticate";
2-
import { authorizePublish } from "./security/authorize_publish";
3-
import { authorizeSubscribe } from "./security/authorize_subscribe";
1+
import { authenticate } from "./authentication/authenticate";
2+
import { authorizePublish } from "./authorization/authorize_publish";
3+
import { authorizeSubscribe } from "./authorization/authorize_subscribe";
44
import { log } from "./logger";
55

66
export let onReady = (server) => () => {

app/security/authenticate.ts

-22
This file was deleted.

app/security/maybe_enable_ssl.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)