Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring an Upgrading Packages #61

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.DS_Store
.vscode
node_modules/
db/
logs.txt
debug.txt
info.txt
errors.txt
warnings.txt
.DS_Store
.vscode
node_modules/
db/
logs.txt
debug.txt
info.txt
errors.txt
warnings.txt
dev/
190 changes: 95 additions & 95 deletions ClusterOperator/IdService.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,95 @@
/* eslint-disable no-else-return */
/* eslint-disable no-restricted-syntax */
const sessions = require('memory-cache');
const bitcoinMessage = require('bitcoinjs-message');
const fluxAPI = require('../lib/fluxAPI');
const config = require('./config');
const log = require('../lib/log');

class IdService {
static loginPhrases = [this.generateLoginPhrase(), this.generateLoginPhrase()];

static sessionExpireTime = 24 * 60 * 60 * 1000; // 24 hours

static ownerZelID = null;

/**
* [generateLoginPhrase]
*/
static generateLoginPhrase() {
const timestamp = new Date().getTime();
const phrase = timestamp + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
return phrase;
}

/**
* [addNewSession]
*/
static addNewSession(sessionID, userParams) {
// eslint-disable-next-line no-param-reassign
if (!userParams) userParams = 'NA';
sessions.put(sessionID, userParams, this.sessionExpireTime);
log.info(`new session from ${userParams}`);
return sessionID;
}

/**
* [verifySession]
*/
static verifySession(sessionID, userParams) {
// eslint-disable-next-line no-param-reassign
if (!userParams) userParams = 'NA';
const value = sessions.get(sessionID);
if (value !== userParams) return false;
sessions.put(sessionID, userParams, this.sessionExpireTime);
return true;
}

/**
* [removeSession]
*/
static removeSession(sessionID) {
log.info('session logged out.');
sessions.del(sessionID);
return true;
}

/**
* [verifyLogin]
*/
static verifyLogin(loginPhrase, signature) {
const timestamp = new Date().getTime();
const message = loginPhrase;
const maxHours = 30 * 60 * 1000;
// check timestamp
if (Number(message.substring(0, 13)) < (timestamp - maxHours) || Number(message.substring(0, 13)) > timestamp || message.length > 70 || message.length < 40) {
return false;
}
let isValid = false;
if (this.ownerZelID) bitcoinMessage.verify(message, this.ownerZelID, signature);
if (!isValid) isValid = bitcoinMessage.verify(message, '15c3aH6y9Koq1Dg1rGXE9Ypn5nL2AbSJCu', signature);
if (!isValid) isValid = bitcoinMessage.verify(message, '1PLscmdxWLUMStF1EShFSH836kgyKHKKFH', signature);
return isValid;
}

/**
* [getLoginPhrase]
*/
static getLoginPhrase() {
return this.loginPhrases[0];
}

/**
* [updateLoginPhrase]
*/
static updateLoginPhrase() {
this.loginPhrases.push(this.generateLoginPhrase());
this.loginPhrases.shift();
}

static async init() {
this.ownerZelID = await fluxAPI.getApplicationOwner(config.AppName);
}
}
// eslint-disable-next-line func-names
module.exports = IdService;
/* eslint-disable no-else-return */
/* eslint-disable no-restricted-syntax */
const sessions = require('memory-cache');
const bitcoinMessage = require('bitcoinjs-message');
const fluxAPI = require('../lib/fluxAPI');
const config = require('./config');
const log = require('../lib/log');
class IdService {
static loginPhrases = [this.generateLoginPhrase(), this.generateLoginPhrase()];
static sessionExpireTime = 24 * 60 * 60 * 1000; // 24 hours
static ownerZelID = null;
/**
* [generateLoginPhrase]
*/
static generateLoginPhrase() {
const timestamp = new Date().getTime();
const phrase = timestamp + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
return phrase;
}
/**
* [addNewSession]
*/
static addNewSession(sessionID, userParams) {
// eslint-disable-next-line no-param-reassign
if (!userParams) userParams = 'NA';
sessions.put(sessionID, userParams, this.sessionExpireTime);
log.info(`new session from ${userParams}`);
return sessionID;
}
/**
* [verifySession]
*/
static verifySession(sessionID, userParams) {
// eslint-disable-next-line no-param-reassign
if (!userParams) userParams = 'NA';
const value = sessions.get(sessionID);
if (value !== userParams) return false;
sessions.put(sessionID, userParams, this.sessionExpireTime);
return true;
}
/**
* [removeSession]
*/
static removeSession(sessionID) {
log.info('session logged out.');
sessions.del(sessionID);
return true;
}
/**
* [verifyLogin]
*/
static verifyLogin(loginPhrase, signature) {
const timestamp = new Date().getTime();
const message = loginPhrase;
const maxHours = 30 * 60 * 1000;
// check timestamp
if (Number(message.substring(0, 13)) < (timestamp - maxHours) || Number(message.substring(0, 13)) > timestamp || message.length > 70 || message.length < 40) {
return false;
}
let isValid = false;
if (this.ownerZelID) isValid = bitcoinMessage.verify(message, this.ownerZelID, signature);
if (!isValid) isValid = bitcoinMessage.verify(message, '15c3aH6y9Koq1Dg1rGXE9Ypn5nL2AbSJCu', signature);
if (!isValid) isValid = bitcoinMessage.verify(message, '1PLscmdxWLUMStF1EShFSH836kgyKHKKFH', signature);
return isValid;
}
/**
* [getLoginPhrase]
*/
static getLoginPhrase() {
return this.loginPhrases[0];
}
/**
* [updateLoginPhrase]
*/
static updateLoginPhrase() {
this.loginPhrases.push(this.generateLoginPhrase());
this.loginPhrases.shift();
}
static async init() {
this.ownerZelID = await fluxAPI.getApplicationOwner(config.AppName);
}
}
// eslint-disable-next-line func-names
module.exports = IdService;
Loading
Loading