SecretStorageVSCode拡張API

ユーザー設定をVSCodeに保存する方法はいくつかあります。バージョン1.53.0の到着前に、機密情報がメメントに格納されなければならなかったでオブジェクトworkspaceStateとglobalStateまたは、例えば、ショルダーキーボードまた、トークン付きのパスワードを標準の構成ファイルに保存したり、環境変数を使用したりすることは、このデータが他の拡張機能によって読み取られてキャッシュされる可能性があるため、最善の方法ではありませんでした。





この記事では、settings.json



からデータを読み取る方法について説明しますenvironment variables



次に、VSCodeSecretStorageからの値を持つキーの保存と提供を担当する最小限の機能を備えたクラスを作成します。





通常、プロジェクトに名前を付けますfancycolor



プロジェクトの初期化プロセスについては、VSCode Extensionsのドキュメント詳しく説明されているのですぐに始めましょう





settings.json

すべてのVSCode拡張子のすべての設定は共通のファイルに保存されるsettings.json



ため、どの拡張子からでもアクセスできます。たとえば、私たちのアプリケーションfancycolor



から、別の人気のあるアプリケーションの構成から、すべてのホストとそれに対応するプラットフォームのリストを簡単に読み取ることができますSSH - Remote







const configurationWorkspace = workspace.getConfiguration()
const sshRemotePlatform: string | undefined = configurationWorkspace.get(
  "remote.SSH.remotePlatform"
)
console.log(sshRemotePlatform)
      
      



このコードは、拡張機能の構成を一覧表示しますSSH - Remote







Proxy {ubuntu: 'linux', home: 'linux', raspberry: 'linux'}
      
      



環境変数

VSCode . .bashrc



Linux User.Environment



Windows process.env



.





/home/ubuntu/.env



ACCESS_TOKEN_ENV



.bashrc



.





echo 'export ACCESS_TOKEN_ENV="d8aba3b2-fda0-414a-b867-4798b7892bb4"' >> /home/ubuntu/.env
echo "source /home/ubuntu/.env" >> /home/ubuntu/.bashrc
      
      



Windows Powershell.





[System.Environment]::SetEnvironmentVariable('ACCESS_TOKEN_ENV', 'd8aba3b2-fda0-414a-b867-4798b7892bb4', [System.EnvironmentVariableTarget]::User)
      
      



VSCode fancycolor



extension.





import * as process from "process"
export const accessTokenEnv = process.env["ACCESS_TOKEN_ENV"]
console.log(accessTokenEnv)
      
      



.





d8aba3b2-fda0-414a-b867-4798b7892bb4
      
      



SecretStorage

, , VSCode. AuthSettings



, fancycolor_token



, :





  • init



    SecretStorage





  • getter instance







  • storeAuthData



    SecretStorage





  • getAuthData



    SecretStorage





import { ExtensionContext, SecretStorage } from "vscode"

export default class AuthSettings {
	private static _instance: AuthSettings

	constructor(private secretStorage: SecretStorage) {}

	static init(context: ExtensionContext): void {
		/*
		Create instance of new AuthSettings.
		*/
		AuthSettings._instance = new AuthSettings(context.secrets)
	}

	static get instance(): AuthSettings {
		/*
		Getter of our AuthSettings existing instance.
		*/
		return AuthSettings._instance
	}

	async storeAuthData(token?: string): Promise<void> {
		/*
		Update values in bugout_auth secret storage.
		*/
		if (token) {
			this.secretStorage.store("fancycolor_token", token)
		}
	}

	async getAuthData(): Promise<string | undefined> {
		/*
		Retrieve data from secret storage.
		*/
		return await this.secretStorage.get("fancycolor_token")
	}
}
      
      



extension.ts



書き込み機能が追加およびコマンドのパレットのコマンドを使用してトークンを取得することができます。





import * as vscode from "vscode"

import AuthSettings from "./settings"

export function activate(context: vscode.ExtensionContext) {
	// Initialize and get current instance of our Secret Storage
	AuthSettings.init(context)
	const settings = AuthSettings.instance
	
	// Register commands to save and retrieve token
	vscode.commands.registerCommand("fancycolor.setToken", async () => {
		const tokenInput = await vscode.window.showInputBox()
		await settings.storeAuthData(tokenInput)
	})
	vscode.commands.registerCommand("fancycolor.getToken", async () => {
		const tokenOutput = await settings.getAuthData()
		console.log(tokenOutput)
	})
}

export function deactivate() {}
      
      



package.json



コマンドfancycolor.setToken



登録するだけfancycolor.getToken



です。後で、VSCode SecretStorageを使用するときに、アプリケーション用に作成された特定のSecretStorageを排他的に参照できるようになります。これには、独自のが割り当てられます_id: 'undefined_publisher.fancycolor'










All Articles