diff --git a/database/src/queries/communityHandshakes.ts b/database/src/queries/communityHandshakes.ts index 23fe4f0d1..9dc83118b 100644 --- a/database/src/queries/communityHandshakes.ts +++ b/database/src/queries/communityHandshakes.ts @@ -6,14 +6,17 @@ import { Ed25519PublicKey } from 'shared' * Find a pending community handshake by public key. * @param publicKey The public key of the community. * @param apiVersion The API version of the community. + * @param status The status of the community handshake. Optional, if not set, it will find a pending community handshake. * @returns The CommunityHandshakeState with associated federated community and community. */ -export function findPendingCommunityHandshake(publicKey: Ed25519PublicKey, apiVersion: string): Promise { +export function findPendingCommunityHandshake( + publicKey: Ed25519PublicKey, apiVersion: string, status?: CommunityHandshakeStateType +): Promise { return CommunityHandshakeState.findOne({ where: { publicKey: publicKey.asBuffer(), apiVersion, - status: Not(In([ + status: status || Not(In([ CommunityHandshakeStateType.EXPIRED, CommunityHandshakeStateType.FAILED, CommunityHandshakeStateType.SUCCESS diff --git a/federation/src/graphql/api/1_0/util/authenticateCommunity.ts b/federation/src/graphql/api/1_0/util/authenticateCommunity.ts index 23f336800..4b95898ba 100644 --- a/federation/src/graphql/api/1_0/util/authenticateCommunity.ts +++ b/federation/src/graphql/api/1_0/util/authenticateCommunity.ts @@ -41,7 +41,7 @@ export async function startOpenConnectionCallback( let state: DbCommunityHandshakeState | null = null try { - const pendingState = await findPendingCommunityHandshake(publicKey, api) + const pendingState = await findPendingCommunityHandshake(publicKey, api, CommunityHandshakeStateType.START_OPEN_CONNECTION_CALLBACK) if (pendingState) { const stateLogic = new CommunityHandshakeStateLogic(pendingState) // retry on timeout or failure @@ -134,15 +134,12 @@ export async function startAuthentication( if (!comB.publicJwtKey) { throw new Error('Public JWT key still not exist for foreign community') } - state = await findPendingCommunityHandshake(fedComBPublicKey, fedComB.apiVersion) + state = await findPendingCommunityHandshake(fedComBPublicKey, fedComB.apiVersion, CommunityHandshakeStateType.START_COMMUNITY_AUTHENTICATION) if (!state) { throw new Error('No pending community handshake found') } const stateLogic = new CommunityHandshakeStateLogic(state) - if ( - (await stateLogic.isTimeoutUpdate()) || - state.status !== CommunityHandshakeStateType.START_COMMUNITY_AUTHENTICATION - ) { + if ((await stateLogic.isTimeoutUpdate())) { methodLogger.debug('invalid state', new CommunityHandshakeStateLoggingView(state)) throw new Error('No valid pending community handshake found') }