ソースを参照

Keep the password change receipt across session rotation

彭峰 13 時間 前
親
コミット
8500be8243

+ 13 - 0
projects/xiaoshu-admin/src/app/cloud-functions.service.spec.ts

@@ -64,6 +64,19 @@ describe('CloudFunctionsService', () => {
     expect(failure).toBeDefined();
   }));
 
+  it('delivers an in-flight password change receipt after another tab signs in', async () => {
+    const url = CLOUD_FUNCTION_BASE_URL + '/xiaoshu/admin/gateway';
+    const pending = service.admin('changeOwnPassword');
+    const assertion = expectAsync(pending).toBeResolvedTo({ reauthenticationRequired: true });
+    const request = http.expectOne(url);
+    const sessions = TestBed.inject(AdminSessionService);
+    const freshSession = { ...sessions.session()!, sessionToken: 'fresh-tab' };
+    spyOn(sessions, 'session').and.returnValue(freshSession);
+
+    request.flush({ success: true, data: { reauthenticationRequired: true } });
+    await assertion;
+  });
+
   it('does not replay a password change when its response is lost', fakeAsync(() => {
     const url = CLOUD_FUNCTION_BASE_URL + '/xiaoshu/admin/gateway';
     let failure: Error | undefined;

+ 8 - 3
projects/xiaoshu-admin/src/app/cloud-functions.service.ts

@@ -67,8 +67,13 @@ export class CloudFunctionsService {
       const identity = stableRequestJson(this.sessions.session());
       const {refresh: _refresh,...cacheParams}=params;
       const key = stableRequestJson([path, identity, cacheParams]);
-      let requestStarted=Date.now();
-      const execute = () => {requestStarted=Date.now();return this.http.post<T>(`${CLOUD_FUNCTION_BASE_URL}/${path}`, { token, params }).pipe(
+      let requestStarted = Date.now();
+      let passwordChangeSent = false;
+      const execute = () => {
+        requestStarted = Date.now();
+        // This write can invalidate its own request token before its receipt arrives.
+        passwordChangeSent = path === 'xiaoshu/admin/gateway' && operation === 'changeOwnPassword';
+        return this.http.post<T>(`${CLOUD_FUNCTION_BASE_URL}/${path}`, { token, params }).pipe(
         timeout({ first: timeoutMs, with: () => throwError(() => new CloudFunctionError(this.transportMessage(readOnly))) }),
         retry({ count: 1, delay: (error) => {
           const vocabularyRead = operation === 'ops/vocabulary/tree' || operation === 'ops/vocabulary/list';
@@ -91,7 +96,7 @@ export class CloudFunctionsService {
       }));};
       const scheduled = this.scheduler.schedule(execute, {
         read: readOnly, key, heavy: /^ops\/(?:lessons\/(?:list|detail)|payroll\/(?:overview|export))$/.test(operation),
-        background: operation === 'ops/live/versions' || operation === 'ops/lessons/export/download', valid: () => stableRequestJson(this.sessions.session()) === identity,
+        background: operation === 'ops/live/versions' || operation === 'ops/lessons/export/download', valid: () => passwordChangeSent || stableRequestJson(this.sessions.session()) === identity,
         cacheMs: value=>['ops/lessons/list', 'ops/vocabulary/tree', 'ops/pay-rates/list', 'ops/teaching-rules/list'].includes(operation)?Math.max(0,((value as {data?:{clientFetchedAt?:number}}).data?.clientFetchedAt??Date.now())+30000-Date.now()):0,
         refresh: params['refresh'] === true,
         cacheable: value => (value as CloudEnvelope<unknown>)?.success === true,