Skip to content

Commit bc4212b

Browse files
authored
refactor(adb): refactor daemon authentication process (#810)
1 parent 7df9ca2 commit bc4212b

File tree

23 files changed

+1119
-697
lines changed

23 files changed

+1119
-697
lines changed

libraries/adb-credential-nodejs/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export namespace TangoNodeStorage {
225225

226226
// Re-export everything except Web-only storages
227227
export {
228-
AdbWebCryptoCredentialStore,
228+
AdbWebCryptoCredentialManager,
229229
TangoPasswordProtectedStorage,
230230
TangoPrfStorage,
231231
} from "@yume-chan/adb-credential-web";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1+
export * from "./manager.js";
12
export * from "./storage/index.js";
2-
export * from "./store.js";

libraries/adb-credential-web/src/store.ts renamed to libraries/adb-credential-web/src/manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type {
2-
AdbCredentialStore,
2+
AdbCredentialManager,
33
AdbPrivateKey,
44
MaybeError,
55
} from "@yume-chan/adb";
66
import { rsaParsePrivateKey } from "@yume-chan/adb";
77

88
import type { TangoKeyStorage } from "./storage/index.js";
99

10-
export class AdbWebCryptoCredentialStore implements AdbCredentialStore {
10+
export class AdbWebCryptoCredentialManager implements AdbCredentialManager {
1111
readonly #storage: TangoKeyStorage;
1212

1313
readonly #name: string | undefined;

libraries/adb-credential-web/src/storage/password.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ async function deriveAesKey(password: string, salt?: Uint8Array<ArrayBuffer>) {
5555
}
5656

5757
class PasswordIncorrectError extends Error {
58-
constructor() {
58+
#keyName: string | undefined;
59+
get keyName() {
60+
return this.#keyName;
61+
}
62+
63+
constructor(keyName: string | undefined) {
5964
super("Password incorrect");
65+
this.#keyName = keyName;
6066
}
6167
}
6268

@@ -78,7 +84,7 @@ export class TangoPasswordProtectedStorage implements TangoKeyStorage {
7884
privateKey: Uint8Array<ArrayBuffer>,
7985
name: string | undefined,
8086
): Promise<undefined> {
81-
const password = await this.#requestPassword("save");
87+
const password = await this.#requestPassword("save", name);
8288
const { salt, aesKey } = await deriveAesKey(password);
8389

8490
const iv = new Uint8Array(AesIvLength);
@@ -101,7 +107,7 @@ export class TangoPasswordProtectedStorage implements TangoKeyStorage {
101107
// Clear secret memory
102108
// * No way to clear `password` and `aesKey`
103109
// * `salt`, `iv`, `encrypted` and `bundle` are not secrets
104-
// * `data` is owned by caller and will be cleared by caller
110+
// * `privateKey` is owned by caller and will be cleared by caller
105111
}
106112

107113
async *load(): AsyncGenerator<MaybeError<TangoKey>, void, void> {
@@ -118,7 +124,7 @@ export class TangoPasswordProtectedStorage implements TangoKeyStorage {
118124
new Uint8ArrayExactReadable(serialized),
119125
);
120126

121-
const password = await this.#requestPassword("load");
127+
const password = await this.#requestPassword("load", name);
122128
const { aesKey } = await deriveAesKey(
123129
password,
124130
bundle.pbkdf2Salt as Uint8Array<ArrayBuffer>,
@@ -147,7 +153,7 @@ export class TangoPasswordProtectedStorage implements TangoKeyStorage {
147153
}
148154
} catch (e) {
149155
if (e instanceof DOMException && e.name === "OperationError") {
150-
yield new PasswordIncorrectError();
156+
yield new PasswordIncorrectError(name);
151157
continue;
152158
}
153159

@@ -162,6 +168,7 @@ export class TangoPasswordProtectedStorage implements TangoKeyStorage {
162168
export namespace TangoPasswordProtectedStorage {
163169
export type RequestPassword = (
164170
reason: "save" | "load",
171+
name: string | undefined,
165172
) => MaybePromiseLike<string>;
166173

167174
export type PasswordIncorrectError = typeof PasswordIncorrectError;

libraries/adb/src/commands/sync/socket.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ export class AdbSyncSocketLocked implements AsyncExactReadable {
4141
}
4242

4343
async flush() {
44-
try {
45-
await this.#writeLock.wait();
46-
const buffer = this.#combiner.flush();
47-
if (buffer) {
44+
const buffer = this.#combiner.flush();
45+
if (buffer) {
46+
try {
47+
await this.#writeLock.wait();
4848
await this.#write(buffer);
49+
} finally {
50+
this.#writeLock.notifyOne();
4951
}
50-
} finally {
51-
this.#writeLock.notifyOne();
5252
}
5353
}
5454

libraries/adb/src/commands/sync/sync.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ export class AdbSync {
4242
protected _adb: Adb;
4343
protected _socket: AdbSyncSocket;
4444

45-
readonly #supportsStat: boolean;
45+
readonly #supportsStat2: boolean;
4646
readonly #supportsLs2: boolean;
4747
readonly #fixedPushMkdir: boolean;
4848
readonly #supportsSendReceive2: boolean;
4949
readonly #needPushMkdirWorkaround: boolean;
5050

51-
get supportsStat(): boolean {
52-
return this.#supportsStat;
51+
get supportsStat2(): boolean {
52+
return this.#supportsStat2;
5353
}
5454

5555
get supportsLs2(): boolean {
@@ -72,7 +72,7 @@ export class AdbSync {
7272
this._adb = adb;
7373
this._socket = new AdbSyncSocket(socket, adb.maxPayloadSize);
7474

75-
this.#supportsStat = adb.canUseFeature(AdbFeature.Stat2);
75+
this.#supportsStat2 = adb.canUseFeature(AdbFeature.Stat2);
7676
this.#supportsLs2 = adb.canUseFeature(AdbFeature.Ls2);
7777
this.#fixedPushMkdir = adb.canUseFeature(AdbFeature.FixedPushMkdir);
7878
this.#supportsSendReceive2 = adb.canUseFeature(AdbFeature.SendReceive2);
@@ -87,7 +87,7 @@ export class AdbSync {
8787
* If `path` points to a symbolic link, the returned information is about the link itself (with `type` being `LinuxFileType.Link`).
8888
*/
8989
async lstat(path: string): Promise<AdbSyncStat> {
90-
return await adbSyncLstat(this._socket, path, this.#supportsStat);
90+
return await adbSyncLstat(this._socket, path, this.#supportsStat2);
9191
}
9292

9393
/**
@@ -96,7 +96,7 @@ export class AdbSync {
9696
* If `path` points to a symbolic link, it will be resolved and the returned information is about the target (with `type` being `LinuxFileType.File` or `LinuxFileType.Directory`).
9797
*/
9898
async stat(path: string) {
99-
if (!this.#supportsStat) {
99+
if (!this.#supportsStat2) {
100100
throw new Error("Not supported");
101101
}
102102

libraries/adb/src/daemon/auth.ts

Lines changed: 0 additions & 208 deletions
This file was deleted.

0 commit comments

Comments
 (0)