Compare commits

..

No commits in common. "39cefbc593a057393c59de96d2b7153adb5ea7a5" and "1748fd4034aa5716cf46729794d881bef4b20760" have entirely different histories.

8 changed files with 20 additions and 30 deletions

View File

@ -1,15 +1,18 @@
import consola from "consola";
import * as dotenvSafe from "dotenv-safe";
import "reflect-metadata";
import { createExpressServer } from "routing-controllers";
import authchecker from "./authchecker";
import { config } from './config';
import loaders from "./loaders/index";
import { ErrorHandler } from './middlewares/ErrorHandler';
dotenvSafe.config();
const PORT = process.env.APP_PORT || 4010;
const app = createExpressServer({
authorizationChecker: authchecker,
middlewares: [ErrorHandler],
development: config.development,
development: process.env.NODE_ENV === "production",
cors: true,
routePrefix: "/api",
controllers: [__dirname + "/controllers/*.ts"],
@ -17,9 +20,9 @@ const app = createExpressServer({
async function main() {
await loaders(app);
app.listen(config.internal_port, () => {
app.listen(PORT, () => {
consola.success(
`⚡️[server]: Server is running at http://localhost:${config.internal_port}`
`⚡️[server]: Server is running at http://localhost:${PORT}`
);
});
}

View File

@ -1,7 +1,6 @@
import * as jwt from "jsonwebtoken";
import { Action } from "routing-controllers";
import { getConnectionManager } from 'typeorm';
import { config } from './config';
import { IllegalJWTError, NoPermissionError, UserNonexistantOrRefreshtokenInvalidError } from './errors/AuthError';
import { User } from './models/entities/User';
// -----------
@ -16,7 +15,7 @@ const authchecker = async (action: Action, permissions: string | string[]) => {
const provided_token = action.request.query["auth"];
let jwtPayload = undefined
try {
jwtPayload = <any>jwt.verify(provided_token, config.jwt_secret);
jwtPayload = <any>jwt.verify(provided_token, "securekey");
} catch (error) {
console.log(error);
throw new IllegalJWTError()
@ -43,7 +42,7 @@ const authchecker = async (action: Action, permissions: string | string[]) => {
}
//
try {
jwt.verify(provided_token, config.jwt_secret);
jwt.verify(provided_token, process.env.JWT_SECRET || "secretjwtsecret");
return true
} catch (error) {
return false

View File

@ -1,7 +0,0 @@
import * as dotenvSafe from "dotenv-safe";
dotenvSafe.config();
export const config = {
internal_port: process.env.APP_PORT || 4010,
development: process.env.NODE_ENV === "production",
jwt_secret: process.env.JWT_SECRET || "secretjwtsecret"
}

View File

@ -2,7 +2,6 @@ import * as argon2 from "argon2";
import { IsEmail, IsOptional, IsString } from 'class-validator';
import * as jsonwebtoken from 'jsonwebtoken';
import { getConnectionManager } from 'typeorm';
import { config } from '../../config';
import { InvalidCredentialsError, PasswordNeededError, UserNotFoundError } from '../../errors/AuthError';
import { UsernameOrEmailNeededError } from '../../errors/UserErrors';
import { User } from '../entities/User';
@ -39,7 +38,7 @@ export class CreateAuth {
newAuth.access_token = jsonwebtoken.sign({
userdetails: found_user,
exp: timestamp_accesstoken_expiry
}, config.jwt_secret)
}, "securekey")
newAuth.access_token_expires_at = timestamp_accesstoken_expiry
//
const timestamp_refresh_expiry = Math.floor(Date.now() / 1000) + 10 * 36000
@ -47,7 +46,7 @@ export class CreateAuth {
refreshtokencount: found_user.refreshTokenCount,
userid: found_user.id,
exp: timestamp_refresh_expiry
}, config.jwt_secret)
}, "securekey")
newAuth.refresh_token_expires_at = timestamp_refresh_expiry
} else {
throw new InvalidCredentialsError()

View File

@ -1,7 +1,6 @@
import { IsString } from 'class-validator';
import * as jsonwebtoken from 'jsonwebtoken';
import { getConnectionManager } from 'typeorm';
import { config } from '../../config';
import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError';
import { User } from '../entities/User';
import { Logout } from '../responses/ResponseLogout';
@ -17,7 +16,7 @@ export class HandleLogout {
}
let decoded;
try {
decoded = jsonwebtoken.verify(this.token, config.jwt_secret)
decoded = jsonwebtoken.verify(this.token, 'securekey')
} catch (error) {
throw new IllegalJWTError()
}

View File

@ -1,7 +1,6 @@
import { IsString } from 'class-validator';
import * as jsonwebtoken from 'jsonwebtoken';
import { getConnectionManager } from 'typeorm';
import { config } from '../../config';
import { IllegalJWTError, JwtNotProvidedError, RefreshTokenCountInvalidError, UserNotFoundError } from '../../errors/AuthError';
import { User } from '../entities/User';
import { Auth } from '../responses/ResponseAuth';
@ -17,7 +16,7 @@ export class RefreshAuth {
}
let decoded
try {
decoded = jsonwebtoken.verify(this.token, config.jwt_secret)
decoded = jsonwebtoken.verify(this.token, 'securekey')
} catch (error) {
throw new IllegalJWTError()
}
@ -34,7 +33,7 @@ export class RefreshAuth {
newAuth.access_token = jsonwebtoken.sign({
userdetails: found_user,
exp: timestamp_accesstoken_expiry
}, config.jwt_secret)
}, "securekey")
newAuth.access_token_expires_at = timestamp_accesstoken_expiry
//
const timestamp_refresh_expiry = Math.floor(Date.now() / 1000) + 10 * 36000
@ -42,7 +41,7 @@ export class RefreshAuth {
refreshtokencount: found_user.refreshTokenCount,
userid: found_user.id,
exp: timestamp_refresh_expiry
}, config.jwt_secret)
}, "securekey")
newAuth.refresh_token_expires_at = timestamp_refresh_expiry
return newAuth;

View File

@ -1,22 +1,21 @@
import axios from 'axios';
import { config } from '../config';
const base = "http://localhost:" + config.internal_port
describe('GET /api/openapi.json', () => {
it('is http 200', async () => {
const res = await axios.get(base + '/api/openapi.json');
const res = await axios.get('http://localhost:4010/api/openapi.json');
expect(res.status).toEqual(200);
});
});
describe('GET /', () => {
it('is http 404', async () => {
const res = await axios.get(base + '/', { validateStatus: undefined });
const res = await axios.get('http://localhost:4010/', { validateStatus: undefined });
expect(res.status).toEqual(404);
});
});
describe('GET /api/teams', () => {
it('is http 200 && is json', async () => {
const res = await axios.get(base + '/api/teams', { validateStatus: undefined });
const res = await axios.get('http://localhost:4010/api/teams', { validateStatus: undefined });
console.log(res.headers);
expect(res.status).toEqual(200);
expect(res.headers['content-type']).toContain("application/json")
});

View File

@ -1,6 +1,5 @@
import axios from 'axios';
import { config } from '../config';
const base = "http://localhost:" + config.internal_port
const base = "http://localhost:4010"
describe('GET /api/tracks', () => {
it('basic get should return 200', async () => {