Skip to content

Commit

Permalink
fix: token error (#216)
Browse files Browse the repository at this point in the history
* fix: token retrieval error handling

* fix: release stable
  • Loading branch information
attriaayush authored Apr 29, 2022
1 parent 2449c68 commit 3ac2cfc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release-stable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ jobs:
environment: Stable
name: Release
steps:
- name: Fetch sources
uses: actions/checkout@v2

- name: Setup VSCE
run: sudo npm install -g vsce@latest

Expand Down
25 changes: 18 additions & 7 deletions src/snyk/common/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,16 @@ export class Configuration implements IConfiguration {
}

async getToken(): Promise<string | undefined> {
try {
return SecretStorageAdapter.instance.get(SNYK_TOKEN_KEY);
} catch (e) {
// if the token cannot be parsed then clear the token
await this.clearToken();
}
return new Promise(resolve => {
SecretStorageAdapter.instance
.get(SNYK_TOKEN_KEY)
.then(token => resolve(token))
.catch(async _ => {
// clear the token and return empty string
await this.clearToken();
resolve('');
});
});
}

get snykCodeToken(): Promise<string | undefined> {
Expand All @@ -188,7 +192,14 @@ export class Configuration implements IConfiguration {
}

async clearToken(): Promise<void> {
return await SecretStorageAdapter.instance.delete(SNYK_TOKEN_KEY);
return new Promise<void>((resolve, reject) => {
SecretStorageAdapter.instance
.delete(SNYK_TOKEN_KEY)
.then(() => resolve())
.catch(error => {
reject(error);
});
});
}

static get source(): string {
Expand Down
16 changes: 16 additions & 0 deletions src/test/unit/common/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ suite('Configuration', () => {
secrets: {
store: (_key: string, _value: string) => Promise.resolve(),
get: () => Promise.resolve(),
delete: () => Promise.resolve(),
},
} as unknown as ExtensionContext;
SecretStorageAdapter.init(extensionContext);
Expand Down Expand Up @@ -124,6 +125,21 @@ suite('Configuration', () => {
strictEqual(secretStorageGetStub.calledOnce, true);
});

test('Snyk Code: token should be cleared if the retrieval method throws', async () => {
const token = 'snyk-token';

sinon.stub(extensionContext.secrets, 'store').resolves();
const secretStorageDeleteStub = sinon.stub(extensionContext.secrets, 'delete').resolves();
const secretStorageGetStub = sinon.stub(extensionContext.secrets, 'get').rejects('cannot get token');

const configuration = new Configuration(process.env, workspaceStub);
await configuration.setToken(token);

strictEqual(await configuration.snykCodeToken, '');
strictEqual(secretStorageGetStub.calledOnce, true);
strictEqual(secretStorageDeleteStub.calledOnce, true);
});

test('Snyk Code: token returns Snyk Code token when in development', async () => {
const token = 'test-token';
const snykCodeToken = 'snykCode-token';
Expand Down

0 comments on commit 3ac2cfc

Please sign in to comment.