Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

ToS for ISes/IMs: prompt on use screen #3199

Merged
merged 35 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
54aaaba
Initial support for ToS dialogs for IS/IM
dbkr Jul 9, 2019
d4af8d4
Use showIntegrationsManager in other places
dbkr Jul 9, 2019
83f697a
lint
dbkr Jul 9, 2019
fc706e1
Missed a file
dbkr Jul 10, 2019
189dd4c
SERVICE_TYPES
dbkr Jul 10, 2019
8a7227f
Typing
dbkr Jul 10, 2019
fcf82ef
Bots isn't a proper noun so no real reason for it to get a capital
dbkr Jul 10, 2019
be7680c
update i18n strings
dbkr Jul 10, 2019
a9619b3
missed copyright header
dbkr Jul 10, 2019
4396e99
Missed accessToken
dbkr Jul 10, 2019
0ec57b5
Make 'terms' term less overloaded
dbkr Jul 10, 2019
f7750d9
right doc, wrong function
dbkr Jul 10, 2019
6fafd20
add sample terms response
dbkr Jul 10, 2019
c2977dd
More de-overloading of 'terms'
dbkr Jul 10, 2019
994f8f8
Remove redundant dict key
dbkr Jul 10, 2019
72b1ad3
Remove random space
dbkr Jul 10, 2019
06c0bce
These can be pure components
dbkr Jul 10, 2019
8de5c34
focus is a bit silly if its starts disabled
dbkr Jul 10, 2019
0316aa1
Rest of terms/policies renaming
dbkr Jul 10, 2019
f4be4ab
Re-add logic for if no integ url is configured
dbkr Jul 10, 2019
90a0f93
jsdoc-ify comment block
dbkr Jul 10, 2019
f77e7fc
Use m.accepted_terms account data
dbkr Jul 11, 2019
3ab5acd
Add unit test for terms agreement flow
dbkr Jul 11, 2019
6b81532
apparently I was doing array bracket spacing wrong
dbkr Jul 11, 2019
18dde85
s/terms/policies/
dbkr Jul 11, 2019
99d1ed5
s/terms/policies/
dbkr Jul 11, 2019
69fa34d
Fix ScalarAuthClient to refresh tokens if they fail
dbkr Jul 11, 2019
e6fdff4
unsused variable
dbkr Jul 11, 2019
f13dc82
getTerms doesn't need an access token
dbkr Jul 11, 2019
7c43f0b
Don't retry on terms error
dbkr Jul 15, 2019
11ecb4c
s/terms /policies/
dbkr Jul 22, 2019
b664259
more syntactic sugar
dbkr Jul 22, 2019
84bb0eb
Remove random capital
dbkr Jul 22, 2019
7d78782
Strip path component from IM rest url
dbkr Jul 22, 2019
1b0d851
Add note from commit message as a comment
dbkr Jul 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions res/css/_components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
@import "./views/dialogs/_SetPasswordDialog.scss";
@import "./views/dialogs/_SettingsDialog.scss";
@import "./views/dialogs/_ShareDialog.scss";
@import "./views/dialogs/_TermsDialog.scss";
@import "./views/dialogs/_UnknownDeviceDialog.scss";
@import "./views/dialogs/_UploadConfirmDialog.scss";
@import "./views/dialogs/_UserSettingsDialog.scss";
Expand Down
35 changes: 35 additions & 0 deletions res/css/views/dialogs/_TermsDialog.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
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.
*/

.mx_TermsDialog_termsTableHeader {
font-weight: bold;
text-align: left;
}

.mx_TermsDialog_termsTable {
font-size: 12px;
}

.mx_TermsDialog_service, .mx_TermsDialog_summary {
padding-right: 10px;
}

.mx_TermsDialog_link {
mask-image: url('$(res)/img/external-link.svg');
background-color: $accent-color;
width: 10px;
height: 10px;
}
9 changes: 3 additions & 6 deletions src/FromWidgetPostMessageApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import URL from 'url';
import dis from './dispatcher';
import WidgetMessagingEndpoint from './WidgetMessagingEndpoint';
import ActiveWidgetStore from './stores/ActiveWidgetStore';
import sdk from "./index";
import Modal from "./Modal";
import MatrixClientPeg from "./MatrixClientPeg";
import RoomViewStore from "./stores/RoomViewStore";
import { showIntegrationsManager } from './integrations/integrations';
dbkr marked this conversation as resolved.
Show resolved Hide resolved

const WIDGET_API_VERSION = '0.0.2'; // Current API version
const SUPPORTED_WIDGET_API_VERSIONS = [
Expand Down Expand Up @@ -193,13 +192,11 @@ export default class FromWidgetPostMessageApi {
const integType = (data && data.integType) ? data.integType : null;
const integId = (data && data.integId) ? data.integId : null;

// The dialog will take care of scalar auth for us
const IntegrationsManager = sdk.getComponent("views.settings.IntegrationsManager");
Modal.createTrackedDialog('Integrations Manager', '', IntegrationsManager, {
showIntegrationsManager({
room: MatrixClientPeg.get().getRoom(RoomViewStore.getRoomId()),
screen: 'type_' + integType,
integrationId: integId,
}, "mx_IntegrationsManager");
});
} else if (action === 'set_always_on_screen') {
// This is a new message: there is no reason to support the deprecated widgetData here
const data = event.data.data;
Expand Down
48 changes: 33 additions & 15 deletions src/ScalarAuthClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
Copyright 2016 OpenMarket Ltd
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.
Expand All @@ -16,11 +17,14 @@ limitations under the License.

import Promise from 'bluebird';
import SettingsStore from "./settings/SettingsStore";
import { Service, presentTermsForServices, TermsNotSignedError } from './Terms';
const request = require('browser-request');

const SdkConfig = require('./SdkConfig');
const MatrixClientPeg = require('./MatrixClientPeg');

import * as Matrix from 'matrix-js-sdk';

// The version of the integration manager API we're intending to work with
const imApiVersion = "1.1";

Expand Down Expand Up @@ -55,23 +59,11 @@ class ScalarAuthClient {
if (!token) {
return this.registerForToken();
} else {
return this.validateToken(token).then(userId => {
const me = MatrixClientPeg.get().getUserId();
if (userId !== me) {
throw new Error("Scalar token is owned by someone else: " + me);
}
return token;
}).catch(err => {
console.error(err);

// Something went wrong - try to get a new token.
dbkr marked this conversation as resolved.
Show resolved Hide resolved
console.warn("Registering for new scalar token");
return this.registerForToken();
});
return this._checkToken(token);
}
}

validateToken(token) {
_getAccountName(token) {
const url = SdkConfig.get().integrations_rest_url + "/account";

return new Promise(function(resolve, reject) {
Expand All @@ -83,8 +75,10 @@ class ScalarAuthClient {
}, (err, response, body) => {
if (err) {
reject(err);
} else if (body && body.errcode === 'M_TERMS_NOT_SIGNED') {
reject(new TermsNotSignedError());
} else if (response.statusCode / 100 !== 2) {
reject({statusCode: response.statusCode});
reject(body);
} else if (!body || !body.user_id) {
reject(new Error("Missing user_id in response"));
} else {
Expand All @@ -94,11 +88,35 @@ class ScalarAuthClient {
});
}

_checkToken(token) {
return this._getAccountName(token).then(userId => {
const me = MatrixClientPeg.get().getUserId();
if (userId !== me) {
throw new Error("Scalar token is owned by someone else: " + me);
}
return token;
}).catch((e) => {
if (e instanceof TermsNotSignedError) {
console.log("Integrations manager requires new terms to be agreed to");
return presentTermsForServices([new Service(
Matrix.SERVICE_TYPES.IM,
SdkConfig.get().integrations_rest_url,
token,
)]).then(() => {
return token;
});
}
});
}

registerForToken() {
// Get openid bearer token from the HS as the first part of our dance
return MatrixClientPeg.get().getOpenIdToken().then((tokenObject) => {
// Now we can send that to scalar and exchange it for a scalar token
return this.exchangeForScalarToken(tokenObject);
}).then((tokenObject) => {
// Validate it (this mostly checks to see if the IM needs us to agree to some terms)
return this._checkToken(tokenObject);
}).then((tokenObject) => {
window.localStorage.setItem("mx_scalar_token", tokenObject);
return tokenObject;
Expand Down
109 changes: 109 additions & 0 deletions src/Terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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 Promise from 'bluebird';

import MatrixClientPeg from './MatrixClientPeg';
import sdk from './';
import Modal from './Modal';

export class TermsNotSignedError extends Error {}

/**
* Class representing a service that may have terms & conditions that
* require agreement fro mthe user before the user can use that service.
dbkr marked this conversation as resolved.
Show resolved Hide resolved
*/
export class Service {
/**
* @param {MatrixClient.SERVICE_TYPES} serviceType The type of service
* @param {string} baseUrl The Base URL of the service (ie. before '/_matrix')
* @param {string} accessToken The user's access token for the service
*/
constructor(serviceType, baseUrl, accessToken) {
this.serviceType = serviceType;
this.baseUrl = baseUrl;
this.accessToken = accessToken;
}
}

/**
* Present a popup to the user prompting them to agree to terms and conditions
*
* @param {Service[]} services Object with keys 'servicetype', 'baseurl', '
dbkr marked this conversation as resolved.
Show resolved Hide resolved
* @param {function} dialogTermsInteractionCallback Function called with an array of:
dbkr marked this conversation as resolved.
Show resolved Hide resolved
* { service: {Service}, terms: {terms response from API} }
* Must return a Promise which resolves with a list of URLs of documents agreed to
* @returns {Promise} resolves when the user agreed to all necessary terms or rejects
* if they cancel.
*/
export function presentTermsForServices(services) {
return startTermsFlow(services, dialogTermsInteractionCallback);
}

export async function startTermsFlow(services, interactionCallback) {
dbkr marked this conversation as resolved.
Show resolved Hide resolved
const termsPromises = services.map(
dbkr marked this conversation as resolved.
Show resolved Hide resolved
(s) => MatrixClientPeg.get().getTerms(s.serviceType, s.baseUrl, s.accessToken),
);

const terms = await Promise.all(termsPromises);
const termsAndServices = terms.map((t, i) => { return { 'service': services[i], 'terms': t.policies }; });
dbkr marked this conversation as resolved.
Show resolved Hide resolved

const agreedUrls = await interactionCallback(termsAndServices);
console.log("User has agreed to URLs", agreedUrls);

const agreePromises = termsAndServices.map((termsAndService) => {
// filter the agreed URL list for ones that are actually for this service
// (one URL may be used for multiple services)
// Not a particularly efficient loop but probably fine given the numbers involved
const urlsForService = agreedUrls.filter((url) => {
for (const terms of Object.values(termsAndService.terms)) {
dbkr marked this conversation as resolved.
Show resolved Hide resolved
for (const lang of Object.keys(terms)) {
if (lang === 'version') continue;
if (terms[lang].url === url) return true;
}
}
return false;
});

if (urlsForService.length === 0) return Promise.resolve();

return MatrixClientPeg.get().agreeToTerms(
jryans marked this conversation as resolved.
Show resolved Hide resolved
termsAndService.service.serviceType,
termsAndService.service.baseUrl,
termsAndService.service.accessToken,
urlsForService,
);
});
return Promise.all(agreePromises);
}

function dialogTermsInteractionCallback(termsWithServices) {
return new Promise((resolve, reject) => {
console.log("Terms that need agreement", termsWithServices);
const TermsDialog = sdk.getComponent("views.dialogs.TermsDialog");

Modal.createTrackedDialog('Terms of Service', '', TermsDialog, {
termsWithServices: termsWithServices,
dbkr marked this conversation as resolved.
Show resolved Hide resolved
onFinished: (done, agreedUrls) => {
if (!done) {
reject(new TermsNotSignedError());
return;
}
resolve(agreedUrls);
},
});
});
}
Loading