diff --git a/keyserver/src/creators/account-creator.js b/keyserver/src/creators/account-creator.js --- a/keyserver/src/creators/account-creator.js +++ b/keyserver/src/creators/account-creator.js @@ -3,7 +3,6 @@ import { getRustAPI } from 'rust-node-addon'; import bcrypt from 'twin-bcrypt'; -import ashoat from 'lib/facts/ashoat.js'; import bots from 'lib/facts/bots.js'; import genesis from 'lib/facts/genesis.js'; import { policyTypes } from 'lib/facts/policies.js'; @@ -54,6 +53,7 @@ import { fetchOlmAccount } from '../updaters/olm-account-updater.js'; import { updateThread } from '../updaters/thread-updaters.js'; import { viewerAcknowledgmentUpdater } from '../updaters/viewer-acknowledgment-updater.js'; +import { thisKeyserverAdmin } from '../user/identity.js'; const { commbot } = bots; @@ -76,7 +76,7 @@ throw new ServerError('invalid_username'); } - const promises = [searchForUser(request.username)]; + const promises = [searchForUser(request.username), thisKeyserverAdmin()]; const { calendarQuery, signedIdentityKeysBlob, @@ -86,7 +86,7 @@ promises.push(verifyCalendarQueryThreadIDs(calendarQuery)); } - const [existingUser] = await Promise.all(promises); + const [existingUser, admin] = await Promise.all(promises); if ( reservedUsernamesSet.has(request.username.toLowerCase()) || isValidEthereumAddress(request.username.toLowerCase()) @@ -136,7 +136,7 @@ await Promise.all([ updateThread( - createScriptViewer(ashoat.id), + createScriptViewer(admin.id), { threadID: genesis().id, changes: { newMemberIDs: [id] }, @@ -153,7 +153,7 @@ viewer, { type: threadTypes.PERSONAL, - initialMemberIDs: [ashoat.id], + initialMemberIDs: [admin.id], }, { forceAddMembers: true }, ), @@ -165,7 +165,7 @@ const ashoatMessageDatas = ashoatMessages.map(message => ({ type: messageTypes.TEXT, threadID: ashoatThreadID, - creatorID: ashoat.id, + creatorID: admin.id, time: messageTime++, text: message, })); @@ -308,9 +308,11 @@ } async function processAccountCreationCommon(viewer: Viewer) { + const admin = await thisKeyserverAdmin(); + await Promise.all([ updateThread( - createScriptViewer(ashoat.id), + createScriptViewer(admin.id), { threadID: genesis().id, changes: { newMemberIDs: [viewer.userID] }, @@ -326,7 +328,7 @@ viewer, { type: threadTypes.PERSONAL, - initialMemberIDs: [ashoat.id], + initialMemberIDs: [admin.id], }, { forceAddMembers: true }, ), @@ -338,7 +340,7 @@ const ashoatMessageDatas = ashoatMessages.map(message => ({ type: messageTypes.TEXT, threadID: ashoatThreadID, - creatorID: ashoat.id, + creatorID: admin.id, time: messageTime++, text: message, })); diff --git a/keyserver/src/database/setup-db.js b/keyserver/src/database/setup-db.js --- a/keyserver/src/database/setup-db.js +++ b/keyserver/src/database/setup-db.js @@ -1,6 +1,5 @@ // @flow -import ashoat from 'lib/facts/ashoat.js'; import bots from 'lib/facts/bots.js'; import genesis from 'lib/facts/genesis.js'; import { usernameMaxLength } from 'lib/shared/account-utils.js'; @@ -17,14 +16,17 @@ } from '../database/migration-config.js'; import { createScriptViewer } from '../session/scripts.js'; import { ensureUserCredentials } from '../user/checks.js'; +import { thisKeyserverAdmin } from '../user/identity.js'; +import { verifyUserLoggedIn } from '../user/login.js'; async function setupDB() { await ensureUserCredentials(); await createTables(); + await createOlmAccounts(); + await verifyUserLoggedIn(); await createUsers(); await createThreads(); await setUpMetadataTable(); - await createOlmAccounts(); } async function createTables() { @@ -423,22 +425,28 @@ } async function createUsers() { - const [user1, user2] = sortUserIDs(bots.commbot.userID, ashoat.id); - await dbQuery( - SQL` + const admin = await thisKeyserverAdmin(); + + const [user1, user2] = sortUserIDs(bots.commbot.userID, admin.id); + const query = SQL` INSERT INTO ids (id, table_name) VALUES - (${bots.commbot.userID}, 'users'), - (${ashoat.id}, 'users'); + (${bots.commbot.userID}, 'users'); INSERT INTO users (id, username, hash, avatar, creation_time) VALUES (${bots.commbot.userID}, 'commbot', '', NULL, 1530049900980), - (${ashoat.id}, 'ashoat', '', NULL, 1463588881886); + (${admin.id}, ${admin.username}, '', NULL, 1463588881886); INSERT INTO relationships_undirected (user1, user2, status) VALUES (${user1}, ${user2}, ${undirectedStatus.KNOW_OF}); - `, - { multipleStatements: true }, - ); + `; + + if (!isNaN(Number(admin.id))) { + query.append(SQL` + INSERT INTO ids (id, table_name) + VALUES (${admin.id}, 'users'); + `); + } + await dbQuery(query, { multipleStatements: true }); } const createThreadOptions = { forceAddMembers: true }; @@ -451,7 +459,9 @@ (${bots.commbot.staffThreadID}, 'threads') `); - const ashoatViewer = createScriptViewer(ashoat.id); + const admin = await thisKeyserverAdmin(); + + const ashoatViewer = createScriptViewer(admin.id); const createGenesisPromise = createThread( ashoatViewer, { @@ -471,7 +481,7 @@ { id: bots.commbot.staffThreadID, type: threadTypes.COMMUNITY_SECRET_SUBTHREAD, - initialMemberIDs: [ashoat.id], + initialMemberIDs: [admin.id], }, createThreadOptions, ); diff --git a/keyserver/src/scripts/create-community.js b/keyserver/src/scripts/create-community.js --- a/keyserver/src/scripts/create-community.js +++ b/keyserver/src/scripts/create-community.js @@ -1,17 +1,18 @@ // @flow -import ashoat from 'lib/facts/ashoat.js'; import { threadTypes } from 'lib/types/thread-types-enum.js'; import { main } from './utils.js'; import { createThread } from '../creators/thread-creator.js'; import { createScriptViewer } from '../session/scripts.js'; +import { thisKeyserverAdmin } from '../user/identity.js'; const communityName = 'New community'; async function createCommunity() { - const ashoatViewer = createScriptViewer(ashoat.id); - await createThread(ashoatViewer, { + const admin = await thisKeyserverAdmin(); + const adminViewer = createScriptViewer(admin.id); + await createThread(adminViewer, { type: threadTypes.COMMUNITY_ROOT, name: communityName, }); diff --git a/keyserver/src/scripts/create-many-threads-to-trigger-crash-loop.js b/keyserver/src/scripts/create-many-threads-to-trigger-crash-loop.js --- a/keyserver/src/scripts/create-many-threads-to-trigger-crash-loop.js +++ b/keyserver/src/scripts/create-many-threads-to-trigger-crash-loop.js @@ -1,9 +1,9 @@ // @flow -import ashoat from 'lib/facts/ashoat.js'; import { main } from './utils.js'; import { createThread } from '../creators/thread-creator.js'; import { createScriptViewer } from '../session/scripts.js'; +import { thisKeyserverAdmin } from '../user/identity.js'; const testUserID = ''; const numOfThreads = 1000; @@ -36,7 +36,8 @@ // the app should be in a crash loop async function createManyThreadsToTriggerCrashLoop() { - await createThreads(numOfThreads, testUserID, ashoat.id); + const admin = await thisKeyserverAdmin(); + await createThreads(numOfThreads, testUserID, admin.id); } main([createManyThreadsToTriggerCrashLoop]); diff --git a/keyserver/src/scripts/make-channel-private.js b/keyserver/src/scripts/make-channel-private.js --- a/keyserver/src/scripts/make-channel-private.js +++ b/keyserver/src/scripts/make-channel-private.js @@ -1,16 +1,17 @@ // @flow -import ashoat from 'lib/facts/ashoat.js'; import { threadTypes } from 'lib/types/thread-types-enum.js'; import { main } from './utils.js'; import { createScriptViewer } from '../session/scripts.js'; import { updateThread } from '../updaters/thread-updaters.js'; +import { thisKeyserverAdmin } from '../user/identity.js'; const channelID = '-1'; async function makeChannelPrivate() { - const viewer = createScriptViewer(ashoat.id); + const admin = await thisKeyserverAdmin(); + const viewer = createScriptViewer(admin.id); await updateThread(viewer, { threadID: channelID, changes: { type: threadTypes.COMMUNITY_SECRET_SUBTHREAD }, diff --git a/keyserver/src/scripts/move-threads.js b/keyserver/src/scripts/move-threads.js --- a/keyserver/src/scripts/move-threads.js +++ b/keyserver/src/scripts/move-threads.js @@ -1,14 +1,15 @@ // @flow -import ashoat from 'lib/facts/ashoat.js'; import { threadTypes } from 'lib/types/thread-types-enum.js'; import { main } from './utils.js'; import { createScriptViewer } from '../session/scripts.js'; import { updateThread } from '../updaters/thread-updaters.js'; +import { thisKeyserverAdmin } from '../user/identity.js'; async function moveThreads() { - const viewer = createScriptViewer(ashoat.id); + const admin = await thisKeyserverAdmin(); + const viewer = createScriptViewer(admin.id); await updateThread( viewer, { diff --git a/keyserver/src/user/identity.js b/keyserver/src/user/identity.js --- a/keyserver/src/user/identity.js +++ b/keyserver/src/user/identity.js @@ -2,6 +2,7 @@ import type { QueryResults } from 'mysql'; +import ashoat from 'lib/facts/ashoat.js'; import { getCommConfig } from 'lib/utils/comm-config.js'; import { ashoatKeyserverID } from 'lib/utils/validation-utils.js'; @@ -50,6 +51,31 @@ return identityInfo?.userId ?? ashoatKeyserverID; } +export type AdminData = { + +username: string, + +id: string, +}; + +async function thisKeyserverAdmin(): Promise { + const config = await getCommConfig({ + folder: 'secrets', + name: 'user_credentials', + }); + + if (!config) { + return { + id: ashoat.id, + username: ashoat.username, + }; + } + const id = await thisKeyserverID(); + + return { + id, + username: config.username, + }; +} + function saveIdentityInfo(userInfo: IdentityInfo): Promise { const updateQuery = SQL` REPLACE INTO metadata (name, data) @@ -60,4 +86,9 @@ return dbQuery(updateQuery); } -export { fetchIdentityInfo, thisKeyserverID, saveIdentityInfo }; +export { + fetchIdentityInfo, + thisKeyserverID, + thisKeyserverAdmin, + saveIdentityInfo, +}; diff --git a/lib/facts/ashoat.js b/lib/facts/ashoat.js --- a/lib/facts/ashoat.js +++ b/lib/facts/ashoat.js @@ -4,12 +4,14 @@ +id: string, +email: string, +landing_email: string, + +username: string, }; const ashoat: Ashoat = { id: '256', email: 'ashoat@gmail.com', landing_email: 'postmaster@comm.app', + username: 'ashoat', }; export default ashoat;