Skip to content

Commit 9dbffbb

Browse files
committed
feature(gritty) add auth
1 parent 841e9f1 commit 9dbffbb

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ gritty('body', {
5252

5353
### Server API
5454

55-
#### gritty.listen(socket, options)
55+
#### gritty.listen(socket, [, options])
5656

5757
`Gritty` could be used as middleware:
5858

5959
```js
6060
const prefix = '/gritty'; // default
61-
const authCheck = (socket, success) => {}; // optional
61+
const authCheck = (accept, reject) => (username, password) => {
62+
accept();
63+
};
6264

6365
gritty.listen(socket, {
6466
prefix,
65-
authCheck,
67+
authCheck, // optional
6668
})
6769
```
6870

server/gritty.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const pty = require('node-pty-prebuilt');
1414
const Router = express.Router;
1515

1616
const terminalFn = currify(_terminalFn);
17-
const connection = wraptile(onConnection);
17+
const connectionWraped = wraptile(connection);
1818

1919
const CMD = process.platform === 'win32' ? 'cmd.exe' : 'bash';
2020
const isDev = process.env.NODE_ENV === 'development';
@@ -83,12 +83,13 @@ module.exports.listen = (socket, options) => {
8383
socket
8484
.of(prefix || '/gritty')
8585
.on('connection', (socket) => {
86-
const connect = connection(options, socket);
86+
const connect = connectionWraped(options, socket);
8787

8888
if (!authCheck)
89-
return connect();
89+
return connection(options, socket);
9090

91-
authCheck(socket, connect);
91+
const reject = () => socket.emit('reject');
92+
socket.on('auth', authCheck(connect, reject));
9293
});
9394
};
9495

@@ -102,7 +103,9 @@ function check(socket, options) {
102103
throw Error('options.authCheck should be a function!');
103104
}
104105

105-
function onConnection(options, socket) {
106+
function connection(options, socket) {
107+
socket.emit('accept');
108+
106109
let term;
107110

108111
socket.on('terminal', onTerminal);

test/before.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const gritty = require('../');
3+
const gritty = require('..');
44
const http = require('http');
55

66
const express = require('express');

test/server/gritty.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const test = require('tape');
44
const {promisify} = require('es6-promisify');
5+
const currify = require('currify');
56
const request = require('request');
67
const io = require('socket.io-client');
78
const diff = require('sinon-called-with-diff');
@@ -158,29 +159,24 @@ test('gritty: server: socket: emit data', (t) => {
158159
});
159160

160161
test('gritty: server: socket: authCheck', (t) => {
161-
const authCheck = (socket, connection) => {
162-
socket.on('auth', ({username, password}) => {
163-
if (username !== 'hello' || password !== 'world')
164-
return socket.emit('reject');
165-
166-
connection();
167-
socket.emit('accept');
168-
});
169-
};
162+
const authCheck = currify((accept, reject, username, password) => {
163+
if (username !== 'hello' || password !== 'world')
164+
return reject();
165+
166+
accept();
167+
});
170168

171169
before({authCheck}, (port, after) => {
172170
const socket = io(`http://localhost:${port}/gritty`);
173171

174172
socket.once('connect', () => {
175-
socket.emit('auth', {
176-
username: 'hello',
177-
password: 'world',
178-
});
173+
socket.emit('auth', 'hello', 'world');
179174

180175
socket.on('accept', () => {
181-
t.pass('should emit accepet');
182176
socket.close();
183177
after();
178+
179+
t.pass('should emit accepet');
184180
t.end();
185181
});
186182
});

0 commit comments

Comments
 (0)