@@ -134,6 +134,10 @@ export class MSC3906Rendezvous {
134
134
this . _code = JSON . stringify ( raw ) ;
135
135
}
136
136
137
+ /**
138
+ *
139
+ * @returns the checksum of the secure channel if the rendezvous set up was successful, otherwise undefined
140
+ */
137
141
public async startAfterShowingCode ( ) : Promise < string | undefined > {
138
142
const checksum = await this . channel . connect ( ) ;
139
143
@@ -158,17 +162,43 @@ export class MSC3906Rendezvous {
158
162
} ) ;
159
163
160
164
logger . info ( "Waiting for other device to chose protocol" ) ;
161
- const { type, protocol, outcome, reason, intent } = await this . receive ( ) ;
165
+ const nextPayload = await this . receive ( ) ;
166
+
167
+ this . checkForV1Fallback ( nextPayload ) ;
162
168
169
+ const protocol = this . v1FallbackEnabled
170
+ ? await this . handleV1ProtocolPayload ( nextPayload )
171
+ : await this . handleV2ProtocolPayload ( nextPayload ) ;
172
+
173
+ // invalid protocol
174
+ if ( ! protocol || ! LOGIN_TOKEN_PROTOCOL . matches ( protocol ) ) {
175
+ await this . cancel ( RendezvousFailureReason . UnsupportedAlgorithm ) ;
176
+ return undefined ;
177
+ }
178
+
179
+ return checksum ;
180
+ }
181
+
182
+ private checkForV1Fallback ( { type } : MSC3906RendezvousPayload ) : void {
163
183
// even if we didn't start in v1 fallback we might detect that the other device is v1
164
184
if ( type === PayloadType . Finish || type === PayloadType . Progress ) {
165
185
// this is a PDU from a v1 flow so use fallback mode
166
186
this . v1FallbackEnabled = true ;
167
187
}
188
+ }
168
189
169
- // fallback for v1 flow
190
+ /**
191
+ *
192
+ * @returns true if the protocol was received successfully, false otherwise
193
+ */
194
+ private async handleV1ProtocolPayload ( {
195
+ type,
196
+ protocol,
197
+ outcome,
198
+ reason,
199
+ intent,
200
+ } : MSC3906RendezvousPayload ) : Promise < string | void > {
170
201
if ( type === PayloadType . Finish ) {
171
- this . v1FallbackEnabled = true ;
172
202
// new device decided not to complete
173
203
let reason : RendezvousFailureReason ;
174
204
if ( intent ) {
@@ -182,9 +212,29 @@ export class MSC3906Rendezvous {
182
212
reason = RendezvousFailureReason . Unknown ;
183
213
}
184
214
await this . cancel ( reason ) ;
185
- return undefined ;
215
+ return ;
216
+ }
217
+
218
+ // unexpected payload
219
+ if ( type !== PayloadType . Progress ) {
220
+ await this . cancel ( RendezvousFailureReason . Unknown ) ;
221
+ return ;
186
222
}
187
223
224
+ return protocol ;
225
+ }
226
+
227
+ /**
228
+ *
229
+ * @returns true if the protocol was received successfully, false otherwise
230
+ */
231
+ private async handleV2ProtocolPayload ( {
232
+ type,
233
+ protocol,
234
+ outcome,
235
+ reason,
236
+ intent,
237
+ } : MSC3906RendezvousPayload ) : Promise < string | void > {
188
238
// v2 flow
189
239
if ( type === PayloadType . Failure ) {
190
240
// new device decided not to complete
@@ -206,28 +256,16 @@ export class MSC3906Rendezvous {
206
256
failureReason = RendezvousFailureReason . Unknown ;
207
257
}
208
258
await this . cancel ( failureReason ) ;
209
- return undefined ;
210
- }
211
-
212
- // v1 unexpected payload
213
- if ( this . v1FallbackEnabled && type !== PayloadType . Progress ) {
214
- await this . cancel ( RendezvousFailureReason . Unknown ) ;
215
- return undefined ;
259
+ return ;
216
260
}
217
261
218
- // v2 unexpected payload
219
- if ( ! this . v1FallbackEnabled && type !== PayloadType . Protocol ) {
262
+ // unexpected payload
263
+ if ( type !== PayloadType . Protocol ) {
220
264
await this . cancel ( RendezvousFailureReason . Unknown ) ;
221
- return undefined ;
222
- }
223
-
224
- // invalid protocol
225
- if ( ! protocol || ! LOGIN_TOKEN_PROTOCOL . matches ( protocol ) ) {
226
- await this . cancel ( RendezvousFailureReason . UnsupportedAlgorithm ) ;
227
- return undefined ;
265
+ return ;
228
266
}
229
267
230
- return checksum ;
268
+ return protocol ;
231
269
}
232
270
233
271
private async receive ( ) : Promise < MSC3906RendezvousPayload > {
0 commit comments