This commit is contained in:
Evgenii Abramov
2021-01-18 01:52:32 +03:00
commit d6ae5dc1ea
26 changed files with 4083 additions and 0 deletions

54
db/access_tokens.js Normal file
View File

@@ -0,0 +1,54 @@
'use strict';
const loki = require('lokijs');
global.dbl = new loki('./loki.json', {
autoload: true,
autosave: true,
autosaveInterval: 5000,
autoloadCallback() {
global.authl = global.dbl.getCollection('tokens');
if (global.authl === null) {
global.authl = global.dbl.addCollection('tokens');
}
}
});
module.exports.find = (key, done) => {
const ltoken = global.authl.findOne({'token': key});
if (ltoken){
console.log('Token found');
const {userId, clientId} = ltoken;
return done(null, {userId, clientId})
} else {
return done(new Error('Token Not Found'));
}
};
module.exports.findByUserIdAndClientId = (userId, clientId, done) => {
const ltoken = global.authl.findOne({'userId': userId});
if (ltoken){
console.log('Load token by userId: User found');
const {token, userId: uid, clientId: cid} = ltoken;
if (uid === userId && cid === clientId) return done(null, token);
else return done(new Error('Token Not Found'));
} else {
console.log('User not found');
return done(new Error('User Not Found'));
}
};
module.exports.save = (token, userId, clientId, done) => {
console.log('Start saving token');
const ltoken = global.authl.findOne({'userId': userId});
if (ltoken){
console.log('User Updated');
global.authl.update(Object.assign({}, ltoken, {token, userId, clientId}));
} else {
console.log('User not Found. Create new...');
global.authl.insert({'type': 'token', token, userId, clientId});
}
done();
};
/* works */

13
db/authorization_codes.js Normal file
View File

@@ -0,0 +1,13 @@
'use strict';
const codes = {};
module.exports.find = (key, done) => {
if (codes[key]) return done(null, codes[key]);
return done(new Error('Code Not Found'));
};
module.exports.save = (code, clientId, redirectUri, userId, userName, done) => {
codes[code] = {clientId, redirectUri, userId, userName};
done();
};

17
db/clients.js Normal file
View File

@@ -0,0 +1,17 @@
'use strict';
const {clients} = require('../config');
module.exports.findById = (id, done) => {
for (const client of clients) {
if (client.id === id) return done(null, client);
}
return done(new Error('Client Not Found'));
};
module.exports.findByClientId = (clientId, done) => {
for (const client of clients) {
if (client.clientId === clientId) return done(null, client);
}
return done(new Error('Client Not Found'));
};

13
db/index.js Normal file
View File

@@ -0,0 +1,13 @@
'use strict';
const users = require('./users');
const clients = require('./clients');
const accessTokens = require('./access_tokens');
const authorizationCodes = require('./authorization_codes');
module.exports = {
users,
clients,
accessTokens,
authorizationCodes,
};

17
db/users.js Normal file
View File

@@ -0,0 +1,17 @@
'use strict';
const {users} = require('../config');
module.exports.findById = (id, done) => {
for (const user of users) {
if (user.id === id) return done(null, user);
}
return done(new Error('User Not Found'));
};
module.exports.findByUsername = (username, done) => {
for (const user of users) {
if (user.username === username) return done(null, user);
}
return done(new Error('User Not Found'));
};