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: replace keyar with vscode SecretStorage #1600

Merged
merged 6 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "2.16.0",
"publisher": "johnstoncode",
"engines": {
"vscode": "^1.44.0"
"vscode": "^1.53.0"
},
"private": true,
"enableProposedApi": false,
Expand Down Expand Up @@ -61,7 +61,7 @@
"@types/node": "^12.11.7",
"@types/semver": "^7.3.8",
"@types/tmp": "0.2.1",
"@types/vscode": "1.44.0",
"@types/vscode": "1.53.0",
"@types/xml2js": "^0.4.9",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "^4.29.0",
Expand Down
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { tempSvnFs } from "./temp_svn_fs";
import { SvnFileSystemProvider } from "./svnFileSystemProvider";

async function init(
_context: ExtensionContext,
extensionContext: ExtensionContext,
outputChannel: OutputChannel,
disposables: Disposable[]
) {
Expand All @@ -38,7 +38,8 @@ async function init(
const svn = new Svn({ svnPath: info.path, version: info.version });
const sourceControlManager = await new SourceControlManager(
svn,
ConstructorPolicy.Async
ConstructorPolicy.Async,
extensionContext
);

registerCommands(sourceControlManager, disposables);
Expand Down
6 changes: 4 additions & 2 deletions src/historyView/branchChangesProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ export class BranchChangesProvider
),
commands.registerCommand(
"svn.branchchanges.refresh",
() => this._onDidChangeTreeData.fire(),
() => this._onDidChangeTreeData.fire(undefined),
this
),
this.model.onDidChangeRepository(() => this._onDidChangeTreeData.fire())
this.model.onDidChangeRepository(() =>
this._onDidChangeTreeData.fire(undefined)
)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/historyView/repoLogProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export class RepoLogProvider
return;
}
this.logCache.set(repoName, item);
this._onDidChangeTreeData.fire();
this._onDidChangeTreeData.fire(undefined);
}

public addRepolikeGui() {
Expand Down
41 changes: 28 additions & 13 deletions src/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
EventEmitter,
ProgressLocation,
scm,
SecretStorage,
SourceControl,
SourceControlInputBox,
TextDocument,
Expand Down Expand Up @@ -53,7 +54,6 @@ import {
} from "./util";
import { match, matchAll } from "./util/globMatch";
import { RepositoryFilesWatcher } from "./watchers/repositoryFilesWatcher";
import { keytar } from "./vscodeModules";

function shouldShowProgress(operation: Operation): boolean {
switch (operation) {
Expand Down Expand Up @@ -184,7 +184,10 @@ export class Repository implements IRemoteRepository {
this.repository.password = password;
}

constructor(public repository: BaseRepository) {
constructor(
public repository: BaseRepository,
private secrets: SecretStorage
) {
this._fsWatcher = new RepositoryFilesWatcher(repository.root);
this.disposables.push(this._fsWatcher);

Expand Down Expand Up @@ -1007,24 +1010,36 @@ export class Repository implements IRemoteRepository {
await this.lastPromptAuth;
}

try {
return keytar.findCredentials(this.getCredentialServiceName());
} catch (error) {
const secret = await this.secrets.get(this.getCredentialServiceName());

if (typeof secret === "undefined") {
return [];
}

const credentials = JSON.parse(secret) as Array<IStoredAuth>;

return credentials;
}

public async saveAuth(): Promise<void> {
if (this.canSaveAuth && this.username && this.password) {
try {
await keytar.setPassword(
this.getCredentialServiceName(),
this.username,
this.password
);
} catch (error) {
console.log(error);
const secret = await this.secrets.get(this.getCredentialServiceName());
let credentials: Array<IStoredAuth> = [];

if (typeof secret === "string") {
credentials = JSON.parse(secret) as Array<IStoredAuth>;
}

credentials.push({
account: this.username,
password: this.password
});

await this.secrets.store(
this.getCredentialServiceName(),
JSON.stringify(credentials)
);

this.canSaveAuth = false;
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/source_control_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Disposable,
Event,
EventEmitter,
ExtensionContext,
Uri,
window,
workspace,
Expand Down Expand Up @@ -94,7 +95,11 @@ export class SourceControlManager implements IDisposable {
return this._svn;
}

constructor(private _svn: Svn, policy: ConstructorPolicy) {
constructor(
private _svn: Svn,
policy: ConstructorPolicy,
private extensionContact: ExtensionContext
) {
if (policy !== ConstructorPolicy.Async) {
throw new Error("Unsopported policy");
}
Expand Down Expand Up @@ -288,7 +293,8 @@ export class SourceControlManager implements IDisposable {
const repositoryRoot = await this.svn.getRepositoryRoot(path);

const repository = new Repository(
await this.svn.open(repositoryRoot, path)
await this.svn.open(repositoryRoot, path),
this.extensionContact.secrets
);

this.open(repository);
Expand Down
2 changes: 1 addition & 1 deletion src/treeView/dataProviders/svnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class SvnProvider
}

public refresh(): void {
this._onDidChangeTreeData.fire();
this._onDidChangeTreeData.fire(undefined);
}

public getTreeItem(element: RepositoryNode): TreeItem {
Expand Down
65 changes: 0 additions & 65 deletions src/types/keytar.d.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/vscodeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ function getNodeModule<T>(moduleName: string, showError = true): T | undefined {
return undefined;
}

export const keytar = getNodeModule("keytar", false) as typeof import("keytar");

let iconv_lite = getNodeModule(
"iconv-lite-umd",
false
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.2.1.tgz#83ecf4ec22a8c218c71db25f316619fe5b986011"
integrity sha512-7cTXwKP/HLOPVgjg+YhBdQ7bMiobGMuoBmrGmqwIWJv8elC6t1DfVc/mn4fD9UE1IjhwmhaQ5pGVXkmXbH0rhg==

"@types/vscode@1.44.0":
version "1.44.0"
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.44.0.tgz#62ecfe3d0e38942fce556574da54ee1013c775b7"
integrity sha512-WJZtZlinE3meRdH+I7wTsIhpz/GLhqEQwmPGeh4s1irWLwMzCeTV8WZ+pgPTwrDXoafVUWwo1LiZ9HJVHFlJSQ==
"@types/vscode@1.53.0":
version "1.53.0"
resolved "https://registry.yarnpkg.com/@types/vscode/-/vscode-1.53.0.tgz#47b53717af6562f2ad05171bc9c8500824a3905c"
integrity sha512-XjFWbSPOM0EKIT2XhhYm3D3cx3nn3lshMUcWNy1eqefk+oqRuBq8unVb6BYIZqXy9lQZyeUl7eaBCOZWv+LcXQ==

"@types/xml2js@^0.4.9":
version "0.4.9"
Expand Down