forked from deep-foundation/dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.cjs
323 lines (312 loc) · 12.3 KB
/
passport.cjs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
require('react');
require('graphql');
require('lodash');
require('subscriptions-transport-ws');
const { generateApolloClient } = require("@deep-foundation/hasura/client");
const { DeepClient } = require('@deep-foundation/deeplinks/imports/client');
const { minilinks, Link } = require('@deep-foundation/deeplinks/imports/minilinks');
const apolloClient = generateApolloClient({
path: process.env.NEXT_PUBLIC_GQL_PATH || '', // <<= HERE PATH TO UPDATE
ssl: !!~process.env.NEXT_PUBLIC_GQL_PATH.indexOf('localhost') ? false : true,
});
const unloginedDeep = new DeepClient({ apolloClient });
const delay = (time = 1000) => new Promise(res => setTimeout(res, time));
const f = async () => {
const guest = await unloginedDeep.guest();
const guestDeep = new DeepClient({ deep: unloginedDeep, ...guest });
const admin = await guestDeep.login({ linkId: await guestDeep.id('deep', 'admin') });
const deep = new DeepClient({ deep: guestDeep, ...admin });
const { data: [{ id: packageId }] } = await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Package'),
string: { data: { value: '@deep-foundation/passport-username-password' } },
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: await guestDeep.id('deep', 'admin'),
string: { data: { value: 'passport' } },
} },
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'Join'),
to_id: await guestDeep.id('deep', 'admin'),
string: { data: { value: 'passport' } },
} },
});
await deep.insert([
{
type_id: await deep.id('@deep-foundation/core', 'Port'),
number: { data: { value: 4001 } },
in: { data: [
{
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'port' } },
},
{
type_id: await deep.id('@deep-foundation/core', 'RouterListening'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'routerListening' } },
} },
from: { data: {
type_id: await deep.id('@deep-foundation/core', 'Router'),
in: { data: [
{
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'router' } },
},
{
type_id: await deep.id('@deep-foundation/core', 'RouterStringUse'),
string: { data: { value: '/passport' } },
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'routerStringUse' } },
} },
from: { data: {
type_id: await deep.id('@deep-foundation/core', 'Route'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'route' } },
} },
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'HandleRoute'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'handleRoute' } },
} },
to: { data: {
type_id: await deep.id('@deep-foundation/core', 'Handler'),
from_id: await deep.id('@deep-foundation/core', 'dockerSupportsJs'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'handler' } },
} },
to: { data: {
type_id: await deep.id('@deep-foundation/core', 'SyncTextFile'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'syncTextFile' } },
} },
string: { data: {
value: /*javascript*/`
async (req, res, next, { deep, require, gql }) => {
const id = req?.query?.id;
if (!id) res.send({ error: '!id' });
var path = require('path');
var express = require('express');
var passport = require('passport');
var LocalStrategy = require('passport-local');
var session = require('express-session');
var crypto = require('crypto');
const set = async (linkId) => {
console.log('set', linkId);
await deep.apolloClient.query({
query: gql\`query SET($id: String!, $linkId: Int) { authorization { set(input: { id: $id, linkId: $linkId }) { error } } }\`,
variables: { id, linkId },
});
};
const setError = async (error) => {
console.log('setError', error);
await deep.apolloClient.query({
query: gql\`query SET($id: String!, $error: String) { authorization { set(input: { id: $id, error: $error }) { error } } }\`,
variables: { id, error },
});
};
const router = express.Router();
router.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: false,
}));
router.use(passport.initialize());
router.use(passport.session());
router.use(passport.authenticate('session'));
passport.serializeUser(function(user, cb) {
process.nextTick(function() {
console.log('serializeUser', user);
cb(null, { id: user.id });
});
});
passport.deserializeUser(function(user, cb) {
process.nextTick(function() {
console.log('deserializeUser', user);
return cb(null, user);
});
});
passport.use(new LocalStrategy(async function verify(username, password, cb) {
crypto.pbkdf2(password, 'salt', 310000, 32, 'sha256', async function(err, hashedPassword) {
if (err) {
// if error, return error
await setError(String(err));
cb(err, null);
return;
}
// try to found exists username
const { data: usernames } = await deep.select({
type_id: await deep.id('@deep-foundation/passport-username-password', 'Username'),
string: { value: { _eq: username } },
});
if (usernames.length) {
// if username found
const usernameId = usernames[0].id;
const { data: passwords } = await deep.select({
type_id: await deep.id('@deep-foundation/passport-username-password', 'Password'),
from_id: usernameId,
});
if (!passwords.length) {
// if password not found
await setError('!password');
cb('!password', null);
return;
}
const foundedPassword = passwords[0]?.value?.value;
if (foundedPassword != hashedPassword.toString()) {
// if password incorrect
await setError('incorrect');
cb('incorrect', null);
return;
}
// if password correct
await set(usernameId);
cb(null, usernameId);
return;
} else {
// if username not found, create new username
const { data: [{ id: usernameId }] } = await deep.insert({
type_id: await deep.id('@deep-foundation/passport-username-password', 'Username'),
string: { data: { value: username } },
});
// and create new password
const { data: [{ id: passwordId }] } = await deep.insert({
type_id: await deep.id('@deep-foundation/passport-username-password', 'Password'),
string: { data: { value: hashedPassword.toString() } },
from_id: usernameId,
to_id: usernameId,
});
// and set authorization
await set(usernameId);
cb(null, usernameId);
return;
}
});
}));
router.use('*', passport.authenticate('local', {}));
router.handle(req, res, () => {
res.send(JSON.stringify({}));
});
}
`,
} },
} },
} },
} },
} },
}
] },
} },
},
] },
},
{
type_id: await deep.id('@deep-foundation/core', 'Type'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'Username' } },
} },
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'String'),
} },
},
{
type_id: await deep.id('@deep-foundation/core', 'Type'),
from_id: await deep.id('@deep-foundation/core', 'Any'),
to_id: await deep.id('@deep-foundation/core', 'Any'),
in: { data: {
type_id: await deep.id('@deep-foundation/core', 'Contain'),
from_id: packageId,
string: { data: { value: 'Password' } },
} },
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'Value'),
to_id: await deep.id('@deep-foundation/core', 'String'),
} },
},
]);
await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Rule'),
out: { data: [
{
type_id: await deep.id('@deep-foundation/core', 'RuleSubject'),
to: { data: {
type_id: await deep.id('@deep-foundation/core', 'Selector'),
out: { data: [
{
type_id: await deep.id('@deep-foundation/core', 'SelectorInclude'),
to_id: await deep.id('@deep-foundation/passport-username-password', 'Username'),
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'SelectorTree'),
to_id: await deep.id('@deep-foundation/core', 'joinTree'),
}, },
},
] },
}, },
},
{
type_id: await deep.id('@deep-foundation/core', 'RuleObject'),
to: { data: {
type_id: await deep.id('@deep-foundation/core', 'Selector'),
out: { data: [
{
type_id: await deep.id('@deep-foundation/core', 'SelectorInclude'),
to_id: await deep.id('@deep-foundation/passport-username-password', 'Username'),
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'SelectorTree'),
to_id: await deep.id('@deep-foundation/core', 'containTree'),
}, },
},
{
type_id: await deep.id('@deep-foundation/core', 'SelectorInclude'),
to_id: await deep.id('@deep-foundation/passport-username-password', 'Password'),
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'SelectorTree'),
to_id: await deep.id('@deep-foundation/core', 'containTree'),
}, },
},
], },
}, },
},
{
type_id: await deep.id('@deep-foundation/core', 'RuleAction'),
to: { data: {
type_id: await deep.id('@deep-foundation/core', 'Selector'),
out: { data: [
{
type_id: await deep.id('@deep-foundation/core', 'SelectorInclude'),
to_id: await deep.id('@deep-foundation/core', 'AllowSelectType'),
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'SelectorTree'),
to_id: await deep.id('@deep-foundation/core', 'containTree'),
}, },
},
{
type_id: await deep.id('@deep-foundation/core', 'SelectorInclude'),
to_id: await deep.id('@deep-foundation/core', 'AllowInsertType'),
out: { data: {
type_id: await deep.id('@deep-foundation/core', 'SelectorTree'),
to_id: await deep.id('@deep-foundation/core', 'containTree'),
}, },
},
], },
}, },
},
], },
});
};
f();