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

Adds the automated creation of an admin account #93

Merged
merged 3 commits into from
Sep 23, 2021
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
2 changes: 1 addition & 1 deletion api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const startCache = async () => {
const main = async () => {
if (await Connection.open()) {
await startCache()
await startupChecks.startValidation()
await startupChecks.startValidation(app)
await challenges.createChallengeCache()

app.post('/v1/account/login', accounts.login);
Expand Down
1 change: 0 additions & 1 deletion api/controllers/Accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ const create = async (req, res, next) => {
challengeID: null,
timestamp: GTimestamp,
type: 'initial_register',
perms: 0,
points: 0,
correct: true,
submission: '',
Expand Down
47 changes: 45 additions & 2 deletions api/utils/startupChecks.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const Connection = require('./mongoDB.js')
const validators = require('./validators.js')
const crypto = require('crypto');
const argon2 = require('argon2');

const startValidation = async () => {
const startValidation = async (app) => {
const collections = Connection.collections
const db = Connection.db

// (1) Insert Validation
try {
Expand Down Expand Up @@ -33,7 +34,49 @@ const startValidation = async () => {
console.log("Transcations indexes created")
}

await createDefaultAdminAccount(collections.users, collections.transactions, app);

return true
}

async function createDefaultAdminAccount(userCollection, transactionColl, app) {
const adminAccount = await userCollection.findOne({type: 2});

if (adminAccount === null){
console.log("No admin account found, so one will be created.");
const adminUser = crypto.randomBytes(8).toString('hex');
const adminPassword = crypto.randomBytes(64).toString('hex');
const adminEmail = adminUser + "@localhost";
console.log("Admin Account Username: " + adminUser);
console.log("Admin Account Password: " + adminPassword);
console.log("Admin Account Email: " + adminEmail);

await userCollection.insertOne({
username: adminUser,
email: adminEmail,
password: await argon2.hash(adminPassword),
type: 2
});
let latestSolveSubmissionID = app.get("latestSolveSubmissionID")
if (isNaN(latestSolveSubmissionID)) latestSolveSubmissionID = 1
else latestSolveSubmissionID += 1
let insertDoc = {
author: adminUser.toLowerCase(),
challenge: 'Registered',
challengeID: null,
timestamp: new Date(),
type: 'initial_register',
points: 0,
correct: true,
submission: '',
lastChallengeID: latestSolveSubmissionID
}
let transactionsCache = app.get("transactionsCache")
transactionsCache.push(insertDoc)
app.set("transactionsCache", transactionsCache)
await transactionColl.insertOne(insertDoc)

}
};

module.exports = {startValidation}