Skip to content

Commit

Permalink
RD-1291 Introduced TS to backend (#1575)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubama authored Sep 7, 2021
1 parent aeda175 commit 1b7bf59
Show file tree
Hide file tree
Showing 131 changed files with 3,404 additions and 2,879 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"overrides": [
{
"files": ["./*.js", "scripts/*.js"],
"files": ["./*.{js,ts}", "scripts/*.js"],
"rules": {
"import/no-extraneous-dependencies": [
"error",
Expand Down
15 changes: 12 additions & 3 deletions backend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
{
"extends": [
"../node_modules/cloudify-ui-common/configs/eslint-common-node.json"
"plugin:node/recommended-module",
"../.eslintrc.temp-overrides.json"
],
"rules": {
"prefer-promise-reject-errors": "off"
}
"prefer-promise-reject-errors": "off",
"node/no-missing-require": ["error", {
"tryExtensions": [".js", ".ts", ".json"]
}],
"node/no-missing-import": ["error", {"tryExtensions": [".js", ".json", ".ts"]}]
},
"overrides": [{
"files": "**/*.js",
"extends": ["../node_modules/cloudify-ui-common/configs/eslint-common-node.json"]
}]
}
13 changes: 13 additions & 0 deletions backend/__mocks__/passport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { RequestHandler } from 'express';
import { noop } from 'lodash';

const middlewareMock: RequestHandler = (req, _res, next) => {
req.user = { username: 'testuser' };
next();
};

export default {
authenticate: () => middlewareMock,
initialize: () => middlewareMock,
use: noop
};
85 changes: 43 additions & 42 deletions backend/app.js → backend/app.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
const fs = require('fs');
const path = require('path');
const expressStaticGzip = require('express-static-gzip');
const express = require('express');
const passport = require('passport');
const cookieParser = require('cookie-parser');
const morgan = require('morgan');

const config = require('./config');
const Consts = require('./consts');
const LoggerHandler = require('./handler/LoggerHandler');
const ServerSettings = require('./serverSettings');
const { getResourcePath } = require('./utils');

const getCookieStrategy = require('./auth/CookieStrategy');
const getTokenStrategy = require('./auth/TokenStrategy');
const getSamlStrategy = require('./auth/SamlStrategy');
const samlSetup = require('./samlSetup');
const Auth = require('./routes/Auth');

const Applications = require('./routes/Applications');
const BlueprintAdditions = require('./routes/BlueprintAdditions');
const BlueprintUserData = require('./routes/BlueprintUserData');
const ClientConfig = require('./routes/ClientConfig');
const External = require('./routes/External');
const File = require('./routes/File');
const GitHub = require('./routes/GitHub');
const Maps = require('./routes/Maps');
const Plugins = require('./routes/Plugins');
const ServerProxy = require('./routes/ServerProxy');
const SourceBrowser = require('./routes/SourceBrowser');
const Style = require('./routes/Style');
const Templates = require('./routes/Templates');
const UserApp = require('./routes/UserApp');
const WidgetBackend = require('./routes/WidgetBackend');
const Widgets = require('./routes/Widgets');
const Filters = require('./routes/Filters');
// @ts-nocheck File not migrated fully to TS
import fs from 'fs';
import path from 'path';
import expressStaticGzip from 'express-static-gzip';
import express from 'express';
import passport from 'passport';
import cookieParser from 'cookie-parser';
import morgan from 'morgan';

import { getConfig, getClientConfig } from './config';
import { CONTEXT_PATH } from './consts';
import LoggerHandler from './handler/LoggerHandler';
import { getMode } from './serverSettings';
import { getResourcePath } from './utils';

import getCookieStrategy from './auth/CookieStrategy';
import getTokenStrategy from './auth/TokenStrategy';
import getSamlStrategy from './auth/SamlStrategy';
import validateSamlConfig from './samlSetup';
import Auth from './routes/Auth';

import Applications from './routes/Applications';
import BlueprintAdditions from './routes/BlueprintAdditions';
import BlueprintUserData from './routes/BlueprintUserData';
import ClientConfig from './routes/ClientConfig';
import External from './routes/External';
import File from './routes/File';
import GitHub from './routes/GitHub';
import Maps from './routes/Maps';
import Plugins from './routes/Plugins';
import ServerProxy from './routes/ServerProxy';
import SourceBrowser from './routes/SourceBrowser';
import Style from './routes/Style';
import Templates from './routes/Templates';
import UserApp from './routes/UserApp';
import WidgetBackend from './routes/WidgetBackend';
import Widgets from './routes/Widgets';
import Filters from './routes/Filters';

const logger = LoggerHandler.getLogger('App');
const contextPath = Consts.CONTEXT_PATH;
const contextPath = CONTEXT_PATH;
const oldContextPath = '/stage';
const app = express();

Expand Down Expand Up @@ -66,9 +67,9 @@ app.use(contextPath, (req, res, next) => {
next();
});

const samlConfig = config.get().app.saml;
const samlConfig = getConfig().app.saml;
if (samlConfig.enabled) {
samlSetup.validate(samlConfig);
validateSamlConfig(samlConfig);
passport.use(getSamlStrategy());
}

Expand Down Expand Up @@ -116,7 +117,7 @@ app.use(`${contextPath}/github`, GitHub);
app.use(`${contextPath}/external`, External);
app.use(`${contextPath}/file`, File);
app.use(`${contextPath}/config`, (req, res) => {
res.send(config.getForClient(ServerSettings.settings.mode));
res.send(getClientConfig(getMode()));
});
app.use(`${contextPath}/wb`, WidgetBackend);
app.use(`${contextPath}/plugins`, Plugins);
Expand Down Expand Up @@ -155,4 +156,4 @@ app.use((err, req, res, next) => {
res.status(err.status || 404).send({ message: message || err });
});

module.exports = app;
export default app;
15 changes: 0 additions & 15 deletions backend/auth/CookieStrategy.js

This file was deleted.

12 changes: 12 additions & 0 deletions backend/auth/CookieStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-nocheck File not migrated fully to TS
import { Strategy } from 'passport-cookie';
import { getUser } from '../handler/AuthHandler';
import { TOKEN_COOKIE_NAME } from '../consts';

export default () => {
return new Strategy({ cookieName: TOKEN_COOKIE_NAME }, (token, done) =>
getUser(token)
.then(user => done(null, user))
.catch(err => done(null, false, err + token))
);
};
25 changes: 0 additions & 25 deletions backend/auth/SamlStrategy.js

This file was deleted.

23 changes: 23 additions & 0 deletions backend/auth/SamlStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// @ts-nocheck File not migrated fully to TS

import { Strategy } from 'passport-saml';
import fs from 'fs';
import { getConfig } from '../config';

export default () => {
let cert;
try {
cert = fs.readFileSync(getConfig().app.saml.certPath, 'utf-8');
} catch (e) {
throw new Error('Could not read SAML certificate [saml.certPath]', e);
}

return new Strategy(
{
path: '/auth/saml/callback',
entryPoint: getConfig().app.saml.ssoUrl,
cert
},
(user, done) => done(null, user)
);
};
19 changes: 8 additions & 11 deletions backend/auth/TokenStrategy.js → backend/auth/TokenStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
/**
* Created by edenp on 7/30/17.
*/
// @ts-nocheck File not migrated fully to TS
import { Strategy } from 'passport-unique-token';
import { getUser } from '../handler/AuthHandler';
import { getLogger } from '../handler/LoggerHandler';

const UniqueTokenStrategy = require('passport-unique-token').Strategy;
const AuthHandler = require('../handler/AuthHandler');
const LoggerHandler = require('../handler/LoggerHandler');
const logger = getLogger('Passport');

const logger = LoggerHandler.getLogger('Passport');

module.exports = () => {
return new UniqueTokenStrategy(
export default () => {
return new Strategy(
{
tokenHeader: 'authentication-token'
},
(token, done) => {
AuthHandler.getUser(token)
getUser(token)
.then(user => {
return done(null, user);
})
Expand Down
3 changes: 3 additions & 0 deletions backend/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript']
};
73 changes: 0 additions & 73 deletions backend/config.js

This file was deleted.

Loading

0 comments on commit 1b7bf59

Please sign in to comment.