Skip to content

Commit 28354f1

Browse files
replaced keytar with vscode API (#197)
1 parent 1df424e commit 28354f1

File tree

5 files changed

+30
-45
lines changed

5 files changed

+30
-45
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Change Log
22

3-
## [1.6.9] 2023-09-i7
3+
## [1.6.10] 2023-10-10
4+
5+
### Fixed
6+
7+
- replaced keytar with vscode secure storage
8+
9+
## [1.6.9] 2023-09-17
410

511
### Added
612

client/src/config.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class RemoteManager {
177177
private vault: PasswordVault
178178

179179
private constructor() {
180-
this.vault = new PasswordVault()
180+
this.vault = PasswordVault.get()
181181
workspace.onDidChangeConfiguration(this.configChanged, this)
182182
}
183183
private configChanged({ affectsConfiguration }: ConfigurationChangeEvent) {
@@ -285,11 +285,12 @@ export class RemoteManager {
285285
return result
286286
}
287287

288-
public clearPassword(connectionId: string, userName: string) {
289-
return this.vault.deletePassword(
288+
public async clearPassword(connectionId: string, userName: string) {
289+
await this.vault.deletePassword(
290290
`vscode.abapfs.${formatKey(connectionId)}`,
291291
userName
292292
)
293+
return true
293294
}
294295

295296
public async getPassword(connectionId: string, userName: string) {

client/src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
documentWillSave,
1717
restoreLocks
1818
} from "./listeners"
19-
import { log } from "./lib"
19+
import { PasswordVault, log } from "./lib"
2020
import { LanguageCommands } from "./langClient"
2121
import { registerRevisionModel, AbapRevisionLens } from "./scm/abaprevisions"
2222
import { ClassHierarchyLensProvider } from "./adt/classhierarchy"
@@ -39,6 +39,7 @@ export async function activate(ctx: ExtensionContext): Promise<AbapFsApi> {
3939
context = ctx
4040
const startTime = new Date().getTime()
4141
log("activating ABAPfs...")
42+
new PasswordVault(ctx)
4243
loadTokens()
4344
clearTokens()
4445
const sub = context.subscriptions

client/src/lib/externalmodules.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,26 @@
1-
// keytar depends on a native module shipped in vscode
2-
// this loads only the type definitions
3-
import * as keytarType from "./keytar"
4-
import * as winRegistryType from "vscode-windows-registry";
5-
6-
// get the module from vscode. This is not an official API, might break at some point
7-
// this is required because keytar includes a binary we can't include
8-
// see https://github.com/microsoft/vscode/issues/68738
9-
function getCodeModule<T>(moduleName: string): T | undefined {
10-
// adapted from https://github.com/Microsoft/vscode-pull-request-github/blob/master/src/authentication/keychain.ts
11-
// I guess we use eval to load the embedded module at runtime
12-
// rather than allowing webpack to bundle it
13-
// tslint:disable-next-line: no-eval
14-
const vscodeRequire = eval("require")
15-
try {
16-
return vscodeRequire(moduleName)
17-
} catch (err) {
18-
return undefined
19-
}
20-
}
21-
22-
let keytar: typeof keytarType | undefined
23-
const keytarErr = () => new Error("Error accessing system secure store")
24-
1+
import { ExtensionContext } from "vscode"
252
export class PasswordVault {
26-
constructor() {
27-
if (!keytar) keytar = getCodeModule<typeof keytarType>("keytar")
3+
private static instance: PasswordVault
4+
constructor(private context: ExtensionContext) {
5+
PasswordVault.instance = this
286
}
297

308
getPassword(service: string, account: string) {
31-
if (!keytar) throw keytarErr()
32-
return keytar.getPassword(service, account)
9+
return this.context.secrets.get(`${service}:${account}`)
3310
}
3411

3512
setPassword(service: string, account: string, password: string) {
36-
if (!keytar) throw keytarErr()
37-
return keytar.setPassword(service, account, password)
13+
return this.context.secrets.store(`${service}:${account}`, password)
3814
}
3915

4016
deletePassword(service: string, account: string) {
41-
if (!keytar) throw keytarErr()
42-
return keytar.deletePassword(service, account)
17+
return this.context.secrets.delete(`${service}:${account}`)
4318
}
44-
45-
acounts(service: string) {
46-
if (!keytar) throw keytarErr()
47-
return keytar.findCredentials(service)
19+
async accounts(service: string): Promise<{ account: string, password: string }[]> {
20+
return [] //TODO:implement or remove
21+
}
22+
static get() {
23+
if (!PasswordVault.instance) throw new Error("No password vault defined")
24+
return PasswordVault.instance
4825
}
4926
}

client/src/scm/abapGit/credentials.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const mandInbox = (prompt: string, password = false) =>
4040

4141
export async function repoCredentials(repoUrl: string) {
4242
const cred: ScmCredentials = { user: getDefaultUser(repoUrl), password: "" }
43-
const vault = new PasswordVault()
43+
const vault = PasswordVault.get()
4444
const pwdFromVault = async (x: ScmCredentials) => {
4545
if (x.user)
4646
x.password = (await vault.getPassword(pwdService(repoUrl), x.user)) || ""
@@ -81,11 +81,11 @@ export async function dataCredentials(
8181
}
8282

8383
export const deletePassword = (repo: GitRepo, user: string) => {
84-
const vault = new PasswordVault()
84+
const vault = PasswordVault.get()
8585
return vault.deletePassword(pwdService(repo.url), user)
8686
}
8787

8888
export const listPasswords = (repo: GitRepo) => {
89-
const vault = new PasswordVault()
90-
return vault.acounts(pwdService(repo.url))
89+
const vault = PasswordVault.get()
90+
return vault.accounts(pwdService(repo.url))
9191
}

0 commit comments

Comments
 (0)