2021-07-16 09:52:51 +02:00
|
|
|
import { adminApi } from "../Services/AdminApi";
|
2022-03-11 17:02:58 +01:00
|
|
|
import { ADMIN_API_URL, DISABLE_ANONYMOUS } from "../Enum/EnvironmentVariable";
|
2021-07-13 19:09:07 +02:00
|
|
|
import { GameRoomPolicyTypes } from "../Model/PusherRoom";
|
2021-12-14 18:55:41 +01:00
|
|
|
import { isMapDetailsData, MapDetailsData } from "../Messages/JsonMessages/MapDetailsData";
|
2021-08-15 22:51:06 +02:00
|
|
|
import { AuthTokenData, jwtTokenManager } from "../Services/JWTTokenManager";
|
2021-12-08 14:46:23 +01:00
|
|
|
import { InvalidTokenError } from "./InvalidTokenError";
|
2022-03-11 17:02:58 +01:00
|
|
|
import { parse } from "query-string";
|
|
|
|
import { BaseHttpController } from "./BaseHttpController";
|
2020-05-09 17:50:47 +02:00
|
|
|
|
2022-03-11 17:02:58 +01:00
|
|
|
export class MapController extends BaseHttpController {
|
2020-05-10 14:48:34 +02:00
|
|
|
// Returns a map mapping map name to file name of the map
|
2022-03-11 17:02:58 +01:00
|
|
|
routes() {
|
|
|
|
/**
|
|
|
|
* @openapi
|
|
|
|
* /map:
|
|
|
|
* get:
|
|
|
|
* description: Returns a map mapping map name to file name of the map
|
|
|
|
* produces:
|
|
|
|
* - "application/json"
|
|
|
|
* parameters:
|
|
|
|
* - name: "playUri"
|
|
|
|
* in: "query"
|
|
|
|
* description: "The full URL of WorkAdventure to load this map"
|
|
|
|
* required: true
|
|
|
|
* type: "string"
|
|
|
|
* - name: "authToken"
|
|
|
|
* in: "query"
|
|
|
|
* description: "The authentication token"
|
|
|
|
* required: true
|
|
|
|
* type: "string"
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: The details of the map
|
|
|
|
* content:
|
|
|
|
* application/json:
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* required:
|
|
|
|
* - mapUrl
|
|
|
|
* - policy_type
|
|
|
|
* - tags
|
|
|
|
* - textures
|
|
|
|
* - authenticationMandatory
|
|
|
|
* - roomSlug
|
|
|
|
* - contactPage
|
|
|
|
* - group
|
|
|
|
* properties:
|
|
|
|
* mapUrl:
|
|
|
|
* type: string
|
|
|
|
* description: The full URL to the JSON map file
|
|
|
|
* example: https://myuser.github.io/myrepo/map.json
|
|
|
|
* policy_type:
|
|
|
|
* type: integer
|
|
|
|
* description: ANONYMOUS_POLICY = 1, MEMBERS_ONLY_POLICY = 2, USE_TAGS_POLICY= 3
|
|
|
|
* example: 1
|
|
|
|
* tags:
|
|
|
|
* type: array
|
|
|
|
* description: The list of tags required to enter this room
|
|
|
|
* items:
|
|
|
|
* type: string
|
|
|
|
* example: speaker
|
|
|
|
* authenticationMandatory:
|
|
|
|
* type: boolean|null
|
|
|
|
* description: Whether the authentication is mandatory or not for this map.
|
|
|
|
* example: true
|
|
|
|
* roomSlug:
|
|
|
|
* type: string
|
|
|
|
* description: The slug of the room
|
|
|
|
* deprecated: true
|
|
|
|
* example: foo
|
|
|
|
* contactPage:
|
|
|
|
* type: string|null
|
|
|
|
* description: The URL to the contact page
|
|
|
|
* example: https://mycompany.com/contact-us
|
|
|
|
* group:
|
|
|
|
* type: string|null
|
|
|
|
* description: The group this room is part of (maps the notion of "world" in WorkAdventure SAAS)
|
|
|
|
* example: myorg/myworld
|
|
|
|
* iframeAuthentication:
|
|
|
|
* type: string|null
|
|
|
|
* description: The URL of the authentication Iframe
|
|
|
|
* example: https://mycompany.com/authc
|
|
|
|
* expireOn:
|
|
|
|
* type: string|undefined
|
|
|
|
* description: The date (in ISO 8601 format) at which the room will expire
|
|
|
|
* example: 2022-11-05T08:15:30-05:00
|
|
|
|
* canReport:
|
|
|
|
* type: boolean|undefined
|
|
|
|
* description: Whether the "report" feature is enabled or not on this room
|
|
|
|
* example: true
|
2022-03-15 15:50:25 +01:00
|
|
|
* loadingLogo:
|
|
|
|
* type: string
|
|
|
|
* description: The URL of the image to be used on the loading page
|
|
|
|
* example: https://example.com/logo.png
|
|
|
|
* loginSceneLogo:
|
|
|
|
* type: string
|
|
|
|
* description: The URL of the image to be used on the LoginScene
|
|
|
|
* example: https://example.com/logo_login.png
|
2022-03-11 17:02:58 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
this.app.get("/map", (req, res) => {
|
|
|
|
const query = parse(req.path_query);
|
2021-07-13 19:09:07 +02:00
|
|
|
if (typeof query.playUri !== "string") {
|
2021-11-24 16:20:07 +01:00
|
|
|
console.error("Expected playUri parameter in /map endpoint");
|
2022-03-11 17:02:58 +01:00
|
|
|
res.status(400);
|
|
|
|
res.send("Expected playUri parameter");
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
2021-07-13 19:09:07 +02:00
|
|
|
|
|
|
|
// If no admin URL is set, let's react on '/_/[instance]/[map url]' URLs
|
|
|
|
if (!ADMIN_API_URL) {
|
|
|
|
const roomUrl = new URL(query.playUri);
|
|
|
|
|
|
|
|
const match = /\/_\/[^/]+\/(.+)/.exec(roomUrl.pathname);
|
|
|
|
if (!match) {
|
2022-03-11 17:02:58 +01:00
|
|
|
res.status(404);
|
|
|
|
res.json({});
|
2021-07-13 19:09:07 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapUrl = roomUrl.protocol + "//" + match[1];
|
|
|
|
|
2022-03-11 17:02:58 +01:00
|
|
|
res.json({
|
|
|
|
mapUrl,
|
|
|
|
policy_type: GameRoomPolicyTypes.ANONYMOUS_POLICY,
|
|
|
|
roomSlug: null, // Deprecated
|
|
|
|
group: null,
|
|
|
|
tags: [],
|
|
|
|
contactPage: null,
|
|
|
|
authenticationMandatory: DISABLE_ANONYMOUS,
|
|
|
|
} as MapDetailsData);
|
2021-07-13 19:09:07 +02:00
|
|
|
|
2020-11-13 12:11:59 +01:00
|
|
|
return;
|
2020-10-13 15:12:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
2020-10-13 15:29:08 +02:00
|
|
|
try {
|
2021-08-15 08:51:35 +02:00
|
|
|
let userId: string | undefined = undefined;
|
|
|
|
if (query.authToken != undefined) {
|
2021-08-15 22:51:06 +02:00
|
|
|
let authTokenData: AuthTokenData;
|
|
|
|
try {
|
|
|
|
authTokenData = jwtTokenManager.verifyJWTToken(query.authToken as string);
|
|
|
|
userId = authTokenData.identifier;
|
|
|
|
} catch (e) {
|
2021-11-16 11:14:27 +01:00
|
|
|
try {
|
|
|
|
// Decode token, in this case we don't need to create new token.
|
|
|
|
authTokenData = jwtTokenManager.verifyJWTToken(query.authToken as string, true);
|
|
|
|
userId = authTokenData.identifier;
|
2021-11-24 16:20:07 +01:00
|
|
|
console.info("JWT expire, but decoded", userId);
|
2021-11-16 11:14:27 +01:00
|
|
|
} catch (e) {
|
2021-12-08 14:46:23 +01:00
|
|
|
if (e instanceof InvalidTokenError) {
|
|
|
|
// The token was not good, redirect user on login page
|
2022-03-11 17:02:58 +01:00
|
|
|
res.status(401);
|
|
|
|
res.send("Token decrypted error");
|
2021-12-08 14:46:23 +01:00
|
|
|
return;
|
|
|
|
} else {
|
2022-03-11 17:02:58 +01:00
|
|
|
this.castErrorToResponse(e, res);
|
|
|
|
return;
|
2021-12-08 14:46:23 +01:00
|
|
|
}
|
2021-11-16 11:14:27 +01:00
|
|
|
}
|
2021-08-15 22:51:06 +02:00
|
|
|
}
|
2021-08-15 08:51:35 +02:00
|
|
|
}
|
2022-04-12 14:21:19 +02:00
|
|
|
const mapDetails = isMapDetailsData.safeParse(
|
|
|
|
await adminApi.fetchMapDetails(query.playUri as string, userId)
|
|
|
|
);
|
2020-10-13 15:29:08 +02:00
|
|
|
|
2022-04-12 14:21:19 +02:00
|
|
|
if (mapDetails.success && DISABLE_ANONYMOUS) {
|
|
|
|
mapDetails.data.authenticationMandatory = true;
|
2021-11-12 16:43:37 +01:00
|
|
|
}
|
|
|
|
|
2022-03-11 17:02:58 +01:00
|
|
|
res.json(mapDetails);
|
|
|
|
return;
|
2020-10-13 15:29:08 +02:00
|
|
|
} catch (e) {
|
2022-03-11 17:02:58 +01:00
|
|
|
this.castErrorToResponse(e, res);
|
2020-10-13 15:29:08 +02:00
|
|
|
}
|
2020-10-13 15:12:24 +02:00
|
|
|
})();
|
2020-05-09 17:50:47 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-10 14:48:34 +02:00
|
|
|
}
|