Update API key validation and handling

- Modified the API key validation logic to check for non-empty strings before validating.
- Updated the API key prefix from 'sk.' to 'sk-' in the ApiKeyManager and corresponding tests.
- Adjusted the test cases to reflect the new API key format.
This commit is contained in:
bobo.yang 2023-08-03 10:58:18 +08:00
parent e9910019e9
commit bef66fb80e
3 changed files with 14 additions and 14 deletions

View File

@ -74,8 +74,8 @@ export function registerApiKeySettingCommand(context: vscode.ExtensionContext) {
placeHolder: "Set OPENAI_API_KEY (or DevChat Access Key)" placeHolder: "Set OPENAI_API_KEY (or DevChat Access Key)"
}) ?? ''; }) ?? '';
if (!isValidApiKey(passwordInput)) { if (passwordInput.trim() !== "" && !isValidApiKey(passwordInput)) {
UiUtilWrapper.showErrorMessage("You access key is invalid!"); UiUtilWrapper.showErrorMessage("Your access key is invalid!");
return ; return ;
} }
ApiKeyManager.writeApiKeySecret(passwordInput); ApiKeyManager.writeApiKeySecret(passwordInput);

View File

@ -15,7 +15,7 @@ export class ApiKeyManager {
} }
static getKeyType(apiKey: string): string | undefined { static getKeyType(apiKey: string): string | undefined {
if (apiKey.startsWith("sk.")) { if (apiKey.startsWith("sk-")) {
return "sk"; return "sk";
} else if (apiKey.startsWith("DC.")) { } else if (apiKey.startsWith("DC.")) {
return "DC"; return "DC";

View File

@ -14,28 +14,28 @@ describe('ApiKeyManager', () => {
describe('getApiKey', () => { describe('getApiKey', () => {
it('should return the secret storage API key', async () => { it('should return the secret storage API key', async () => {
sinon.stub(UiUtilWrapper, 'secretStorageGet').resolves('sk.secret'); sinon.stub(UiUtilWrapper, 'secretStorageGet').resolves('sk-secret');
sinon.stub(UiUtilWrapper, 'getConfiguration').returns(undefined); sinon.stub(UiUtilWrapper, 'getConfiguration').returns(undefined);
const apiKey = await ApiKeyManager.getApiKey(); const apiKey = await ApiKeyManager.getApiKey();
expect(apiKey).to.equal('sk.secret'); expect(apiKey).to.equal('sk-secret');
}); });
it('should return the configuration API key', async () => { it('should return the configuration API key', async () => {
sinon.stub(UiUtilWrapper, 'secretStorageGet').resolves(undefined); sinon.stub(UiUtilWrapper, 'secretStorageGet').resolves(undefined);
sinon.stub(UiUtilWrapper, 'getConfiguration').returns('sk.config'); sinon.stub(UiUtilWrapper, 'getConfiguration').returns('sk-config');
const apiKey = await ApiKeyManager.getApiKey(); const apiKey = await ApiKeyManager.getApiKey();
expect(apiKey).to.equal('sk.config'); expect(apiKey).to.equal('sk-config');
}); });
it('should return the environment variable API key', async () => { it('should return the environment variable API key', async () => {
sinon.stub(UiUtilWrapper, 'secretStorageGet').resolves(undefined); sinon.stub(UiUtilWrapper, 'secretStorageGet').resolves(undefined);
sinon.stub(UiUtilWrapper, 'getConfiguration').returns(undefined); sinon.stub(UiUtilWrapper, 'getConfiguration').returns(undefined);
process.env.OPENAI_API_KEY = 'sk.env'; process.env.OPENAI_API_KEY = 'sk-env';
const apiKey = await ApiKeyManager.getApiKey(); const apiKey = await ApiKeyManager.getApiKey();
expect(apiKey).to.equal('sk.env'); expect(apiKey).to.equal('sk-env');
}); });
}); });
@ -43,7 +43,7 @@ describe('ApiKeyManager', () => {
it('should return the configuration endpoint', () => { it('should return the configuration endpoint', () => {
sinon.stub(UiUtilWrapper, 'getConfiguration').returns('https://config-endpoint.com'); sinon.stub(UiUtilWrapper, 'getConfiguration').returns('https://config-endpoint.com');
const endPoint = ApiKeyManager.getEndPoint('sk.key'); const endPoint = ApiKeyManager.getEndPoint('sk-key');
expect(endPoint).to.equal('https://config-endpoint.com'); expect(endPoint).to.equal('https://config-endpoint.com');
}); });
@ -51,7 +51,7 @@ describe('ApiKeyManager', () => {
sinon.stub(UiUtilWrapper, 'getConfiguration').returns(undefined); sinon.stub(UiUtilWrapper, 'getConfiguration').returns(undefined);
process.env.OPENAI_API_BASE = 'https://env-endpoint.com'; process.env.OPENAI_API_BASE = 'https://env-endpoint.com';
const endPoint = ApiKeyManager.getEndPoint('sk.key'); const endPoint = ApiKeyManager.getEndPoint('sk-key');
expect(endPoint).to.equal('https://env-endpoint.com'); expect(endPoint).to.equal('https://env-endpoint.com');
}); });
@ -65,7 +65,7 @@ describe('ApiKeyManager', () => {
describe('getKeyType', () => { describe('getKeyType', () => {
it('should return "sk" for sk keys', () => { it('should return "sk" for sk keys', () => {
const keyType = ApiKeyManager.getKeyType('sk.key'); const keyType = ApiKeyManager.getKeyType('sk-key');
expect(keyType).to.equal('sk'); expect(keyType).to.equal('sk');
}); });
@ -84,8 +84,8 @@ describe('ApiKeyManager', () => {
it('should store the API key in secret storage', async () => { it('should store the API key in secret storage', async () => {
const storeSecretStub = sinon.stub(UiUtilWrapper, 'storeSecret').resolves(); const storeSecretStub = sinon.stub(UiUtilWrapper, 'storeSecret').resolves();
await ApiKeyManager.writeApiKeySecret('sk.secret'); await ApiKeyManager.writeApiKeySecret('sk-secret');
expect(storeSecretStub.calledWith('devchat_OPENAI_API_KEY', 'sk.secret')).to.be.true; expect(storeSecretStub.calledWith('devchat_OPENAI_API_KEY', 'sk-secret')).to.be.true;
}); });
}); });
}); });