diff --git a/keyserver/src/user/identity.js b/keyserver/src/user/identity.js new file mode 100644 --- /dev/null +++ b/keyserver/src/user/identity.js @@ -0,0 +1,37 @@ +// @flow + +import type { QueryResults } from 'mysql'; + +import { SQL, dbQuery } from '../database/database.js'; + +const userIDMetadataKey = 'user_id'; +const accessTokenMetadataKey = 'access_token'; + +// This information is minted when registering with identity service +export type IdentityInfo = { +userId: string, +accessToken: string }; + +async function fetchIdentityInfo(): Promise { + const versionQuery = SQL` + SELECT data + FROM metadata + WHERE name IN (${userIDMetadataKey}, ${accessTokenMetadataKey}); + `; + + const [[userId, accessToken]] = await dbQuery(versionQuery); + if (!userId || !accessToken) { + return null; + } + return { userId, accessToken }; +} + +async function saveIdentityInfo(userInfo: IdentityInfo): Promise { + const updateQuery = SQL` + REPLACE INTO metadata (name, data) + VALUES (${userIDMetadataKey}, ${userInfo.userId}), + (${accessTokenMetadataKey}, ${userInfo.accessToken}) + `; + + return dbQuery(updateQuery); +} + +export { fetchIdentityInfo, saveIdentityInfo }; diff --git a/keyserver/src/user/login.js b/keyserver/src/user/login.js --- a/keyserver/src/user/login.js +++ b/keyserver/src/user/login.js @@ -1,7 +1,6 @@ // @flow import type { Account as OlmAccount } from '@commapp/olm'; -import type { QueryResults } from 'mysql'; import { getRustAPI } from 'rust-node-addon'; import type { OLMOneTimeKeys } from 'lib/types/crypto-types'; @@ -9,15 +8,12 @@ import { ServerError } from 'lib/utils/errors.js'; import { values } from 'lib/utils/objects.js'; -import { SQL, dbQuery } from '../database/database.js'; +import { saveIdentityInfo, fetchIdentityInfo } from './identity.js'; +import type { IdentityInfo } from './identity.js'; import { fetchCallUpdateOlmAccount } from '../updaters/olm-account-updater.js'; import { validateAccountPrekey } from '../utils/olm-utils.js'; type UserCredentials = { +username: string, +password: string }; -type IdentityInfo = { +userId: string, +accessToken: string }; - -const userIDMetadataKey = 'user_id'; -const accessTokenMetadataKey = 'access_token'; export type AccountKeysSet = { +identityKeys: string, @@ -59,30 +55,6 @@ account.mark_keys_as_published(); } -async function fetchIdentityInfo(): Promise { - const versionQuery = SQL` - SELECT data - FROM metadata - WHERE name IN (${userIDMetadataKey}, ${accessTokenMetadataKey}); - `; - - const [[userId, accessToken]] = await dbQuery(versionQuery); - if (!userId || !accessToken) { - return null; - } - return { userId, accessToken }; -} - -async function saveIdentityInfo(userInfo: IdentityInfo): Promise { - const updateQuery = SQL` - REPLACE INTO metadata (name, data) - VALUES (${userIDMetadataKey}, ${userInfo.userId}), - (${accessTokenMetadataKey}, ${userInfo.accessToken}) - `; - - return dbQuery(updateQuery); -} - async function verifyUserLoggedIn(): Promise { const result = await fetchIdentityInfo();