Skip to content

Commit

Permalink
Merge branch 'main' into pubsub
Browse files Browse the repository at this point in the history
  • Loading branch information
vdfdev committed Aug 10, 2021
2 parents cbc2bcf + 241701f commit 8b25386
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/master/master.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,19 @@ describe('authentication', () => {

beforeEach(resetTestEnvironment);

test('auth success', async () => {
const authenticateCredentials = () => true;
const master = new Master(
game,
storage,
TransportAPI(send, sendAll),
new Auth({ authenticateCredentials })
);
const ret = await master.onChatMessage(matchID, chatMessage, undefined);
expect(ret).toBeUndefined();
expect(sendAll).toHaveBeenCalled();
});

test('auth failure', async () => {
const authenticateCredentials = () => false;
const master = new Master(
Expand All @@ -979,17 +992,17 @@ describe('authentication', () => {
expect(sendAll).not.toHaveBeenCalled();
});

test('auth success', async () => {
test('invalid packet', async () => {
const authenticateCredentials = () => true;
const master = new Master(
game,
storage,
TransportAPI(send, sendAll),
new Auth({ authenticateCredentials })
);
const ret = await master.onChatMessage(matchID, chatMessage, undefined);
expect(ret).toBeUndefined();
expect(sendAll).toHaveBeenCalled();
const ret = await master.onChatMessage(matchID, undefined, undefined);
expect(ret && ret.error).toBe('unauthorized');
expect(sendAll).not.toHaveBeenCalled();
});

test('default', async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/master/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ export class Master {
metadata: true,
}
);
if (!(chatMessage && typeof chatMessage.sender === 'string')) {
return { error: 'unauthorized' };
}
const isAuthentic = await this.auth.authenticateCredentials({
playerID: chatMessage.sender,
credentials,
Expand Down

0 comments on commit 8b25386

Please sign in to comment.