Merge pull request #3271 from matrix-org/jryans/is-terms-prompt-on-use

Show terms modal when inviting by email
pull/21833/head
J. Ryan Stinnett 2019-08-02 15:41:23 +01:00 committed by GitHub
commit c1f6d82a4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 8 deletions

View File

@ -14,7 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import { SERVICE_TYPES } from 'matrix-js-sdk';
import MatrixClientPeg from './MatrixClientPeg'; import MatrixClientPeg from './MatrixClientPeg';
import { Service, startTermsFlow, TermsNotSignedError } from './Terms';
export default class IdentityAuthClient { export default class IdentityAuthClient {
constructor() { constructor() {
@ -40,24 +43,46 @@ export default class IdentityAuthClient {
if (!token) { if (!token) {
token = await this.registerForToken(); token = await this.registerForToken();
if (token) {
this.accessToken = token; this.accessToken = token;
window.localStorage.setItem("mx_is_access_token", token); window.localStorage.setItem("mx_is_access_token", token);
} }
return token;
}
try { try {
await this._checkToken(token); await this._checkToken(token);
} catch (e) { } catch (e) {
if (e instanceof TermsNotSignedError) {
// Retrying won't help this
throw e;
}
// Retry in case token expired // Retry in case token expired
token = await this.registerForToken(); token = await this.registerForToken();
if (token) {
this.accessToken = token; this.accessToken = token;
window.localStorage.setItem("mx_is_access_token", token); window.localStorage.setItem("mx_is_access_token", token);
} }
}
return token; return token;
} }
async _checkToken(token) { async _checkToken(token) {
try {
await MatrixClientPeg.get().getIdentityAccount(token); await MatrixClientPeg.get().getIdentityAccount(token);
} catch (e) {
if (e.errcode === "M_TERMS_NOT_SIGNED") {
console.log("Identity Server requires new terms to be agreed to");
await startTermsFlow([new Service(
SERVICE_TYPES.IS,
MatrixClientPeg.get().idBaseUrl,
token,
)]);
return;
}
throw e;
}
// We should ensure the token in `localStorage` is cleared // We should ensure the token in `localStorage` is cleared
// appropriately. We already clear storage on sign out, but we'll need // appropriately. We already clear storage on sign out, but we'll need
@ -73,8 +98,8 @@ export default class IdentityAuthClient {
await MatrixClientPeg.get().registerWithIdentityServer(hsOpenIdToken); await MatrixClientPeg.get().registerWithIdentityServer(hsOpenIdToken);
await this._checkToken(identityAccessToken); await this._checkToken(identityAccessToken);
return identityAccessToken; return identityAccessToken;
} catch (err) { } catch (e) {
if (err.cors === "rejected" || err.httpStatus === 404) { if (e.cors === "rejected" || e.httpStatus === 404) {
// Assume IS only supports deprecated v1 API for now // Assume IS only supports deprecated v1 API for now
// TODO: Remove this path once v2 is only supported version // TODO: Remove this path once v2 is only supported version
// See https://github.com/vector-im/riot-web/issues/10443 // See https://github.com/vector-im/riot-web/issues/10443
@ -82,7 +107,7 @@ export default class IdentityAuthClient {
this.authEnabled = false; this.authEnabled = false;
return; return;
} }
console.error(err); throw e;
} }
} }
} }