@theia/ai-chatgpt 1.77.0-next.99 → 1.77.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/chatgpt-command-contribution.d.ts +1 -1
- package/lib/browser/chatgpt-command-contribution.d.ts.map +1 -1
- package/lib/browser/chatgpt-command-contribution.js +42 -54
- package/lib/browser/chatgpt-command-contribution.js.map +1 -1
- package/lib/browser/chatgpt-command-contribution.spec.js +50 -52
- package/lib/browser/chatgpt-command-contribution.spec.js.map +1 -1
- package/lib/browser/chatgpt-frontend-application-contribution.d.ts +1 -1
- package/lib/browser/chatgpt-frontend-application-contribution.d.ts.map +1 -1
- package/lib/browser/chatgpt-frontend-application-contribution.js +50 -58
- package/lib/browser/chatgpt-frontend-application-contribution.js.map +1 -1
- package/lib/browser/chatgpt-frontend-application-contribution.spec.js +131 -133
- package/lib/browser/chatgpt-frontend-application-contribution.spec.js.map +1 -1
- package/lib/browser/chatgpt-frontend-module.js +23 -25
- package/lib/browser/chatgpt-frontend-module.js.map +1 -1
- package/lib/browser/chatgpt-status-bar-contribution.d.ts +1 -1
- package/lib/browser/chatgpt-status-bar-contribution.d.ts.map +1 -1
- package/lib/browser/chatgpt-status-bar-contribution.js +28 -38
- package/lib/browser/chatgpt-status-bar-contribution.js.map +1 -1
- package/lib/browser/chatgpt-status-bar-contribution.spec.js +39 -41
- package/lib/browser/chatgpt-status-bar-contribution.spec.js.map +1 -1
- package/lib/common/chatgpt-auth-service.js +4 -7
- package/lib/common/chatgpt-auth-service.js.map +1 -1
- package/lib/common/chatgpt-language-models-manager.js +3 -6
- package/lib/common/chatgpt-language-models-manager.js.map +1 -1
- package/lib/common/chatgpt-preferences.js +28 -31
- package/lib/common/chatgpt-preferences.js.map +1 -1
- package/lib/common/chatgpt-preferences.spec.js +9 -11
- package/lib/common/chatgpt-preferences.spec.js.map +1 -1
- package/lib/common/index.d.ts +3 -3
- package/lib/common/index.d.ts.map +1 -1
- package/lib/common/index.js +3 -6
- package/lib/common/index.js.map +1 -1
- package/lib/node/chatgpt-auth-service-impl.d.ts +2 -2
- package/lib/node/chatgpt-auth-service-impl.d.ts.map +1 -1
- package/lib/node/chatgpt-auth-service-impl.js +31 -38
- package/lib/node/chatgpt-auth-service-impl.js.map +1 -1
- package/lib/node/chatgpt-auth-service-impl.spec.js +203 -201
- package/lib/node/chatgpt-auth-service-impl.spec.js.map +1 -1
- package/lib/node/chatgpt-auth-service.d.ts +1 -1
- package/lib/node/chatgpt-auth-service.d.ts.map +1 -1
- package/lib/node/chatgpt-auth-service.js +8 -11
- package/lib/node/chatgpt-auth-service.js.map +1 -1
- package/lib/node/chatgpt-backend-module.js +25 -27
- package/lib/node/chatgpt-backend-module.js.map +1 -1
- package/lib/node/chatgpt-language-model.d.ts +2 -2
- package/lib/node/chatgpt-language-model.d.ts.map +1 -1
- package/lib/node/chatgpt-language-model.js +15 -19
- package/lib/node/chatgpt-language-model.js.map +1 -1
- package/lib/node/chatgpt-language-model.spec.js +155 -95
- package/lib/node/chatgpt-language-model.spec.js.map +1 -1
- package/lib/node/chatgpt-language-models-manager-impl.d.ts +4 -4
- package/lib/node/chatgpt-language-models-manager-impl.d.ts.map +1 -1
- package/lib/node/chatgpt-language-models-manager-impl.js +33 -42
- package/lib/node/chatgpt-language-models-manager-impl.js.map +1 -1
- package/lib/node/chatgpt-language-models-manager-impl.spec.js +51 -51
- package/lib/node/chatgpt-language-models-manager-impl.spec.js.map +1 -1
- package/lib/node/chatgpt-model-catalog.d.ts +1 -1
- package/lib/node/chatgpt-model-catalog.d.ts.map +1 -1
- package/lib/node/chatgpt-model-catalog.js +21 -29
- package/lib/node/chatgpt-model-catalog.js.map +1 -1
- package/lib/node/chatgpt-model-catalog.spec.js +43 -46
- package/lib/node/chatgpt-model-catalog.spec.js.map +1 -1
- package/lib/node/chatgpt-oauth.js +22 -30
- package/lib/node/chatgpt-oauth.js.map +1 -1
- package/lib/node/chatgpt-oauth.spec.js +33 -35
- package/lib/node/chatgpt-oauth.spec.js.map +1 -1
- package/lib/node/chatgpt-response-api-utils.d.ts +1 -3
- package/lib/node/chatgpt-response-api-utils.d.ts.map +1 -1
- package/lib/node/chatgpt-response-api-utils.js +9 -22
- package/lib/node/chatgpt-response-api-utils.js.map +1 -1
- package/package.json +15 -6
- package/src/browser/chatgpt-command-contribution.spec.ts +2 -2
- package/src/browser/chatgpt-command-contribution.ts +1 -1
- package/src/browser/chatgpt-frontend-application-contribution.spec.ts +3 -3
- package/src/browser/chatgpt-frontend-application-contribution.ts +2 -2
- package/src/browser/chatgpt-frontend-module.ts +4 -4
- package/src/browser/chatgpt-status-bar-contribution.spec.ts +3 -3
- package/src/browser/chatgpt-status-bar-contribution.ts +2 -2
- package/src/common/chatgpt-preferences.spec.ts +1 -1
- package/src/common/index.ts +3 -3
- package/src/node/chatgpt-auth-service-impl.spec.ts +10 -5
- package/src/node/chatgpt-auth-service-impl.ts +3 -3
- package/src/node/chatgpt-auth-service.ts +2 -2
- package/src/node/chatgpt-backend-module.ts +6 -6
- package/src/node/chatgpt-language-model.spec.ts +64 -4
- package/src/node/chatgpt-language-model.ts +3 -3
- package/src/node/chatgpt-language-models-manager-impl.spec.ts +8 -6
- package/src/node/chatgpt-language-models-manager-impl.ts +5 -5
- package/src/node/chatgpt-model-catalog.spec.ts +4 -4
- package/src/node/chatgpt-model-catalog.ts +3 -3
- package/src/node/chatgpt-oauth.spec.ts +1 -1
- package/src/node/chatgpt-response-api-utils.ts +1 -13
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
// *****************************************************************************
|
|
3
2
|
// Copyright (C) 2026 EclipseSource GmbH.
|
|
4
3
|
//
|
|
@@ -14,22 +13,21 @@
|
|
|
14
13
|
//
|
|
15
14
|
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
16
15
|
// *****************************************************************************
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const chatgpt_model_catalog_1 = require("./chatgpt-model-catalog");
|
|
16
|
+
import { expect } from 'chai';
|
|
17
|
+
import http from 'http';
|
|
18
|
+
import { syncBuiltinESMExports } from 'module';
|
|
19
|
+
import * as sinon from 'sinon';
|
|
20
|
+
import { Container } from '@theia/core/shared/inversify';
|
|
21
|
+
import { ILogger } from '@theia/core/lib/common/logger';
|
|
22
|
+
import { KeyStoreService } from '@theia/core/lib/common/key-store';
|
|
23
|
+
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
|
|
24
|
+
import { Deferred } from '@theia/core/lib/common/promise-util';
|
|
25
|
+
import { ChatGptAuthServiceImpl, createRemoteAuthService } from './chatgpt-auth-service-impl.js';
|
|
26
|
+
import { CHATGPT_REDIRECT_URI } from './chatgpt-oauth.js';
|
|
27
|
+
import { ChatGptAuthServiceConfig, ChatGptBackendAuthService, DEFAULT_CHATGPT_AUTH_SERVICE_CONFIG } from './chatgpt-auth-service.js';
|
|
28
|
+
import { ChatGptModelCatalog } from './chatgpt-model-catalog.js';
|
|
31
29
|
/** Avoids binding the loopback callback port. The code hand over is exercised through {@link completeLogin} instead. */
|
|
32
|
-
class TestAuthService extends
|
|
30
|
+
class TestAuthService extends ChatGptAuthServiceImpl {
|
|
33
31
|
async startCallbackServer() {
|
|
34
32
|
return this.callbackServerStartup;
|
|
35
33
|
}
|
|
@@ -86,8 +84,8 @@ async function expectRejection(promise, message) {
|
|
|
86
84
|
catch (caught) {
|
|
87
85
|
error = caught;
|
|
88
86
|
}
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
expect(error).to.be.instanceOf(Error);
|
|
88
|
+
expect(error.message).to.match(message);
|
|
91
89
|
}
|
|
92
90
|
/** Lets all pending microtasks run, so queued key store updates and generation changes are applied. */
|
|
93
91
|
async function flush() {
|
|
@@ -117,10 +115,10 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
117
115
|
findCredentials: sinon.stub().resolves([]),
|
|
118
116
|
keys: sinon.stub().resolves([])
|
|
119
117
|
};
|
|
120
|
-
container = new
|
|
121
|
-
container.bind(
|
|
122
|
-
container.bind(
|
|
123
|
-
container.bind(
|
|
118
|
+
container = new Container();
|
|
119
|
+
container.bind(ChatGptAuthServiceConfig).toConstantValue(DEFAULT_CHATGPT_AUTH_SERVICE_CONFIG);
|
|
120
|
+
container.bind(KeyStoreService).toConstantValue(keyStoreService);
|
|
121
|
+
container.bind(ILogger).to(MockLogger).inSingletonScope();
|
|
124
122
|
container.bind(TestAuthService).toSelf().inSingletonScope();
|
|
125
123
|
authService = container.get(TestAuthService);
|
|
126
124
|
fetchStub = sinon.stub(globalThis, 'fetch');
|
|
@@ -129,6 +127,7 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
129
127
|
// Abandoned login attempts keep a timer alive.
|
|
130
128
|
await authService.cancelLogin();
|
|
131
129
|
sinon.restore();
|
|
130
|
+
syncBuiltinESMExports();
|
|
132
131
|
});
|
|
133
132
|
describe('custom backend configuration', () => {
|
|
134
133
|
const config = {
|
|
@@ -141,7 +140,7 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
141
140
|
};
|
|
142
141
|
const redirectUri = 'http://127.0.0.1:2468/custom/callback';
|
|
143
142
|
beforeEach(() => {
|
|
144
|
-
container.rebind(
|
|
143
|
+
container.rebind(ChatGptAuthServiceConfig).toConstantValue(config);
|
|
145
144
|
container.rebind(TestAuthService).toSelf().inSingletonScope();
|
|
146
145
|
authService = container.get(TestAuthService);
|
|
147
146
|
});
|
|
@@ -149,31 +148,31 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
149
148
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
150
149
|
const session = await authService.startLogin();
|
|
151
150
|
const url = new URL(session.authorizationUrl);
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
await expectRejection(authService.completeLogin(`${
|
|
155
|
-
|
|
151
|
+
expect(url.searchParams.get('client_id')).to.equal(config.clientId);
|
|
152
|
+
expect(url.searchParams.get('redirect_uri')).to.equal(redirectUri);
|
|
153
|
+
await expectRejection(authService.completeLogin(`${CHATGPT_REDIRECT_URI}?code=code&state=${authService.pendingState}`), /callback/);
|
|
154
|
+
expect(await authService.completeLogin(`${redirectUri}?code=code&state=${authService.pendingState}`)).to.equal(true);
|
|
156
155
|
const body = fetchStub.firstCall.args[1].body;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
expect(body.get('client_id')).to.equal(config.clientId);
|
|
157
|
+
expect(body.get('redirect_uri')).to.equal(redirectUri);
|
|
158
|
+
expect(setPasswordStub.firstCall.args.slice(0, 2)).to.deep.equal([config.keyStoreService, config.keyStoreAccount]);
|
|
160
159
|
});
|
|
161
160
|
it('uses the configured key store identifiers and client when refreshing and signing out', async () => {
|
|
162
161
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
163
162
|
fetchStub.resolves(tokenResponse({ access_token: REFRESHED_TOKEN, expires_in: 3600 }));
|
|
164
163
|
await authService.getCredentials();
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
expect(getPasswordStub.firstCall.args).to.deep.equal([config.keyStoreService, config.keyStoreAccount]);
|
|
165
|
+
expect(fetchStub.firstCall.args[1].body.get('client_id')).to.equal(config.clientId);
|
|
166
|
+
expect(setPasswordStub.firstCall.args.slice(0, 2)).to.deep.equal([config.keyStoreService, config.keyStoreAccount]);
|
|
168
167
|
await authService.signOut();
|
|
169
|
-
|
|
168
|
+
expect(deletePasswordStub.firstCall.args).to.deep.equal([config.keyStoreService, config.keyStoreAccount]);
|
|
170
169
|
});
|
|
171
170
|
it('accepts only the configured loopback callback path', async () => {
|
|
172
171
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), expires_in: 3600 }));
|
|
173
172
|
await authService.startLogin();
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
173
|
+
expect(authService.callCallback(`/auth/callback?code=code&state=${authService.pendingState}`).statusCode).to.equal(404);
|
|
174
|
+
expect(authService.callCallback(`${config.callbackPath}?code=code&state=${authService.pendingState}`).statusCode).to.equal(200);
|
|
175
|
+
expect(await authService.waitForLogin()).to.equal(true);
|
|
177
176
|
});
|
|
178
177
|
it('binds the listener to the configured host and port', async () => {
|
|
179
178
|
const server = {
|
|
@@ -182,12 +181,15 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
182
181
|
close: sinon.stub(),
|
|
183
182
|
closeAllConnections: sinon.stub()
|
|
184
183
|
};
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
184
|
+
// The named exports of ES modules cannot be stubbed, but those of built-in modules can be synchronized
|
|
185
|
+
// with changes of the module object, which is their default export.
|
|
186
|
+
sinon.stub(http, 'createServer').returns(server);
|
|
187
|
+
syncBuiltinESMExports();
|
|
188
|
+
container.bind(ChatGptAuthServiceImpl).toSelf().inSingletonScope();
|
|
189
|
+
const service = container.get(ChatGptAuthServiceImpl);
|
|
188
190
|
try {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
+
expect((await service.startLogin()).callbackListening).to.equal(true);
|
|
192
|
+
expect(server.listen.firstCall.args.slice(0, 2)).to.deep.equal([config.callbackPort, config.callbackHost]);
|
|
191
193
|
}
|
|
192
194
|
finally {
|
|
193
195
|
await service.cancelLogin();
|
|
@@ -206,11 +208,11 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
206
208
|
await authService.cancelLogin();
|
|
207
209
|
listening(server);
|
|
208
210
|
await starting;
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
211
|
+
expect(await result).to.equal(false);
|
|
212
|
+
expect(server.close.calledOnce).to.equal(true);
|
|
213
|
+
expect(server.closeAllConnections.calledOnce).to.equal(true);
|
|
214
|
+
expect(clock.countTimers()).to.equal(0);
|
|
215
|
+
expect(authService.pendingState).to.equal(undefined);
|
|
214
216
|
});
|
|
215
217
|
it('closes a replaced listener without closing or timing out the current attempt', async () => {
|
|
216
218
|
const clock = sinon.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
|
|
@@ -222,54 +224,54 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
222
224
|
await flush();
|
|
223
225
|
const staleResult = authService.waitForLogin();
|
|
224
226
|
authService.callbackServerStartup = Promise.resolve(currentServer);
|
|
225
|
-
|
|
227
|
+
expect((await authService.startLogin()).callbackListening).to.equal(true);
|
|
226
228
|
const currentState = authService.pendingState;
|
|
227
229
|
listening(staleServer);
|
|
228
230
|
await staleStartup;
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
231
|
+
expect(await staleResult).to.equal(false);
|
|
232
|
+
expect(staleServer.close.calledOnce).to.equal(true);
|
|
233
|
+
expect(staleServer.closeAllConnections.calledOnce).to.equal(true);
|
|
234
|
+
expect(currentServer.close.called).to.equal(false);
|
|
235
|
+
expect(authService.pendingState).to.equal(currentState);
|
|
236
|
+
expect(clock.countTimers()).to.equal(1);
|
|
235
237
|
await authService.cancelLogin();
|
|
236
|
-
|
|
237
|
-
|
|
238
|
+
expect(clock.countTimers()).to.equal(0);
|
|
239
|
+
expect(currentServer.close.calledOnce).to.equal(true);
|
|
238
240
|
});
|
|
239
241
|
});
|
|
240
242
|
describe('getAuthState', () => {
|
|
241
243
|
it('reports no session when nothing is stored', async () => {
|
|
242
|
-
|
|
244
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
243
245
|
});
|
|
244
246
|
it('reports the account and plan of the stored session', async () => {
|
|
245
247
|
getPasswordStub.resolves(storedCredentials(Date.now() + 3600_000));
|
|
246
|
-
|
|
248
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
247
249
|
});
|
|
248
250
|
it('reads the session from the ChatGPT key store entry', async () => {
|
|
249
251
|
getPasswordStub.resolves(storedCredentials(Date.now() + 3600_000));
|
|
250
252
|
await authService.getAuthState();
|
|
251
|
-
|
|
253
|
+
expect(getPasswordStub.firstCall.args).to.deep.equal(['theia-chatgpt', 'default']);
|
|
252
254
|
});
|
|
253
255
|
it('reports no session when the stored credentials are malformed', async () => {
|
|
254
256
|
getPasswordStub.resolves('not-json');
|
|
255
|
-
|
|
257
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
256
258
|
});
|
|
257
259
|
it('reports no session for an empty stored object', async () => {
|
|
258
260
|
getPasswordStub.resolves('{}');
|
|
259
|
-
|
|
261
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
260
262
|
});
|
|
261
263
|
it('reports no session when the stored credentials have no ChatGPT account', async () => {
|
|
262
264
|
getPasswordStub.resolves(JSON.stringify({ accessToken: 'token', refreshToken: 'refresh', expiresAt: Date.now() + 3600_000 }));
|
|
263
|
-
|
|
264
|
-
|
|
265
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
266
|
+
expect(await authService.getCredentials()).to.equal(undefined);
|
|
265
267
|
});
|
|
266
268
|
it('reports no session for blank stored values, which cannot authenticate a request', async () => {
|
|
267
269
|
getPasswordStub.resolves(JSON.stringify({ accessToken: 'token', refreshToken: 'refresh', expiresAt: Date.now() + 3600_000, accountId: ' ' }));
|
|
268
|
-
|
|
270
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
269
271
|
});
|
|
270
272
|
it('reports no session for a blank stored access token', async () => {
|
|
271
273
|
getPasswordStub.resolves(JSON.stringify({ accessToken: ' ', refreshToken: 'refresh', expiresAt: Date.now() + 3600_000, accountId: 'account-id' }));
|
|
272
|
-
|
|
274
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
273
275
|
});
|
|
274
276
|
});
|
|
275
277
|
describe('completeLogin', () => {
|
|
@@ -281,39 +283,39 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
281
283
|
authService.addClient({ onAuthStateChanged: state => states.push(state) });
|
|
282
284
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
283
285
|
const session = await authService.startLogin();
|
|
284
|
-
|
|
285
|
-
|
|
286
|
+
expect(session.callbackListening).to.equal(false);
|
|
287
|
+
expect(await authService.completeLogin('the-code')).to.equal(true);
|
|
286
288
|
const body = fetchStub.firstCall.args[1].body;
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
289
|
+
expect(body.get('grant_type')).to.equal('authorization_code');
|
|
290
|
+
expect(body.get('code')).to.equal('the-code');
|
|
291
|
+
expect(body.get('code_verifier')).to.have.length.greaterThan(0);
|
|
292
|
+
expect(body.get('redirect_uri')).to.equal(CHATGPT_REDIRECT_URI);
|
|
293
|
+
expect(setPasswordStub.calledOnce).to.equal(true);
|
|
294
|
+
expect(setPasswordStub.firstCall.args.slice(0, 2)).to.deep.equal(['theia-chatgpt', 'default']);
|
|
295
|
+
expect(states).to.deep.equal([{ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' }]);
|
|
294
296
|
});
|
|
295
297
|
it('rejects a redirect URL that belongs to another sign in attempt', async () => {
|
|
296
298
|
await authService.startLogin();
|
|
297
|
-
await expectRejection(authService.completeLogin(`${
|
|
298
|
-
|
|
299
|
+
await expectRejection(authService.completeLogin(`${CHATGPT_REDIRECT_URI}?code=the-code&state=other`), /state/);
|
|
300
|
+
expect(fetchStub.called).to.equal(false);
|
|
299
301
|
});
|
|
300
302
|
it('accepts the redirect URL of the pending sign in attempt', async () => {
|
|
301
303
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
302
304
|
await authService.startLogin();
|
|
303
|
-
|
|
305
|
+
expect(await authService.completeLogin(`${CHATGPT_REDIRECT_URI}?code=the-code&state=${authService.pendingState}`)).to.equal(true);
|
|
304
306
|
});
|
|
305
307
|
it('rejects a token response without a ChatGPT account', async () => {
|
|
306
308
|
fetchStub.resolves(tokenResponse({ access_token: 'not-a-jwt', refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
307
309
|
await authService.startLogin();
|
|
308
310
|
await expectRejection(authService.completeLogin('the-code'), /account could not be determined/i);
|
|
309
|
-
|
|
311
|
+
expect(setPasswordStub.called).to.equal(false);
|
|
310
312
|
});
|
|
311
313
|
it('does not publish a session that could not be persisted', async () => {
|
|
312
314
|
setPasswordStub.rejects(new Error('key store unavailable'));
|
|
313
315
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
314
316
|
await authService.startLogin();
|
|
315
317
|
await expectRejection(authService.completeLogin('the-code'), /key store unavailable/);
|
|
316
|
-
|
|
318
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
317
319
|
});
|
|
318
320
|
it('does not sign in a login attempt that was cancelled while the tokens were requested', async () => {
|
|
319
321
|
let respond = () => { };
|
|
@@ -323,9 +325,9 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
323
325
|
await waitFor(() => fetchStub.called);
|
|
324
326
|
await authService.cancelLogin();
|
|
325
327
|
respond(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
328
|
+
expect(await signingIn).to.equal(false);
|
|
329
|
+
expect(setPasswordStub.called).to.equal(false);
|
|
330
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
329
331
|
});
|
|
330
332
|
for (const hasPreviousSession of [false, true]) {
|
|
331
333
|
it(`rolls back a cancelled login write ${hasPreviousSession ? 'to the previous session' : 'without leaving credentials'}`, async () => {
|
|
@@ -342,24 +344,24 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
342
344
|
await waitFor(() => setPasswordStub.called);
|
|
343
345
|
const reading = authService.getAuthState();
|
|
344
346
|
await flush();
|
|
345
|
-
|
|
347
|
+
expect(getPasswordStub.calledOnce).to.equal(true);
|
|
346
348
|
await authService.cancelLogin();
|
|
347
349
|
completeWrite();
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
350
|
+
expect(await signingIn).to.equal(false);
|
|
351
|
+
expect(await result).to.equal(false);
|
|
352
|
+
expect(states).to.be.empty;
|
|
353
|
+
expect(await reading).to.deep.equal(hasPreviousSession
|
|
352
354
|
? { isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' }
|
|
353
355
|
: { isAuthenticated: false });
|
|
354
|
-
|
|
356
|
+
expect((await authService.getCredentials())?.accountId).to.equal(hasPreviousSession ? 'account-id' : undefined);
|
|
355
357
|
if (hasPreviousSession) {
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
358
|
+
expect(setPasswordStub.callCount).to.equal(2);
|
|
359
|
+
expect(setPasswordStub.lastCall.args[2]).to.equal(previous);
|
|
360
|
+
expect(deletePasswordStub.called).to.equal(false);
|
|
359
361
|
}
|
|
360
362
|
else {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
+
expect(setPasswordStub.calledOnce).to.equal(true);
|
|
364
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
363
365
|
}
|
|
364
366
|
});
|
|
365
367
|
}
|
|
@@ -377,15 +379,15 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
377
379
|
await authService.startLogin();
|
|
378
380
|
const newLogin = authService.completeLogin('new-code');
|
|
379
381
|
await flush();
|
|
380
|
-
|
|
382
|
+
expect(setPasswordStub.calledOnce).to.equal(true);
|
|
381
383
|
completeWrite();
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
384
|
+
expect(await oldLogin).to.equal(false);
|
|
385
|
+
expect(await newLogin).to.equal(true);
|
|
386
|
+
expect(states).to.have.lengthOf(1);
|
|
387
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
388
|
+
expect(deletePasswordStub.firstCall.calledBefore(setPasswordStub.lastCall)).to.equal(true);
|
|
389
|
+
expect(JSON.parse(setPasswordStub.lastCall.args[2]).accountId).to.equal('new-account');
|
|
390
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: newToken, accountId: 'new-account' });
|
|
389
391
|
});
|
|
390
392
|
it('does not let a stale failed login write affect its replacement', async () => {
|
|
391
393
|
let failWrite = () => { };
|
|
@@ -398,9 +400,9 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
398
400
|
await authService.startLogin();
|
|
399
401
|
const newLogin = authService.completeLogin('new-code');
|
|
400
402
|
failWrite(new Error('stale write failed'));
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
403
|
+
expect(await oldLogin).to.equal(false);
|
|
404
|
+
expect(await newLogin).to.equal(true);
|
|
405
|
+
expect((await authService.getCredentials())?.accountId).to.equal('new-account');
|
|
404
406
|
});
|
|
405
407
|
it('stops notifying a client whose registration was disposed', async () => {
|
|
406
408
|
const states = [];
|
|
@@ -408,17 +410,17 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
408
410
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
409
411
|
await authService.startLogin();
|
|
410
412
|
await authService.completeLogin('the-code');
|
|
411
|
-
|
|
413
|
+
expect(states).to.be.empty;
|
|
412
414
|
});
|
|
413
415
|
});
|
|
414
416
|
describe('getCredentials', () => {
|
|
415
417
|
it('returns undefined when there is no session', async () => {
|
|
416
|
-
|
|
418
|
+
expect(await authService.getCredentials()).to.equal(undefined);
|
|
417
419
|
});
|
|
418
420
|
it('uses the stored access token while it is valid', async () => {
|
|
419
421
|
getPasswordStub.resolves(storedCredentials(Date.now() + 3600_000));
|
|
420
|
-
|
|
421
|
-
|
|
422
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: 'stored-token', accountId: 'account-id' });
|
|
423
|
+
expect(fetchStub.called).to.equal(false);
|
|
422
424
|
});
|
|
423
425
|
it('serves callers that arrive while the key store is still being read', async () => {
|
|
424
426
|
getPasswordStub.callsFake(async () => {
|
|
@@ -426,9 +428,9 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
426
428
|
return storedCredentials(Date.now() + 3600_000);
|
|
427
429
|
});
|
|
428
430
|
const [first, second] = await Promise.all([authService.getCredentials(), authService.getCredentials()]);
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
431
|
+
expect(first).to.deep.equal({ accessToken: 'stored-token', accountId: 'account-id' });
|
|
432
|
+
expect(second).to.deep.equal(first);
|
|
433
|
+
expect(getPasswordStub.calledOnce).to.equal(true);
|
|
432
434
|
});
|
|
433
435
|
it('discards a pending key store load after a new session is accepted', async () => {
|
|
434
436
|
let completeRead = () => { };
|
|
@@ -438,10 +440,10 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
438
440
|
const newToken = createAccessToken('new-account');
|
|
439
441
|
fetchStub.resolves(tokenResponse({ access_token: newToken, expires_in: 3600 }));
|
|
440
442
|
await authService.startLogin();
|
|
441
|
-
|
|
443
|
+
expect(await authService.completeLogin('new-code')).to.equal(true);
|
|
442
444
|
completeRead(storedCredentials(Date.now() + 3600_000));
|
|
443
|
-
|
|
444
|
-
|
|
445
|
+
expect(await reading).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
446
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: newToken, accountId: 'new-account' });
|
|
445
447
|
});
|
|
446
448
|
for (const response of [
|
|
447
449
|
tokenResponse({ access_token: REFRESHED_TOKEN, expires_in: 3600 }),
|
|
@@ -457,19 +459,19 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
457
459
|
const newToken = createAccessToken('new-account');
|
|
458
460
|
fetchStub.resolves(tokenResponse({ access_token: newToken, expires_in: 3600 }));
|
|
459
461
|
await authService.startLogin();
|
|
460
|
-
|
|
462
|
+
expect(await authService.completeLogin('new-code')).to.equal(true);
|
|
461
463
|
respond(response);
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
464
|
+
expect(await refreshing).to.equal(undefined);
|
|
465
|
+
expect(deletePasswordStub.called).to.equal(false);
|
|
466
|
+
expect(setPasswordStub.calledOnce).to.equal(true);
|
|
467
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: newToken, accountId: 'new-account' });
|
|
466
468
|
});
|
|
467
469
|
}
|
|
468
470
|
for (const signOutFirst of [false, true]) {
|
|
469
471
|
it(`refreshes a new session independently of a pending old refresh ${signOutFirst ? 'after sign out' : 'after direct replacement'}`, async () => {
|
|
470
472
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
471
|
-
const oldResponse = new
|
|
472
|
-
const newResponse = new
|
|
473
|
+
const oldResponse = new Deferred();
|
|
474
|
+
const newResponse = new Deferred();
|
|
473
475
|
fetchStub.onFirstCall().returns(oldResponse.promise);
|
|
474
476
|
fetchStub.onSecondCall().resolves(tokenResponse({
|
|
475
477
|
access_token: createAccessToken('new-account'), refresh_token: 'new-refresh', expires_in: 0
|
|
@@ -481,82 +483,82 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
481
483
|
await authService.signOut();
|
|
482
484
|
}
|
|
483
485
|
await authService.startLogin();
|
|
484
|
-
|
|
486
|
+
expect(await authService.completeLogin('new-code')).to.equal(true);
|
|
485
487
|
const newRefresh = authService.getCredentials();
|
|
486
488
|
await flush();
|
|
487
|
-
|
|
488
|
-
|
|
489
|
+
expect(fetchStub.callCount).to.equal(3);
|
|
490
|
+
expect(fetchStub.thirdCall.args[1].body.get('refresh_token')).to.equal('new-refresh');
|
|
489
491
|
oldResponse.resolve(tokenResponse({ access_token: REFRESHED_TOKEN, expires_in: 3600 }));
|
|
490
|
-
|
|
492
|
+
expect(await oldRefresh).to.equal(undefined);
|
|
491
493
|
const concurrentRefresh = authService.getCredentials();
|
|
492
494
|
await flush();
|
|
493
495
|
// Finishing the old refresh must not clear the new session's coalesced request.
|
|
494
|
-
|
|
496
|
+
expect(fetchStub.callCount).to.equal(3);
|
|
495
497
|
const newToken = createAccessToken('new-account');
|
|
496
498
|
newResponse.resolve(tokenResponse({ access_token: newToken, expires_in: 3600 }));
|
|
497
499
|
const expected = { accessToken: newToken, accountId: 'new-account' };
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
500
|
+
expect(await newRefresh).to.deep.equal(expected);
|
|
501
|
+
expect(await concurrentRefresh).to.deep.equal(expected);
|
|
502
|
+
expect(await authService.getCredentials()).to.deep.equal(expected);
|
|
503
|
+
expect(deletePasswordStub.callCount).to.equal(signOutFirst ? 1 : 0);
|
|
502
504
|
});
|
|
503
505
|
}
|
|
504
506
|
it('refreshes an expiring access token and retains the refresh token when none is rotated in', async () => {
|
|
505
507
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
506
508
|
fetchStub.resolves(tokenResponse({ access_token: REFRESHED_TOKEN, expires_in: 3600 }));
|
|
507
|
-
|
|
509
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: REFRESHED_TOKEN, accountId: 'refreshed-account' });
|
|
508
510
|
const body = fetchStub.firstCall.args[1].body;
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
511
|
+
expect(body.get('grant_type')).to.equal('refresh_token');
|
|
512
|
+
expect(body.get('refresh_token')).to.equal('stored-refresh-token');
|
|
513
|
+
expect(JSON.parse(setPasswordStub.firstCall.args[2]).refreshToken).to.equal('stored-refresh-token');
|
|
512
514
|
});
|
|
513
515
|
it('performs a single token request for concurrent refreshes', async () => {
|
|
514
516
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
515
517
|
fetchStub.resolves(tokenResponse({ access_token: REFRESHED_TOKEN, refresh_token: 'rotated', expires_in: 3600 }));
|
|
516
518
|
const [first, second] = await Promise.all([authService.getCredentials(), authService.getCredentials()]);
|
|
517
|
-
|
|
518
|
-
|
|
519
|
+
expect(first).to.deep.equal(second);
|
|
520
|
+
expect(fetchStub.calledOnce).to.equal(true);
|
|
519
521
|
});
|
|
520
522
|
it('signs out when the refresh token is rejected', async () => {
|
|
521
523
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
522
524
|
fetchStub.resolves(tokenResponse({ error: 'invalid_grant' }, 400));
|
|
523
525
|
await expectRejection(authService.getCredentials(), /session has expired/i);
|
|
524
|
-
|
|
525
|
-
|
|
526
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
527
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
526
528
|
});
|
|
527
529
|
it('signs out when the refresh token is reported as spent under an OpenAI specific code', async () => {
|
|
528
530
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
529
531
|
fetchStub.resolves(tokenResponse({ error: 'refresh_token_expired' }, 400));
|
|
530
532
|
await expectRejection(authService.getCredentials(), /session has expired/i);
|
|
531
|
-
|
|
532
|
-
|
|
533
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
534
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
533
535
|
});
|
|
534
536
|
it('keeps the session when the refresh fails temporarily', async () => {
|
|
535
537
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
536
538
|
fetchStub.resolves(tokenResponse({ error: 'server_error' }, 503));
|
|
537
539
|
await expectRejection(authService.getCredentials(), /503/);
|
|
538
|
-
|
|
539
|
-
|
|
540
|
+
expect(deletePasswordStub.called).to.equal(false);
|
|
541
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
540
542
|
});
|
|
541
543
|
it('keeps the session when the refresh is rate limited, which does not reject the grant', async () => {
|
|
542
544
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
543
545
|
fetchStub.resolves(tokenResponse({ error: 'rate_limit_exceeded' }, 429));
|
|
544
546
|
await expectRejection(authService.getCredentials(), /429/);
|
|
545
|
-
|
|
546
|
-
|
|
547
|
+
expect(deletePasswordStub.called).to.equal(false);
|
|
548
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
547
549
|
});
|
|
548
550
|
it('keeps the session when the token endpoint answers without an OAuth error', async () => {
|
|
549
551
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
550
552
|
fetchStub.resolves({ ok: false, status: 400, text: async () => '<html>Bad Request</html>' });
|
|
551
553
|
await expectRejection(authService.getCredentials(), /400/);
|
|
552
|
-
|
|
553
|
-
|
|
554
|
+
expect(deletePasswordStub.called).to.equal(false);
|
|
555
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
554
556
|
});
|
|
555
557
|
it('keeps the account of the session when a refreshed token does not restate it', async () => {
|
|
556
558
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
557
559
|
fetchStub.resolves(tokenResponse({ access_token: 'not-a-jwt', refresh_token: 'rotated', expires_in: 3600 }));
|
|
558
|
-
|
|
559
|
-
|
|
560
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: 'not-a-jwt', accountId: 'account-id' });
|
|
561
|
+
expect(JSON.parse(setPasswordStub.firstCall.args[2])).to.include({ accountId: 'account-id', accountLabel: 'user@example.com', planType: 'plus' });
|
|
560
562
|
});
|
|
561
563
|
it('discards a refresh that completes after the user signed out', async () => {
|
|
562
564
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
@@ -566,9 +568,9 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
566
568
|
await waitFor(() => fetchStub.called);
|
|
567
569
|
await authService.signOut();
|
|
568
570
|
respond(tokenResponse({ access_token: REFRESHED_TOKEN, refresh_token: 'rotated', expires_in: 3600 }));
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
571
|
+
expect(await refreshing).to.equal(undefined);
|
|
572
|
+
expect(setPasswordStub.called).to.equal(false);
|
|
573
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
572
574
|
});
|
|
573
575
|
it('does not sign out the session of a new sign in when a stale refresh is rejected', async () => {
|
|
574
576
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
@@ -580,13 +582,13 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
580
582
|
await authService.signOut();
|
|
581
583
|
fetchStub.resolves(tokenResponse({ access_token: newToken, refresh_token: 'new-refresh', expires_in: 3600 }));
|
|
582
584
|
await authService.startLogin();
|
|
583
|
-
|
|
585
|
+
expect(await authService.completeLogin('the-code')).to.equal(true);
|
|
584
586
|
respond(tokenResponse({ error: 'invalid_grant' }, 400));
|
|
585
|
-
|
|
587
|
+
expect(await refreshing).to.equal(undefined);
|
|
586
588
|
// Only the explicit sign out removed credentials, the rejected refresh left the new session alone.
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
589
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
590
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
591
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: newToken, accountId: 'new-account' });
|
|
590
592
|
});
|
|
591
593
|
it('does not publish a session whose write outlived the sign out', async () => {
|
|
592
594
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
@@ -599,10 +601,10 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
599
601
|
await flush();
|
|
600
602
|
completeWrite();
|
|
601
603
|
await signingOut;
|
|
602
|
-
|
|
604
|
+
expect(await refreshing).to.equal(undefined);
|
|
603
605
|
// The sign out is queued behind the stale write, so it removes what that write left behind.
|
|
604
|
-
|
|
605
|
-
|
|
606
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
607
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
606
608
|
});
|
|
607
609
|
it('keeps the session of a sign in that happened while a stale write was still pending', async () => {
|
|
608
610
|
getPasswordStub.resolves(storedCredentials(Date.now() + 1000));
|
|
@@ -620,9 +622,9 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
620
622
|
await flush();
|
|
621
623
|
completeStaleWrite();
|
|
622
624
|
await Promise.all([refreshing, signingOut, signingIn]);
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
625
|
+
expect(await signingIn).to.equal(true);
|
|
626
|
+
expect(JSON.parse(setPasswordStub.lastCall.args[2]).accountId).to.equal('new-account');
|
|
627
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
626
628
|
});
|
|
627
629
|
});
|
|
628
630
|
describe('loopback callback', () => {
|
|
@@ -630,22 +632,22 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
630
632
|
await authService.startLogin();
|
|
631
633
|
});
|
|
632
634
|
it('rejects a request to an unknown route', () => {
|
|
633
|
-
|
|
634
|
-
|
|
635
|
+
expect(authService.callCallback(`/elsewhere?code=the-code&state=${authService.pendingState}`).statusCode).to.equal(404);
|
|
636
|
+
expect(fetchStub.called).to.equal(false);
|
|
635
637
|
});
|
|
636
638
|
it('rejects a callback of another sign in attempt', () => {
|
|
637
|
-
|
|
638
|
-
|
|
639
|
+
expect(authService.callCallback('/auth/callback?code=the-code&state=other').statusCode).to.equal(400);
|
|
640
|
+
expect(fetchStub.called).to.equal(false);
|
|
639
641
|
});
|
|
640
642
|
it('rejects a callback without an authorization code', () => {
|
|
641
|
-
|
|
642
|
-
|
|
643
|
+
expect(authService.callCallback(`/auth/callback?state=${authService.pendingState}`).statusCode).to.equal(400);
|
|
644
|
+
expect(fetchStub.called).to.equal(false);
|
|
643
645
|
});
|
|
644
646
|
it('exchanges the code delivered by the browser', async () => {
|
|
645
647
|
fetchStub.resolves(tokenResponse({ access_token: createAccessToken('account-id'), refresh_token: 'refresh-token', expires_in: 3600 }));
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
648
|
+
expect(authService.callCallback(`/auth/callback?code=the-code&state=${authService.pendingState}`).statusCode).to.equal(200);
|
|
649
|
+
expect(await authService.waitForLogin()).to.equal(true);
|
|
650
|
+
expect(fetchStub.firstCall.args[1].body.get('code')).to.equal('the-code');
|
|
649
651
|
});
|
|
650
652
|
});
|
|
651
653
|
describe('signOut', () => {
|
|
@@ -655,26 +657,26 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
655
657
|
authService.addClient({ onAuthStateChanged: state => states.push(state) });
|
|
656
658
|
await authService.getAuthState();
|
|
657
659
|
await authService.signOut();
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
660
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
661
|
+
expect(deletePasswordStub.firstCall.args).to.deep.equal(['theia-chatgpt', 'default']);
|
|
662
|
+
expect(states).to.deep.equal([{ isAuthenticated: false }]);
|
|
663
|
+
expect(await authService.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
662
664
|
});
|
|
663
665
|
for (const pendingDiscovery of [false, true]) {
|
|
664
666
|
it(`notifies clients and invalidates ${pendingDiscovery ? 'pending' : 'cached'} models before keystore deletion completes`, async () => {
|
|
665
667
|
getPasswordStub.resolves(storedCredentials(Date.now() + 3600_000));
|
|
666
|
-
const deletion = new
|
|
668
|
+
const deletion = new Deferred();
|
|
667
669
|
deletePasswordStub.returns(deletion.promise);
|
|
668
670
|
const listingResponse = tokenResponse({ models: [{ slug: 'gpt-5.5', supported_in_api: true, visibility: 'list' }] });
|
|
669
|
-
const discoveryResponse = new
|
|
671
|
+
const discoveryResponse = new Deferred();
|
|
670
672
|
fetchStub.returns(pendingDiscovery ? discoveryResponse.promise : Promise.resolve(listingResponse));
|
|
671
|
-
container.bind(
|
|
672
|
-
container.bind(
|
|
673
|
-
const catalog = container.get(
|
|
673
|
+
container.bind(ChatGptBackendAuthService).toConstantValue(authService);
|
|
674
|
+
container.bind(ChatGptModelCatalog).toSelf().inSingletonScope();
|
|
675
|
+
const catalog = container.get(ChatGptModelCatalog);
|
|
674
676
|
const models = catalog.getAvailableModels();
|
|
675
677
|
await waitFor(() => fetchStub.called);
|
|
676
678
|
if (!pendingDiscovery) {
|
|
677
|
-
|
|
679
|
+
expect(await models).to.deep.equal(['gpt-5.5']);
|
|
678
680
|
}
|
|
679
681
|
const clientStates = [];
|
|
680
682
|
const eventStates = [];
|
|
@@ -682,27 +684,27 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
682
684
|
authService.onAuthStateChanged(state => eventStates.push(state));
|
|
683
685
|
let signedOut = false;
|
|
684
686
|
const signingOut = authService.signOut().then(() => { signedOut = true; });
|
|
685
|
-
|
|
686
|
-
|
|
687
|
+
expect(clientStates).to.deep.equal([{ isAuthenticated: false }]);
|
|
688
|
+
expect(eventStates).to.deep.equal([{ isAuthenticated: false }]);
|
|
687
689
|
await waitFor(() => deletePasswordStub.called);
|
|
688
|
-
|
|
689
|
-
|
|
690
|
+
expect(signedOut).to.equal(false);
|
|
691
|
+
expect(await catalog.getAvailableModels()).to.deep.equal([]);
|
|
690
692
|
if (pendingDiscovery) {
|
|
691
693
|
discoveryResponse.resolve(listingResponse);
|
|
692
|
-
|
|
694
|
+
expect(await models).to.deep.equal([]);
|
|
693
695
|
}
|
|
694
|
-
|
|
696
|
+
expect(fetchStub.calledOnce).to.equal(true);
|
|
695
697
|
deletion.resolve(true);
|
|
696
698
|
await signingOut;
|
|
697
|
-
|
|
698
|
-
|
|
699
|
+
expect(clientStates).to.have.lengthOf(1);
|
|
700
|
+
expect(eventStates).to.have.lengthOf(1);
|
|
699
701
|
});
|
|
700
702
|
}
|
|
701
703
|
it('abandons a pending sign in', async () => {
|
|
702
704
|
await authService.startLogin();
|
|
703
705
|
const pending = authService.waitForLogin();
|
|
704
706
|
await authService.signOut();
|
|
705
|
-
|
|
707
|
+
expect(await pending).to.equal(false);
|
|
706
708
|
});
|
|
707
709
|
});
|
|
708
710
|
describe('createRemoteAuthService', () => {
|
|
@@ -713,25 +715,25 @@ describe('ChatGptAuthServiceImpl', () => {
|
|
|
713
715
|
}
|
|
714
716
|
it('does not expose the access token to the frontend', async () => {
|
|
715
717
|
getPasswordStub.resolves(storedCredentials(Date.now() + 3600_000));
|
|
716
|
-
const remote =
|
|
717
|
-
|
|
718
|
-
|
|
718
|
+
const remote = createRemoteAuthService(authService);
|
|
719
|
+
expect(invoke(remote, 'getCredentials')).to.equal(undefined);
|
|
720
|
+
expect(await authService.getCredentials()).to.deep.equal({ accessToken: 'stored-token', accountId: 'account-id' });
|
|
719
721
|
});
|
|
720
722
|
it('serves only the methods of the service interface', () => {
|
|
721
|
-
const remote =
|
|
723
|
+
const remote = createRemoteAuthService(authService);
|
|
722
724
|
const served = Object.keys(remote).filter(key => typeof remote[key] === 'function');
|
|
723
725
|
// `onAuthStateChanged` is an event the frontend proxy resolves locally, it is never dispatched over RPC.
|
|
724
|
-
|
|
725
|
-
|
|
726
|
+
expect(served.sort()).to.deep.equal(['cancelLogin', 'completeLogin', 'getAuthState', 'onAuthStateChanged', 'signOut', 'startLogin', 'waitForLogin']);
|
|
727
|
+
expect(invoke(remote, 'addClient', { onAuthStateChanged: () => { } })).to.equal(undefined);
|
|
726
728
|
});
|
|
727
729
|
it('delegates the served methods to the backend service', async () => {
|
|
728
730
|
getPasswordStub.resolves(storedCredentials(Date.now() + 3600_000));
|
|
729
|
-
const remote =
|
|
730
|
-
|
|
731
|
-
|
|
731
|
+
const remote = createRemoteAuthService(authService);
|
|
732
|
+
expect(await remote.getAuthState()).to.deep.equal({ isAuthenticated: true, accountLabel: 'user@example.com', planType: 'plus' });
|
|
733
|
+
expect((await remote.startLogin()).authorizationUrl).to.match(/^https:\/\/auth\.openai\.com\/oauth\/authorize\?/);
|
|
732
734
|
await remote.signOut();
|
|
733
|
-
|
|
734
|
-
|
|
735
|
+
expect(deletePasswordStub.calledOnce).to.equal(true);
|
|
736
|
+
expect(await remote.getAuthState()).to.deep.equal({ isAuthenticated: false });
|
|
735
737
|
});
|
|
736
738
|
});
|
|
737
739
|
});
|