UNPKG

matrix-js-sdk

Version:
138 lines (128 loc) 6.11 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; /* Copyright 2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import { OAuthGrantType } from "../matrix.js"; import { MSC4108FailureReason } from "./RendezvousFailureReason.js"; import { MSC4108SignInWithQR } from "./MSC4108SignInWithQR.js"; import { MSC4108RendezvousSession } from "./transports/MSC4108RendezvousSession.js"; import { MSC4108SecureChannel } from "./channels/MSC4108SecureChannel.js"; import { RendezvousIntent } from "./RendezvousIntent.js"; import { logger } from "../logger.js"; export * from "./MSC4108SignInWithQR.js"; export * from "./RendezvousError.js"; export * from "./RendezvousFailureReason.js"; export * from "./RendezvousIntent.js"; export * from "./transports/index.js"; export * from "./channels/index.js"; /** * Check if the homeserver that the client is connected to supports a variant of sign-in with QR that we can use. * * @param client the client to check for sign-in with QR support * @returns true if the homeserver that the client is connected to supports a variant of sign-in with QR that we can use, false otherwise. */ export function isSignInWithQRAvailable(_x) { return _isSignInWithQRAvailable.apply(this, arguments); } /** * Start a linking flow from an existing authenticated client by generating a QR code that can be scanned by the new device. * The new device will then authenticate with the server and link itself to the same account as the existing client and * share the end-to-end encryption keys. * * @param client the existing client * @param onFailure callback for when the linking process fails * @param abortSignal an AbortSignal that can be used to cancel the linking process, * for example when the user cancels out of the flow. * This will unbind the {@link onFailure} callback and prevent any further steps in the flow from being executed. * @returns a promise that resolves to an instance of the linking flow */ function _isSignInWithQRAvailable() { _isSignInWithQRAvailable = _asyncToGenerator(function* (client) { var metadata; try { metadata = yield client.getAuthMetadata(); } catch (e) { logger.warn("Failed to fetch auth metadata, assuming sign-in with QR is unavailable", e); return false; } // check for support of device authorization grant if (!metadata.grant_types_supported.includes(OAuthGrantType.DeviceAuthorization)) { return false; } // check for unstable support for MSC4108 2024 version return client.doesServerSupportUnstableFeature("org.matrix.msc4108"); }); return _isSignInWithQRAvailable.apply(this, arguments); } export function linkNewDeviceByGeneratingQR(_x2, _x3, _x4) { return _linkNewDeviceByGeneratingQR.apply(this, arguments); } /** * Start a sign-in flow by generating a QR code that can be scanned by an existing authenticated client. * The existing client will then help complete the authentication of the new device and link it to the same account, * sharing the end-to-end encryption keys. * * @param tempClient temporary client used during the flow for the rendezvous channel * @param onFailure callback for when the sign-in process fails * @param abortSignal an AbortSignal that can be used to cancel the linking process, * for example when the user cancels out of the flow. * This will unbind the {@link onFailure} callback and prevent any further steps in the flow from being executed. * @returns a promise that resolves to an instance of the sign-in flow */ function _linkNewDeviceByGeneratingQR() { _linkNewDeviceByGeneratingQR = _asyncToGenerator(function* (client, onFailure, abortSignal) { // we assume rust crypto is already initialised return initGenerateQrFlow(RendezvousIntent.RECIPROCATE_LOGIN_ON_EXISTING_DEVICE, client, onFailure, abortSignal); }); return _linkNewDeviceByGeneratingQR.apply(this, arguments); } export function signInByGeneratingQR(_x5, _x6, _x7) { return _signInByGeneratingQR.apply(this, arguments); } function _signInByGeneratingQR() { _signInByGeneratingQR = _asyncToGenerator(function* (tempClient, onFailure, abortSignal) { // ensure rust crypto is initialized as needed for the secure channel var RustSdkCryptoJs = yield import("@matrix-org/matrix-sdk-crypto-wasm"); yield RustSdkCryptoJs.initAsync(); return initGenerateQrFlow(RendezvousIntent.LOGIN_ON_NEW_DEVICE, tempClient, onFailure, abortSignal); }); return _signInByGeneratingQR.apply(this, arguments); } function initGenerateQrFlow(_x8, _x9, _x0, _x1) { return _initGenerateQrFlow.apply(this, arguments); } function _initGenerateQrFlow() { _initGenerateQrFlow = _asyncToGenerator(function* (intent, client, onFailure, abortSignal) { var session = new MSC4108RendezvousSession({ onFailure, client }); var channel = new MSC4108SecureChannel(session, undefined, onFailure); var flow = new MSC4108SignInWithQR(channel, false, intent === RendezvousIntent.LOGIN_ON_NEW_DEVICE ? undefined : client, onFailure); if (abortSignal.aborted) return flow; abortSignal.onabort = () => { // Detach failure handlers session.onFailure = undefined; channel.onFailure = undefined; flow.onFailure = undefined; // Cancel the session flow.cancel(MSC4108FailureReason.UserCancelled); }; yield session.send(""); // open channel if (!abortSignal.aborted) { yield flow.generateCode(); } return flow; }); return _initGenerateQrFlow.apply(this, arguments); } //# sourceMappingURL=index.js.map