Logout upstream as well (best effort)

This commit is contained in:
Travis Ralston 2019-07-10 21:26:22 -06:00
parent 18ee47fa49
commit c96366b647
3 changed files with 19 additions and 7 deletions

View File

@ -112,9 +112,13 @@ export default class AccountController {
* @returns {Promise<*>} Resolves when complete.
*/
public async logout(user: IMSCUser): Promise<any> {
// TODO: Create a link to upstream tokens to log them out too
const tokens = await UserScalarToken.findAll({where: {scalarToken: user.token}});
const tokens = await UserScalarToken.findAll({where: {scalarToken: user.token}, include: [Upstream]});
for (const token of tokens) {
if (token.upstream) {
LogService.info("AccountController", "Logging user out of upstream");
const client = new ScalarClient(token.upstream, ScalarClient.KIND_MATRIX_V1);
await client.logout(token.scalarToken);
}
await token.destroy();
}
Cache.for(CACHE_SCALAR_ACCOUNTS).clear();

View File

@ -1,5 +1,11 @@
import {
AllowNull, AutoIncrement, BelongsTo, Column, ForeignKey, Model, PrimaryKey,
AllowNull,
AutoIncrement,
BelongsTo,
Column,
ForeignKey,
Model,
PrimaryKey,
Table
} from "sequelize-typescript";
import User from "./User";
@ -33,4 +39,7 @@ export default class UserScalarToken extends Model<UserScalarToken> {
@Column
@ForeignKey(() => Upstream)
upstreamId?: number;
@BelongsTo(() => Upstream)
upstream: Upstream;
}

View File

@ -4,7 +4,6 @@ import * as request from "request";
import { LogService } from "matrix-js-snippets";
import Upstream from "../db/models/Upstream";
import { SCALAR_API_VERSION } from "../utils/common-constants";
import * as url from "url";
const REGISTER_ROUTE = "/register";
const ACCOUNT_INFO_ROUTE = "/account";
@ -30,8 +29,8 @@ export class ScalarClient {
},
};
} else {
const parsed = url.parse(this.upstream.scalarUrl);
parsed.path = '/_matrix/integrations/v1' + (path === ACCOUNT_INFO_ROUTE ? path : `${ACCOUNT_INFO_ROUTE}${path}`);
const parsed = new URL(this.upstream.scalarUrl);
parsed.pathname = '/_matrix/integrations/v1' + (path === ACCOUNT_INFO_ROUTE ? path : `${ACCOUNT_INFO_ROUTE}${path}`);
const headers = {};
if (token) headers['Authorization'] = `Bearer ${token}`;
@ -110,7 +109,7 @@ export class ScalarClient {
LogService.error("ScalarClient", err);
reject(err);
} else if (res.statusCode !== 200) {
LogService.error("ScalarClient", "Got status code " + res.statusCode + " while getting information for token");
LogService.error("ScalarClient", "Got status code " + res.statusCode + " while logging out token");
reject(res.statusCode);
} else {
resolve(res.body);