@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
95 lines • 3.54 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2020 Ericsson and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import * as cookie from 'cookie';
import * as crypto from 'crypto';
import { injectable, postConstruct, inject, named } from 'inversify';
import { isObject, isString } from '../../common/index.js';
import { ElectronSecurityToken } from '../../electron-common/electron-token.js';
import { ILogger } from '../../common/logger.js';
/**
* On Electron, we want to make sure that only Electron's browser-windows access the backend services.
*/
let ElectronTokenValidator = class ElectronTokenValidator {
init() {
this.electronSecurityToken = this.getToken();
}
allowWsUpgrade(request) {
return this.allowRequest(request);
}
/**
* Expects the token to be passed via cookies by default.
*/
allowRequest(request) {
if (!this.electronSecurityToken) {
return true;
}
const cookieHeader = request.headers.cookie;
if (isString(cookieHeader)) {
const token = cookie.parse(cookieHeader)[ElectronSecurityToken];
if (isString(token)) {
return this.isTokenValid(JSON.parse(token));
}
}
return false;
}
/**
* Validates a token.
*
* This method both checks the shape of the parsed token data and its actual value.
*
* @param token Parsed object sent by the client as the token.
*/
isTokenValid(token) {
if (isObject(token) && isString(token.value)) {
try {
const received = Buffer.from(token.value, 'utf8');
const expected = Buffer.from(this.electronSecurityToken.value, 'utf8');
return received.byteLength === expected.byteLength && crypto.timingSafeEqual(received, expected);
}
catch (error) {
this.logger.error(error);
}
}
return false;
}
/**
* Returns the token to compare to when authorizing requests.
*/
getToken() {
const token = process.env[ElectronSecurityToken];
if (token) {
return JSON.parse(token);
}
else {
// No token has been passed to the backend server
// That indicates we're running without a local frontend
return undefined;
}
}
};
__decorate([
inject(ILogger),
named('core:ElectronTokenValidator')
], ElectronTokenValidator.prototype, "logger", void 0);
__decorate([
postConstruct()
], ElectronTokenValidator.prototype, "init", null);
ElectronTokenValidator = __decorate([
injectable()
], ElectronTokenValidator);
export { ElectronTokenValidator };
//# sourceMappingURL=electron-token-validator.js.map