-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathauthService.js
More file actions
80 lines (59 loc) · 1.99 KB
/
authService.js
File metadata and controls
80 lines (59 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import net from 'net';
import R from 'ramda';
import uuid from 'uuid';
let tcpServer = null;
const usernameAndPasswordByRequestId = {};
export const createCredRequestId = () => uuid.v4();
export const getUsernameAndPassword = (credsRequestId) =>
usernameAndPasswordByRequestId[credsRequestId];
export const storeUsernameAndPassword = (credsRequestId, username, password) => {
usernameAndPasswordByRequestId[credsRequestId] = { username, password };
};
export const clearUsernameAndPassword = (credsRequestId) => {
delete usernameAndPasswordByRequestId[credsRequestId];
};
const socketListener = (socket) => {
socket.on('data', (data) => {
const input = data.toString();
const { credsRequestId, property } = JSON.parse(input) || {};
if (!credsRequestId || !property || !R.contains(property, ['username', 'password'])) {
socket.end();
throw new Error('Malformed request');
}
const credentials = getUsernameAndPassword(credsRequestId);
if (!credentials) {
socket.end();
throw new Error('No matching credentials for credsRequestId');
}
socket.end(credentials[property]);
});
};
export const ensureAuthServer = () => new Promise((resolve, reject) => {
if (tcpServer) {
resolve();
return;
}
tcpServer = net.createServer(socketListener);
tcpServer.on('error', reject);
tcpServer.listen({ port: 0, host: '127.0.0.1' }, resolve);
});
export const getAuthServerPort = () => (
tcpServer
? tcpServer.address().port
: undefined
);
let nodeBinaryPath = '';
export const setNodeBinaryPath = (binaryPath) => {
nodeBinaryPath = binaryPath;
};
export const getNodeBinaryPath = () => nodeBinaryPath;
let gitAskPassPath = null;
let gitAskPassClientPath = null;
export const getGitAskPassPath = () => gitAskPassPath;
export const setGitAskPassPath = (path) => {
gitAskPassPath = path;
};
export const getGitAskPassClientPath = () => gitAskPassClientPath;
export const setGitAskPassClientPath = (path) => {
gitAskPassClientPath = path;
};