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: automated-region-configuration (IDE-732) #549

Merged
merged 6 commits into from
Oct 25, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Snyk Security Changelog

## [2.20.0]
- If $/snyk.hasAuthenticated transmits an API URL, this is saved in the settings.

## [2.19.1]
- Adjust OSS panel font size.

Expand Down
8 changes: 6 additions & 2 deletions src/snyk/base/services/authenticationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface IAuthenticationService {

setToken(): Promise<void>;

updateToken(token: string): Promise<void>;
updateTokenAndEndpoint(token: string, apiUrl: string): Promise<void>;
}

export type OAuthToken = {
Expand Down Expand Up @@ -86,12 +86,16 @@ export class AuthenticationService implements IAuthenticationService {
return `${token.slice(0, 4)}****${token.slice(-4)}`;
}

async updateToken(token: string): Promise<void> {
async updateTokenAndEndpoint(token: string, apiUrl: string): Promise<void> {
if (!token) {
await this.initiateLogout();
} else {
if (!this.validateToken(token)) return Promise.reject(new Error('The entered token has an invalid format.'));

acke marked this conversation as resolved.
Show resolved Hide resolved
if (apiUrl !== null && apiUrl !== undefined && apiUrl.trim().length > 0) {
await this.configuration.setEndpoint(apiUrl);
}

await this.configuration.setToken(token);
await this.contextService.setContext(SNYK_CONTEXT.AUTHENTICATING, false);
await this.contextService.setContext(SNYK_CONTEXT.LOGGEDIN, true);
Expand Down
4 changes: 2 additions & 2 deletions src/snyk/common/languageServer/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ export class LanguageServer implements ILanguageServer {
}

private registerListeners(client: LanguageClient): void {
client.onNotification(SNYK_HAS_AUTHENTICATED, ({ token }: { token: string }) => {
this.authenticationService.updateToken(token).catch((error: Error) => {
client.onNotification(SNYK_HAS_AUTHENTICATED, ({ token, apiUrl }: { token: string; apiUrl: string }) => {
this.authenticationService.updateTokenAndEndpoint(token, apiUrl).catch((error: Error) => {
ErrorHandler.handle(error, this.logger, error.message);
});
});
Expand Down
33 changes: 23 additions & 10 deletions src/test/unit/base/services/authenticationService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ suite('AuthenticationService', () => {
let languageClientAdapter: ILanguageClientAdapter;
let languageClientSendNotification: sinon.SinonSpy;
let setContextSpy: sinon.SinonSpy;
let setEndpointSpy: sinon.SinonSpy;
let setTokenSpy: sinon.SinonSpy;
let clearTokenSpy: sinon.SinonSpy;
let previewFeaturesSpy: sinon.SinonSpy;
Expand All @@ -38,6 +39,7 @@ suite('AuthenticationService', () => {
setup(() => {
baseModule = {} as IBaseSnykModule;
setContextSpy = sinon.fake();
setEndpointSpy = sinon.fake();
setTokenSpy = sinon.fake();
clearTokenSpy = sinon.fake();
languageClientSendNotification = sinon.fake();
Expand All @@ -57,6 +59,7 @@ suite('AuthenticationService', () => {

config = {
authHost: '',
setEndpoint: setEndpointSpy,
setToken: setTokenSpy,
clearToken: clearTokenSpy,
getPreviewFeatures: previewFeaturesSpy,
Expand Down Expand Up @@ -102,7 +105,7 @@ suite('AuthenticationService', () => {
sinon.assert.calledOnceWithExactly(languageClientSendNotification, DID_CHANGE_CONFIGURATION_METHOD, {});
});

suite('.updateToken()', () => {
suite('.updateTokenAndEndpoint()', () => {
let service: AuthenticationService;
const setLoadingBadgeFake = sinon.fake();

Expand All @@ -126,39 +129,44 @@ suite('AuthenticationService', () => {
);
});

test('sets the token when a valid token is provided', async () => {
test('sets the token and endpoint when a valid token is provided', async () => {
const token = 'be30e2dd-95ac-4450-ad90-5f7cc7429258';
await service.updateToken(token);
const apiUrl = 'https://api.snyk.io';
await service.updateTokenAndEndpoint(token, apiUrl);

sinon.assert.calledWith(setEndpointSpy, apiUrl);
sinon.assert.calledWith(setTokenSpy, token);
});

test('logs out if token is empty', async () => {
await service.updateToken('');
await service.updateTokenAndEndpoint('', '');

sinon.assert.called(clearTokenSpy);
sinon.assert.calledWith(setContextSpy, SNYK_CONTEXT.LOGGEDIN, false);
});

test('sets the proper contexts when setting new token', async () => {
const token = 'be30e2dd-95ac-4450-ad90-5f7cc7429258';
await service.updateToken(token);
const apiUrl = 'https://api.snyk.io';
await service.updateTokenAndEndpoint(token, apiUrl);

sinon.assert.calledWith(setContextSpy, SNYK_CONTEXT.LOGGEDIN, true);
sinon.assert.calledWith(setContextSpy, SNYK_CONTEXT.AUTHENTICATING, false);
});

test('sets the loading badge status when setting new token', async () => {
const token = 'be30e2dd-95ac-4450-ad90-5f7cc7429258';
await service.updateToken(token);
const apiUrl = 'https://api.snyk.io';
await service.updateTokenAndEndpoint(token, apiUrl);

sinon.assert.calledWith(setLoadingBadgeFake, false);
});

test('errors when invalid token is provided', async () => {
const invalidToken = 'thisTokenIsNotValid';
const apiUrl = 'https://api.snyk.io';

await rejects(service.updateToken(invalidToken));
await rejects(service.updateTokenAndEndpoint(invalidToken, apiUrl));
sinon.assert.notCalled(setTokenSpy);
});

Expand All @@ -171,15 +179,19 @@ suite('AuthenticationService', () => {
refresh_token: 'refresh_token',
};
const oauthTokenString = JSON.stringify(oauthToken);
const apiUrl = 'https://api.snyk.io';

await service.updateToken(oauthTokenString);
await service.updateTokenAndEndpoint(oauthTokenString, apiUrl);
sinon.assert.calledWith(setEndpointSpy, apiUrl);
sinon.assert.calledWith(setTokenSpy, oauthTokenString);
});

test('fails with error on non oauth token json string', async () => {
const oauthTokenString = '{}';
const apiUrl = 'https://api.snyk.io';

await rejects(service.updateTokenAndEndpoint(oauthTokenString, apiUrl));

await rejects(service.updateToken(oauthTokenString));
sinon.assert.notCalled(setTokenSpy);
});

Expand All @@ -192,8 +204,9 @@ suite('AuthenticationService', () => {
refresh_token: 'refresh_token',
};
const oauthTokenString = JSON.stringify(oauthToken);
const apiUrl = 'https://api.snyk.io';

await rejects(service.updateToken(oauthTokenString));
await rejects(service.updateTokenAndEndpoint(oauthTokenString, apiUrl));
sinon.assert.notCalled(setTokenSpy);
});
});
Expand Down
Loading