From 69fa34d71f38f98c4090080f2b4427dc46dc2b80 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 11 Jul 2019 16:00:24 +0100 Subject: [PATCH] Fix ScalarAuthClient to refresh tokens if they fail Also add a test to make sure it does it --- src/ScalarAuthClient.js | 8 +++-- test/ScalarAuthClient-test.js | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/ScalarAuthClient-test.js diff --git a/src/ScalarAuthClient.js b/src/ScalarAuthClient.js index decd059dd3..65d033f97b 100644 --- a/src/ScalarAuthClient.js +++ b/src/ScalarAuthClient.js @@ -51,7 +51,7 @@ class ScalarAuthClient { return this.scalarToken != null; // undef or null } - // Returns a scalar_token string + // Returns a promise that resolves to a scalar_token string getScalarToken() { let token = this.scalarToken; if (!token) token = window.localStorage.getItem("mx_scalar_token"); @@ -59,7 +59,9 @@ class ScalarAuthClient { if (!token) { return this.registerForToken(); } else { - return this._checkToken(token); + return this._checkToken(token).catch(() => { + return this.registerForToken(); + }); } } @@ -105,6 +107,8 @@ class ScalarAuthClient { )]).then(() => { return token; }); + } else { + throw e; } }); } diff --git a/test/ScalarAuthClient-test.js b/test/ScalarAuthClient-test.js new file mode 100644 index 0000000000..290cbcc741 --- /dev/null +++ b/test/ScalarAuthClient-test.js @@ -0,0 +1,57 @@ +/* +Copyright 2019 The Matrix.org Foundation C.I.C. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import expect from 'expect'; + +import sinon from 'sinon'; + +import ScalarAuthClient from '../src/ScalarAuthClient'; +import MatrixClientPeg from '../src/MatrixClientPeg'; +import { stubClient } from './test-utils'; + +describe('ScalarAuthClient', function() { + let clientSandbox; + let sac; + + beforeEach(function() { + sinon.stub(window.localStorage, 'getItem').withArgs('mx_scalar_token').returns('brokentoken'); + clientSandbox = stubClient(); + }); + + afterEach(function() { + clientSandbox.restore(); + sinon.restore(); + }); + + it('should request a new token if the old one fails', async function() { + const sac = new ScalarAuthClient(); + + sac._getAccountName = sinon.stub(); + sac._getAccountName.withArgs('brokentoken').rejects({ + message: "Invalid token", + }); + sac._getAccountName.withArgs('wokentoken').resolves(MatrixClientPeg.get().getUserId()); + + MatrixClientPeg.get().getOpenIdToken = sinon.stub().resolves('this is your openid token'); + + sac.exchangeForScalarToken = sinon.stub().withArgs('this is your openid token').resolves('wokentoken'); + + await sac.connect(); + + expect(sac.exchangeForScalarToken.calledWith('this is your openid token')).toBeTruthy(); + expect(sac.scalarToken).toEqual('wokentoken'); + }); +});