Skip to content

Commit

Permalink
🛠 Multiple fixes (linagora#2009)
Browse files Browse the repository at this point in the history
* Avoid sending "members" in api response for non direct channels

* Fix mobile redirection again

* Fix css

* Fix bug

* Fix snake case / camel case

* Fix tests for applications
  • Loading branch information
RomaricMourgues authored Mar 10, 2022
1 parent 8898eee commit 75d200d
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 61 deletions.
12 changes: 6 additions & 6 deletions twake/backend/node/src/cli/cmds/migration_cmds/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,16 @@ export const importDepreciatedFields = (application: PhpApplication): Applicatio
//@ts-ignore
newApplication.stats = newApplication.stats || {};
newApplication.stats.version = 1;
newApplication.stats.createdAt = Date.now();
newApplication.stats.updatedAt = Date.now();
newApplication.stats.created_at = Date.now();
newApplication.stats.updated_at = Date.now();
}

if (!newApplication.api?.privateKey) {
if (!newApplication.api?.private_key) {
//@ts-ignore
newApplication.api = newApplication.api || {};
newApplication.api.hooksUrl = application.depreciated_api_events_url;
newApplication.api.allowedIps = application.depreciated_api_allowed_ip;
newApplication.api.privateKey = application.depreciated_api_private_key;
newApplication.api.hooks_url = application.depreciated_api_events_url;
newApplication.api.allowed_ips = application.depreciated_api_allowed_ip;
newApplication.api.private_key = application.depreciated_api_private_key;
}

if (newApplication.access?.write === undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default {
categories: entity.identity.categories,
compatibility: entity.identity.compatibility,
published: entity.publication.published,
createdAt: entity.stats.createdAt,
created_at: entity.stats.created_at,
};
},
mongoMapping: {
Expand All @@ -27,7 +27,7 @@ export default {
categories: { type: "keyword" },
compatibility: { type: "keyword" },
published: { type: "boolean" },
createdAt: { type: "number" },
created_at: { type: "number" },
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ export type ApplicationPublication = {
};

export type ApplicationStatistics = {
createdAt: number; // RO
updatedAt: number; // RO
created_at: number; // RO
updated_at: number; // RO
version: number; // RO
};

export type ApplicationApi = {
hooksUrl: string;
allowedIps: string;
privateKey: string; // RO
hooks_url: string;
allowed_ips: string;
private_key: string; // RO
};

type ApplicationScopes =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ class ApplicationService implements MarketplaceApplicationServiceAPI {
throw CrudException.notFound("Application not found");
}

if (!app.api.hooksUrl) {
throw CrudException.badRequest("Application hooksUrl is not defined");
if (!app.api.hooks_url) {
throw CrudException.badRequest("Application hooks_url is not defined");
}

const payload = {
Expand All @@ -197,12 +197,12 @@ class ApplicationService implements MarketplaceApplicationServiceAPI {
};

const signature = crypto
.createHmac("sha256", app.api.privateKey)
.createHmac("sha256", app.api.private_key)
.update(JSON.stringify(payload))
.digest("hex");

return await axios
.post(app.api.hooksUrl, payload, {
.post(app.api.hooks_url, payload, {
headers: {
"Content-Type": "application/json",
"X-Twake-Signature": signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ export class ApplicationController
}

entity.identity = app.identity;
entity.api.hooksUrl = app.api.hooksUrl;
entity.api.allowedIps = app.api.allowedIps;
entity.api.hooks_url = app.api.hooks_url;
entity.api.allowed_ips = app.api.allowed_ips;
entity.access = app.access;
entity.display = app.display;

entity.stats.updatedAt = now;
entity.stats.updated_at = now;
entity.stats.version++;

const res = await this.service.applications.save(entity);
Expand All @@ -124,11 +124,11 @@ export class ApplicationController

app.is_default = false;
app.publication.published = false;
app.api.privateKey = randomBytes(32).toString("base64");
app.api.private_key = randomBytes(32).toString("base64");

app.stats = {
createdAt: now,
updatedAt: now,
created_at: now,
updated_at: now,
version: 0,
};

Expand Down
14 changes: 7 additions & 7 deletions twake/backend/node/src/services/applications/web/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ const responseApplicationPublication = {
const applicationStats = {
type: "object",
properties: {
createdAt: { type: "number" },
updatedAt: { type: "number" },
created_at: { type: "number" },
updated_at: { type: "number" },
version: { type: "number" },
},
required: ["createdAt", "updatedAt", "version"],
required: ["created_at", "updated_at", "version"],
};

const apiObject = {
type: "object",
properties: {
hooksUrl: { type: "string" },
allowedIps: { type: "string" },
privateKey: { type: "string" },
hooks_url: { type: "string" },
allowed_ips: { type: "string" },
private_key: { type: "string" },
},
required: ["hooksUrl", "allowedIps"],
required: ["hooks_url", "allowed_ips"],
};

const requestApplicationObject = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expandStringForPrefix } from "../../../core/platform/services/search/adapters/utils";
import { Channel } from "./channel";

export default {
index: "user",
source: (entity: Channel) => {
let source: any = {
workspace_id: entity.workspace_id,
company_id: entity.company_id,
name: entity.channel_group + " " + entity.name,
};
return source;
},
mongoMapping: {
text: {
name: "text",
},
},
esMapping: {
properties: {
name: { type: "text", index_prefixes: {} },
workspace_id: { type: "keyword" },
company_id: { type: "keyword" },
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class Channel {
@Type(() => String)
owner: string;

//This is only used for direct channels
@Column("members", "encoded_json")
members: string[] = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ export class Service implements ChannelService {

logger.info("Saving channel %o", channelToSave);
await this.channelRepository.save(channelToSave);

if (!isDirectChannel) {
channel.members = []; //Members is specific to direct channels
}

const saveResult = new SaveResult<ChannelObject>(
"channel",
{
Expand Down
34 changes: 17 additions & 17 deletions twake/backend/node/test/e2e/application/app-create-update.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,25 @@ describe("Applications", () => {
expect(r.display).toMatchObject(payload.display);
expect(r.publication).toMatchObject(payload.publication);
expect(r.stats).toMatchObject({
createdAt: expect.any(Number),
updatedAt: expect.any(Number),
created_at: expect.any(Number),
updated_at: expect.any(Number),
version: 0,
});

expect(r.api).toMatchObject({
hooksUrl: payload.api.hooksUrl,
allowedIps: payload.api.allowedIps,
privateKey: expect.any(String),
hooks_url: payload.api.hooks_url,
allowed_ips: payload.api.allowed_ips,
private_key: expect.any(String),
});

expect(r.api.privateKey).not.toBe("");
expect(r.api.private_key).not.toBe("");

const dbData = await appRepo.findOne({ id: response.resource.id });

expect(dbData.api).toMatchObject({
allowedIps: payload.api.allowedIps,
hooksUrl: payload.api.hooksUrl,
privateKey: expect.any(String),
allowed_ips: payload.api.allowed_ips,
hooks_url: payload.api.hooks_url,
private_key: expect.any(String),
});

done();
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("Applications", () => {

payload.is_default = true;
payload.identity.name = "test2";
payload.api.hooksUrl = "123123";
payload.api.hooks_url = "123123";
payload.access.read = [];
payload.publication.requested = true;

Expand All @@ -144,8 +144,8 @@ describe("Applications", () => {
expect(r.display).toMatchObject(payload.display);
expect(r.publication).toMatchObject(payload.publication);
expect(r.stats).toMatchObject({
createdAt: expect.any(Number),
updatedAt: expect.any(Number),
created_at: expect.any(Number),
updated_at: expect.any(Number),
version: 1,
});

Expand All @@ -154,9 +154,9 @@ describe("Applications", () => {
const dbData = await appRepo.findOne({ id: response.resource.id });

expect(dbData.api).toMatchObject({
allowedIps: payload.api.allowedIps,
hooksUrl: payload.api.hooksUrl,
privateKey: expect.any(String),
allowed_ips: payload.api.allowed_ips,
hooks_url: payload.api.hooks_url,
private_key: expect.any(String),
});

done();
Expand Down Expand Up @@ -278,8 +278,8 @@ const postPayload = {
compatibility: [],
},
api: {
hooksUrl: "hooksUrl",
allowedIps: "allowedIps",
hooks_url: "hooks_url",
allowed_ips: "allowed_ips",
},
access: {
read: ["messages"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("Application events", () => {
);

appId = createdApplication.resource.id;
signingSecret = createdApplication.resource.api.privateKey;
signingSecret = createdApplication.resource.api.private_key;

ends();

Expand Down Expand Up @@ -84,8 +84,8 @@ const postPayload = {
compatibility: [],
},
api: {
hooksUrl: "http://localhost:3000/test/appHook",
allowedIps: "allowedIps",
hooks_url: "http://localhost:3000/test/appHook",
allowed_ips: "allowed_ips",
},
access: {
read: ["messages"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("Applications", () => {
let testDbService: TestDbService;
let api: Api;
let appId: string;
let privateKey: string;
let private_key: string;
let accessToken: ApplicationLoginResponse["access_token"];

beforeAll(async ends => {
Expand All @@ -29,7 +29,7 @@ describe("Applications", () => {
);

appId = createdApplication.resource.id;
privateKey = createdApplication.resource.api.privateKey;
private_key = createdApplication.resource.api.private_key;

ends();
});
Expand All @@ -44,7 +44,7 @@ describe("Applications", () => {

const response = await api.post("/api/console/v1/login", {
id: appId,
secret: privateKey,
secret: private_key,
});
expect(response.statusCode).toBe(200);

Expand Down Expand Up @@ -127,8 +127,8 @@ const postPayload = {
compatibility: [],
},
api: {
hooksUrl: "hooksUrl",
allowedIps: "allowedIps",
hooks_url: "hooks_url",
allowed_ips: "allowed_ips",
},
access: {
read: ["messages"],
Expand Down
4 changes: 4 additions & 0 deletions twake/frontend/src/app/features/auth/jwt-storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class JWTStorage {
}

isAccessExpired() {
if (!this.jwtData?.expiration) return true;

const expired = new Date().getTime() / 1000 - this.jwtData.expiration > 0;

expired &&
Expand All @@ -109,6 +111,8 @@ class JWTStorage {
}

isRefreshExpired() {
if (!this.jwtData?.expiration) return true;

const expired = new Date().getTime() / 1000 - this.jwtData.refresh_expiration > 0;

expired &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Api from 'app/features/global/framework/api-service';
import { TwakeService } from 'app/features/global/framework/registry-decorator-service';
import { JWTDataType } from 'app/features/auth/jwt-storage-service';
import JWTStorage, { JWTDataType } from 'app/features/auth/jwt-storage-service';

type LoginParams = {
email: string;
Expand Down Expand Up @@ -44,11 +44,15 @@ class ConsoleAPIClient {
Api.post<
undefined,
{ access_token: JWTDataType; message: string; error: string; statusCode: number }
>('/internal/services/console/v1/token', undefined, response =>
>('/internal/services/console/v1/token', undefined, response => {
if (JWTStorage.isRefreshExpired() && JWTStorage.isAccessExpired()) {
reject(new Error('Can not get access token'));
return;
}
response.access_token
? resolve(response.access_token)
: reject(new Error('Can not get access token')),
);
: reject(new Error('Can not get access token'));
});
});
}
}
Expand Down
Loading

0 comments on commit 75d200d

Please sign in to comment.