Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import dns from "dns";
import Joi from "@hapi/joi";
import { getOrganizationIdFromUsername } from "../crud/organization";
import { Request, Response } from "express";
import slugify from "slugify";
import cryptoRandomString from "crypto-random-string";
import { Tokens } from "../interfaces/enum";
import { ApiKeyResponse } from "./jwt";
import { isMatch } from "matcher";
import Hashids from "hashids/cjs";
import { getUserIdFromUsername } from "../crud/user";
import { HASH_IDS, HASH_ID_PREFIX } from "../config";
import systemInfo from "systeminformation";
import pkg from "../../package.json";
const hashIds = new Hashids(
HASH_IDS,
10,
"abcdefghijklmnopqrstuvwxyz1234567890"
);
/**
* Capitalize each first letter in a string
*/
export const capitalizeEachFirstLetter = (string: string) =>
(string = string
.toLowerCase()
.split(" ")
.map(s => s.charAt(0).toUpperCase() + s.toLowerCase().substring(1))
.join(" "));
/**
import anonymize from "ip-anonymize";
import { User } from "../interfaces/tables/user";
import dns from "dns";
import Joi from "@hapi/joi";
import { getOrganizationIdFromUsername } from "../crud/organization";
import { Request, Response } from "express";
import slugify from "slugify";
import cryptoRandomString from "crypto-random-string";
import { Tokens } from "../interfaces/enum";
import { ApiKeyResponse } from "./jwt";
import { isMatch } from "matcher";
import Hashids from "hashids/cjs";
import { getUserIdFromUsername } from "../crud/user";
import { HASH_IDS, HASH_ID_PREFIX } from "../config";
const hashIds = new Hashids(
HASH_IDS,
10,
"abcdefghijklmnopqrstuvwxyz1234567890"
);
/**
* Capitalize each first letter in a string
*/
export const capitalizeEachFirstLetter = (string: string) =>
(string = string
.toLowerCase()
.split(" ")
.map(s => s.charAt(0).toUpperCase() + s.toLowerCase().substring(1))
.join(" "));
/**
const getInstance = type => {
let instance = instances[type];
if (!instance) {
instance = instances[type] = new Hashids(salt + type, 32, alphabet);
}
return instance;
};
public async createAccessToken(
@Arg('input') input: AccessTokenInput,
@Ctx() context: Context
): Promise {
const alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_.';
const salt = String(process.env.HASHID_SALT || 'keyboard dart cat');
const hash = crypto.createHmac('sha512', salt);
const hashid = new Hashids(salt, 108, alphabet);
hash.update(`${Math.floor(1000 * Math.random())}|${Date.now()}`);
const accessToken = this.accessTokenRepository.create(input);
const tokenSeed = hash.digest('hex').match(/.{1,8}/g) || [];
const token = hashid.encode(tokenSeed.map(num => parseInt(num, 16)));
if (!tokenSeed.length || token === '') {
throw new Error('Failed to generate access token');
}
accessToken.token = token;
accessToken.userId = context.user.id;
await this.accessTokenRepository.save(accessToken);
return accessToken;
}
import Hashids from 'hashids/cjs';
import { Repository } from 'typeorm';
const hashid = new Hashids(process.env.HASHID_SALT || 'SaltingTheHash', 10);
export const getUniqueHashId = async (repository: Repository, fieldName: string = 'id') => {
const id = hashid.encode(Date.now());
const count = await repository.count({
where: { [fieldName]: id },
});
return count === 0 ? id : getUniqueHashId(repository, fieldName);
};