Skip to content

Commit bb7dc69

Browse files
committed
Load new scopes if available
1 parent a21b9aa commit bb7dc69

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

src/services/sessionService.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { GitpodPublicApi, IGitpodAPI } from '../publicApi';
1010
import { eventToPromise } from '../common/event';
1111
import { ILogService } from './logService';
1212
import { ITelemetryService } from '../common/telemetry';
13+
import { arrayEquals } from '../common/utils';
1314

1415
export class NoSignedInError extends Error {
1516
constructor() {
@@ -31,7 +32,16 @@ export interface ISessionService {
3132
didFirstLoad: Promise<void>;
3233
}
3334

34-
const sessionScopes = [
35+
const defaultSessionScopes = [
36+
'function:getWorkspace',
37+
'function:getOwnerToken',
38+
'function:getLoggedInUser',
39+
'function:getSSHPublicKeys',
40+
'function:sendHeartBeat',
41+
'resource:default'
42+
];
43+
44+
const managementSessionScopes = [
3545
'function:getWorkspaces',
3646
'function:getWorkspace',
3747
'function:startWorkspace',
@@ -67,8 +77,8 @@ export class SessionService extends Disposable implements ISessionService {
6777
super();
6878

6979
this._register(vscode.authentication.onDidChangeSessions(e => this.handleOnDidChangeSessions(e)));
70-
this.firstLoadPromise = this.tryLoadSession(false);
71-
this.firstLoadPromise.then(() => vscode.commands.executeCommand('setContext', 'gitpod.authenticated', this.isSignedIn()));
80+
this.firstLoadPromise = this.tryLoadSession(false, managementSessionScopes).then(() => this.tryLoadSession(false, defaultSessionScopes) /* delete this after some time when users have a session with managementSessionScopes */);
81+
this.firstLoadPromise.then(() => vscode.commands.executeCommand('setContext', 'gitpod.authenticated', this.session && arrayEquals(this.session.scopes.slice(0).sort(), managementSessionScopes.slice().sort())));
7282
}
7383

7484
private async handleOnDidChangeSessions(e: vscode.AuthenticationSessionsChangeEvent) {
@@ -77,8 +87,9 @@ export class SessionService extends Disposable implements ISessionService {
7787
}
7888
const oldSession = this.session;
7989
this.session = undefined as vscode.AuthenticationSession | undefined;
80-
await this.tryLoadSession(false);
81-
vscode.commands.executeCommand('setContext', 'gitpod.authenticated', this.isSignedIn());
90+
await this.tryLoadSession(false, managementSessionScopes);
91+
await this.tryLoadSession(false, defaultSessionScopes); // delete this after some time when users have a session with managementSessionScopes
92+
vscode.commands.executeCommand('setContext', 'gitpod.authenticated', this.session && arrayEquals(this.session.scopes.slice(0).sort(), managementSessionScopes.slice().sort()));
8293
// host changed, sign out, sign in
8394
const didChange = oldSession?.id !== this.session?.id;
8495
if (didChange) {
@@ -92,19 +103,19 @@ export class SessionService extends Disposable implements ISessionService {
92103
return !!this.session;
93104
}
94105

95-
async signIn(gitpodHost?: string) {
106+
async signIn(gitpodHost?: string, scopes: string[] = managementSessionScopes) {
96107
if (this.loginPromise) {
97108
this.logger.info(`Existing login in progress. Waiting for completion...`);
98109
return this.loginPromise;
99110
}
100111

101-
this.loginPromise = this.doSignIn(gitpodHost);
112+
this.loginPromise = this.doSignIn(gitpodHost || this.hostService.gitpodHost, scopes);
102113
this.loginPromise.finally(() => this.loginPromise = undefined);
103114
return this.loginPromise;
104115
}
105116

106-
private async doSignIn(gitpodHost?: string) {
107-
if (gitpodHost && new URL(this.hostService.gitpodHost).host !== new URL(gitpodHost).host) {
117+
private async doSignIn(gitpodHost: string, scopes: string[]) {
118+
if (new URL(this.hostService.gitpodHost).host !== new URL(gitpodHost).host) {
108119
const changedSessionPromise = eventToPromise(this.onDidChangeSession);
109120
const updated = await this.hostService.changeHost(gitpodHost);
110121
if (!updated) {
@@ -114,11 +125,11 @@ export class SessionService extends Disposable implements ISessionService {
114125
await changedSessionPromise;
115126
}
116127

117-
if (this.isSignedIn()) {
128+
if (this.session && arrayEquals(this.session.scopes.slice(0).sort(), scopes.slice().sort())) {
118129
return;
119130
}
120131

121-
await this.tryLoadSession(true);
132+
await this.tryLoadSession(true, scopes);
122133

123134
if (this.isSignedIn()) {
124135
this.logger.info(`Successfully signed in`);
@@ -127,15 +138,15 @@ export class SessionService extends Disposable implements ISessionService {
127138
}
128139
}
129140

130-
private async tryLoadSession(force: boolean) {
141+
private async tryLoadSession(force: boolean, scopes: string[]) {
131142
try {
132143
if (this.session && !force) {
133144
return;
134145
}
135146

136147
this.session = await vscode.authentication.getSession(
137148
'gitpod',
138-
sessionScopes,
149+
scopes,
139150
{
140151
createIfNone: force,
141152
silent: !force,

0 commit comments

Comments
 (0)