Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pre-filling the DSK and scanning DSK-only QRs #5309

Merged
merged 4 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/core/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,11 @@ export enum TransmitStatus {
OK = 0
}

// Warning: (ae-missing-release-tag) "tryParseDSKFromQRCodeString" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public
export function tryParseDSKFromQRCodeString(qr: string): string | undefined;

// Warning: (ae-missing-release-tag) "TXReport" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/security/QR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createHash } from "crypto";
import { Protocols } from "../capabilities/Protocols";
import { ZWaveError, ZWaveErrorCodes } from "../error/ZWaveError";
import { parseBitMask } from "../values/Primitive";
import { dskToString } from "./DSK";
import { dskToString, isValidDSK } from "./DSK";
import { SecurityClass } from "./SecurityClass";

function readNumber(qr: string, offset: number, length: number): number {
Expand Down Expand Up @@ -217,6 +217,21 @@ function parseTLV(qr: string): {
};
}

/**
* Tries to extract a DSK from a scanned QR code. If the string is a valid DSK (prefixed with "zws2dsk:" or unprefixed), the DSK will be returned.
* This can then be used to initiate an S2 inclusion with pre-filled DSK.
*/
export function tryParseDSKFromQRCodeString(qr: string): string | undefined {
// Trim off whitespace that might have been copied by accident
qr = qr.trim();
if (qr.startsWith("zws2dsk:")) {
qr = qr.slice("zws2dsk:".length);
}
if (isValidDSK(qr)) {
return qr;
}
}

/** Parses a string that has been decoded from a Z-Wave (S2 or SmartStart) QR code */
export function parseQRCodeString(qr: string): QRProvisioningInformation {
// Trim off whitespace that might have been copied by accident
Expand Down
1 change: 1 addition & 0 deletions packages/zwave-js/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ export type InclusionOptions = {
forceSecurity?: boolean;
} | {
strategy: InclusionStrategy.Security_S2;
dsk?: string;
userCallbacks?: InclusionUserCallbacks;
} | {
strategy: InclusionStrategy.Security_S2;
Expand Down
25 changes: 18 additions & 7 deletions packages/zwave-js/src/lib/controller/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
indexDBsByNode,
isRecoverableZWaveError,
isTransmissionError,
isValidDSK,
isZWaveError,
NodeType,
NODE_ID_BROADCAST,
Expand Down Expand Up @@ -2493,13 +2494,23 @@ supported CCs: ${nodeInfo.supportedCCs
const tai2RemainingMs =
inclusionTimeouts.TAI2 - (Date.now() - timerStartTAI2);

const pinResult = await Promise.race([
wait(tai2RemainingMs, true).then(() => false as const),
userCallbacks
.validateDSKAndEnterPIN(dsk)
// ignore errors in application callbacks
.catch(() => false as const),
]);
let pinResult: string | false;
if (
"dsk" in inclusionOptions &&
typeof inclusionOptions.dsk === "string" &&
isValidDSK(inclusionOptions.dsk)
) {
pinResult = inclusionOptions.dsk.slice(0, 5);
} else {
pinResult = await Promise.race([
wait(tai2RemainingMs, true).then(() => false as const),
userCallbacks
.validateDSKAndEnterPIN(dsk)
// ignore errors in application callbacks
.catch(() => false as const),
]);
}

if (
typeof pinResult !== "string" ||
!/^\d{5}$/.test(pinResult)
Expand Down
5 changes: 5 additions & 0 deletions packages/zwave-js/src/lib/controller/Inclusion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ export type InclusionOptions =
}
| {
strategy: InclusionStrategy.Security_S2;
/**
* Allows pre-filling the DSK, e.g. when a DSK-only QR code has been scanned.
* If this is given, the `validateDSKAndEnterPIN` callback will not be called.
*/
dsk?: string;
/**
* Allows overriding the user callbacks for this inclusion.
* If not given, the inclusion user callbacks of the driver options will be used.
Expand Down