diff --git a/keyserver/src/endpoints.js b/keyserver/src/endpoints.js --- a/keyserver/src/endpoints.js +++ b/keyserver/src/endpoints.js @@ -18,7 +18,10 @@ calendarQueryUpdateResponder, } from './responders/entry-responders.js'; import type { JSONResponder } from './responders/handlers.js'; -import { getSessionPublicKeysResponder } from './responders/keys-responders.js'; +import { + getSessionPublicKeysResponder, + getOlmSessionInitializationDataResponder, +} from './responders/keys-responders.js'; import { messageReportCreationResponder } from './responders/message-report-responder.js'; import { textMessageCreationResponder, @@ -262,6 +265,10 @@ responder: updateUserAvatarResponder, requiredPolicies: baseLegalPolicies, }, + get_olm_session_initialization_data: { + responder: getOlmSessionInitializationDataResponder, + requiredPolicies: [], + }, }; export { jsonEndpoints }; diff --git a/keyserver/src/responders/keys-responders.js b/keyserver/src/responders/keys-responders.js --- a/keyserver/src/responders/keys-responders.js +++ b/keyserver/src/responders/keys-responders.js @@ -2,12 +2,19 @@ import t from 'tcomb'; -import type { GetSessionPublicKeysArgs } from 'lib/types/request-types.js'; +import type { + GetOlmSessionInitializationDataResponse, + GetOlmSessionInitializationDataRequest, + GetSessionPublicKeysArgs, +} from 'lib/types/request-types.js'; import type { SessionPublicKeys } from 'lib/types/session-types.js'; import { tShape } from 'lib/utils/validation-utils.js'; import { fetchSessionPublicKeys } from '../fetchers/key-fetchers.js'; +import { fetchKeyserverOlmAccount } from '../fetchers/olm-account-fetcher.js'; import type { Viewer } from '../session/viewer.js'; +import { updateOlmAccount } from '../updaters/olm-account-updater.js'; +import { validateAccountPrekey } from '../utils/olm-utils.js'; import { validateInput } from '../utils/validation-utils.js'; const getSessionPublicKeysInputValidator = tShape({ @@ -26,4 +33,46 @@ return await fetchSessionPublicKeys(request.session); } -export { getSessionPublicKeysResponder }; +const getOlmSessionInitializationDataRequestInputValidator = tShape({ + olmSessionType: t.enums.of(['primary', 'notifications']), + oneTimeKeysCount: t.Number, +}); + +async function getOlmSessionInitializationDataResponder( + viewer: Viewer, + input: any, +): Promise { + await validateInput( + viewer, + getOlmSessionInitializationDataRequestInputValidator, + input, + ); + const request: GetOlmSessionInitializationDataRequest = input; + + const { account, picklingKey } = await fetchKeyserverOlmAccount( + input.olmSessionType, + ); + + await validateAccountPrekey(account); + const prekey = account.prekey(); + + account.generate_one_time_keys(request.oneTimeKeysCount); + const oneTimeKeys = account.one_time_keys(); + account.mark_keys_as_published(); + + const identityKeys = account.identity_keys(); + + const isPrimary = request.olmSessionType === 'primary'; + await updateOlmAccount(account, picklingKey, isPrimary); + + return { + identityKeys, + prekey, + oneTimeKeys, + }; +} + +export { + getSessionPublicKeysResponder, + getOlmSessionInitializationDataResponder, +}; diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -14,7 +14,11 @@ ClientAvatar, UpdateUserAvatarRequest, } from '../types/avatar-types.js'; -import type { GetSessionPublicKeysArgs } from '../types/request-types.js'; +import type { + GetSessionPublicKeysArgs, + GetOlmSessionInitializationDataRequest, + GetOlmSessionInitializationDataResponse, +} from '../types/request-types.js'; import type { UserSearchResult } from '../types/search-types.js'; import type { SessionPublicKeys, @@ -235,6 +239,19 @@ return await callServerEndpoint('get_session_public_keys', data); }; +const getOlmSessionInitializationData = + ( + callServerEndpoint: CallServerEndpoint, + ): (( + data: GetOlmSessionInitializationDataRequest, + ) => Promise) => + async data => { + return await callServerEndpoint( + 'get_olm_session_initialization_data', + data, + ); + }; + const policyAcknowledgmentActionTypes = Object.freeze({ started: 'POLICY_ACKNOWLEDGMENT_STARTED', success: 'POLICY_ACKNOWLEDGMENT_SUCCESS', @@ -267,6 +284,7 @@ deleteAccount, deleteAccountActionTypes, getSessionPublicKeys, + getOlmSessionInitializationData, mergeUserInfos, logIn, logInActionTypes, diff --git a/lib/types/endpoints.js b/lib/types/endpoints.js --- a/lib/types/endpoints.js +++ b/lib/types/endpoints.js @@ -87,6 +87,7 @@ SIWE_NONCE: 'siwe_nonce', SIWE_AUTH: 'siwe_auth', UPDATE_USER_AVATAR: 'update_user_avatar', + GET_OLM_SESSION_INITIALIZATION_DATA: 'get_olm_session_initialization_data', }); type SocketPreferredEndpoint = $Values; diff --git a/lib/types/request-types.js b/lib/types/request-types.js --- a/lib/types/request-types.js +++ b/lib/types/request-types.js @@ -197,3 +197,14 @@ export type GetSessionPublicKeysArgs = { +session: string, }; + +export type GetOlmSessionInitializationDataRequest = { + +olmSessionType: 'primary' | 'notifications', + +oneTimeKeysCount: number, +}; + +export type GetOlmSessionInitializationDataResponse = { + +identityKeys: string, + +prekey: string, + +oneTimeKeys: string, +};