This repository was archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
176 lines (157 loc) · 5.18 KB
/
user.js
File metadata and controls
176 lines (157 loc) · 5.18 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const db = require('./lib/db');
const crypto = require('./lib/crypto');
const protocol = require('./protocol');
const settings = require('./settings');
function getUserInformation(userCode) {
return new Promise(async (resolve, reject) => {
try {
const client = await db.connect();
const doc = await client.db().collection('users').findOne({
accessCode: userCode
}, {
projection: {
_id: false,
email: true,
name: true,
accessCode: true
}
});
await client.close();
resolve(doc);
} catch (err) {
reject(err);
}
});
}
const users = [];
function signIn(examCode, userCode) {
return new Promise(async (resolve, reject) => {
if (users.find(e => (e.examCode === examCode) && (e.userCode === userCode))) { // 중복 로그인 차단
resolve(false);
} else {
try {
const client = await db.connect();
const doc1 = await client.db().collection('exams').findOne({
accessCode: examCode
});
const doc2 = await client.db().collection('users').findOne({
_id: {
$in: doc1.users
},
accessCode: userCode
}, {
projection: {
_id: false,
email: true,
name: true
}
});
await client.close();
doc2.examCode = examCode;
doc2.userCode = userCode;
doc2.accessLog = {
accessTime: new Date()
};
doc2.detected = [];
users.push(doc2);
updateAuthCode();
console.log(`${examCode} ${userCode} signed in. ${doc2.accessLog.accessTime}`);
resolve(true);
} catch (err) {
console.error(err);
resolve(false);
}
}
});
}
async function signOut(userCode) {
const found = users.find(e => e.userCode === userCode);
users.remove(found);
const client = await db.connect();
await client.db().collection('users').updateOne({
accessCode: userCode
}, {
$set: {
accessLog: found.accessLog,
detected: found.detected
}
});
await client.close();
}
function getUserByCode(userCode) {
return new Promise((resolve, reject) => {
const found = users.find(e => e.userCode === userCode);
if (found) {
resolve(found);
} else {
reject(new Error('Failed to get user by code.'));
}
});
}
function getUserByDesktop(desktop) {
return new Promise((resolve, reject) => {
const found = users.find(e => e.desktop === desktop);
if (found) {
resolve(found);
} else {
reject(new Error('Failed to get user by desktop.'));
}
});
}
function getUserByMobile(mobile) {
return new Promise((resolve, reject) => {
const found = users.find(e => e.mobile === mobile);
if (found) {
resolve(found);
} else {
reject(new Error('Failed to get user by mobile.'));
}
});
}
function updateAuthCode() { // 인증 코드 갱신
users.forEach(async e => {
e.authCode = (await crypto.randomBytes(settings.auth.size)).toString('base64');
if (e.desktop) { // 갱신된 인증 코드 전송
e.desktop.write(protocol.toBuffer({
type: 'AUT',
data: {
examCode: e.examCode,
userCode: e.userCode,
authCode: e.authCode
}
}));
}
if (e.lastAuthed && ((new Date() - e.lastAuthed) >= settings.auth.timeout)) {
if (e.desktop) {
e.desktop.write(protocol.toBuffer(protocol.authFailed));
}
if (e.mobile) {
e.mobile.write(protocol.toBuffer(protocol.authFailed));
}
}
});
}
function verifyAuthCode(examCode, userCode, authCode) {
return new Promise((resolve, reject) => {
const found = users.find(e => (e.examCode === examCode) && (e.userCode === userCode));
const result = found && found.authCode && (authCode === found.authCode);
if (result) {
found.lastAuthed = new Date();
}
resolve(result);
});
}
function getAuthCode(examCode, userCode) {
return new Promise((resolve, reject) => {
const found = users.find(e => (e.examCode === examCode) && (e.userCode === userCode));
if (found && found.authCode) {
resolve(found.authCode);
} else {
reject(new Error('Failed to get the auth code.'));
}
});
}
setInterval(updateAuthCode, settings.auth.interval);
module.exports = {
getUserInformation, signIn, signOut, getUserByCode, getUserByDesktop, getUserByMobile, updateAuthCode, verifyAuthCode, getAuthCode
};