weverse
Version:
Wrapper for weverse private API
53 lines (52 loc) • 1.55 kB
JavaScript
import path from 'path';
import fs from 'fs';
import NodeRSA from 'node-rsa';
export function readKey() {
try {
return fs.readFileSync(path.join(__dirname, './publicCert.txt'), 'utf-8');
}
catch (e) {
console.log('Weverse: error reading public key from disk', e);
return null;
}
}
export function encryptPassword(pass, pubKey) {
if (!pubKey)
return null;
const key = new NodeRSA();
key.importKey(pubKey, 'public');
const enc = key.encrypt(Buffer.from(pass));
return enc.toString('base64');
}
export const validateStatus = (status) => status >= 200 && status < 500;
export function createLoginPayload(credentials) {
const publicKey = readKey();
const encryptedPassword = encryptPassword(credentials.password, publicKey);
if (encryptedPassword === null)
throw 'Error encrypting Weverse password';
return {
username: credentials.username,
password: encryptedPassword,
grant_type: 'password',
client_id: 'weverse-test'
};
}
export function createRefreshPayload(credentials) {
return {
refresh_token: credentials.refresh_token,
grant_type: 'refresh_token',
client_id: 'weverse-test'
};
}
export function isWeversePasswordAuthorization(val) {
return val.token === undefined &&
typeof val.password === 'string' &&
typeof val.username === 'string';
}
export function AssignType() {
return class {
constructor(t) {
Object.assign(this, t);
}
};
}