export declare const databaseJWT = "import { DB } from './mongo';\nimport { FieldResolveInput } from 'stucco-js';\nimport { verify } from 'jsonwebtoken';\n\nexport const AdminCollection = 'AdminCollection';\nexport const UserCollection = 'UserCollection';\n\nconst decodeToken = (token: string) => {\n if (!process.env.JWT_SECRET) {\n throw new Error('JWT secret not set');\n }\n const verifiedToken = verify(token, process.env.JWT_SECRET);\n if (typeof verifiedToken !== 'object') {\n throw new Error('Token is not an object');\n }\n if (!('username' in verifiedToken)) {\n throw new Error('Invalid token');\n }\n return verifiedToken as { username: string };\n};\n\nexport const isAdmin = async (username: string): Promise => {\n const db = await DB();\n const col = await db.collection(AdminCollection);\n const adminExists = await col.findOne({\n username,\n });\n console.log(JSON.stringify({ adminExists: !!adminExists }, null, 4));\n return !!adminExists;\n};\n\nexport const getUser = async (token: string): Promise<{ username: string } | undefined> => {\n const db = await DB();\n const col = await db.collection(UserCollection);\n const { username } = decodeToken(token);\n const user = await col.findOne({\n username,\n });\n if (!user) {\n return;\n }\n return {\n username: user.username,\n };\n};\n\nexport const getUserFromHandlerInput = async (input: FieldResolveInput): Promise<{ username: string } | undefined> => {\n if (!input.protocol?.headers) {\n return;\n }\n const { Authorization }: { Authorization?: string[] } = input.protocol.headers;\n if (!Authorization) {\n return;\n }\n const findUser = await getUser(Authorization[0]);\n if (!findUser) {\n return;\n }\n return findUser;\n};\n\nexport const getUserFromHandlerInputOrThrow = async (input: FieldResolveInput): Promise<{ username: string }> => {\n const user = await getUserFromHandlerInput(input);\n if (!user) {\n throw new Error('You are not logged in');\n }\n return user;\n};\n\nexport const isAdminOrThrow = async (input: FieldResolveInput): Promise => {\n const user = await getUserFromHandlerInputOrThrow(input);\n const admin = isAdmin(user.username);\n if (!admin) {\n throw new Error('Only administrator of the system can access this endpoint');\n }\n};\n\nconst genRandomString = (length: number) =>\n crypto\n .randomBytes(Math.ceil(length / 2))\n .toString('hex')\n .slice(0, length);\n\nconst sha512 = (password: string, salt: string) => {\n var hash = crypto.createHmac('sha512', salt);\n hash.update(password);\n const passwordHash = hash.digest('hex');\n return {\n salt,\n passwordHash,\n };\n};\n\nexport const saltHashPassword = (password: string) => {\n const salt = genRandomString(16); /** Gives us salt of length 16 */\n return sha512(password, salt);\n};\n\nexport const comparePasswords = ({ password, hash, salt }: { password: string; hash: string; salt: string }) => {\n return hash === sha512(password, salt).passwordHash;\n};\n";