diff --git a/.env.example b/.env.example index 7d63ce3..28b487d 100644 --- a/.env.example +++ b/.env.example @@ -10,6 +10,7 @@ SERVER_PORT = portnumber JWT_SECRET = ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 JWT_EXPIRATION = [0-9]*(y|d|h|m|s) REFRESH_EXPIRATION = [0-9]*(y|d|h|m|s) +PWA_REFRESH_EXPIRATION = [0-9]*(y|d|h|m|s) MAIL_USERNAME = mail_username MAIL_PASSWORD = mail_password diff --git a/README.md b/README.md index 713f57a..4896a21 100644 --- a/README.md +++ b/README.md @@ -25,20 +25,21 @@ services: container_name: ff_member_administration_server restart: unless-stopped environment: - - DB_TYPE=mysql + - DB_TYPE= # default ist auf mysql gesetzt - DB_HOST=ffm-db - - DB_PORT= # fallback ist auf 3306 gesetzt + - DB_PORT= # default ist auf 3306 gesetzt - DB_NAME=ffadmin - DB_USERNAME=administration_backend - DB_PASSWORD= - JWT_SECRET= - - JWT_EXPIRATION= - - REFRESH_EXPIRATION= + - JWT_EXPIRATION= # default ist auf 15m gesetzt + - REFRESH_EXPIRATION= # default ist auf 1d gesetzt + - PWA_REFRESH_EXPIRATION= # default ist auf 5d gesetzt - MAIL_USERNAME= - MAIL_PASSWORD= - MAIL_HOST= - - MAIL_PORT= - - MAIL_SECURE= + - MAIL_PORT= # default ist auf 578 gesetzt + - MAIL_SECURE= # default ist auf false gesetzt - CLUB_NAME= - CLUB_WEBSITE= volumes: diff --git a/src/command/refreshCommand.ts b/src/command/refreshCommand.ts index e0404c8..80b14cc 100644 --- a/src/command/refreshCommand.ts +++ b/src/command/refreshCommand.ts @@ -1,5 +1,6 @@ export interface CreateRefreshCommand { userId: number; + isFromPwa?: boolean; } export interface DeleteRefreshCommand { diff --git a/src/command/refreshCommandHandler.ts b/src/command/refreshCommandHandler.ts index 86841ad..709f51b 100644 --- a/src/command/refreshCommandHandler.ts +++ b/src/command/refreshCommandHandler.ts @@ -1,11 +1,9 @@ import { dataSource } from "../data-source"; import { refresh } from "../entity/refresh"; -import { REFRESH_EXPIRATION } from "../env.defaults"; +import { PWA_REFRESH_EXPIRATION, REFRESH_EXPIRATION } from "../env.defaults"; import InternalException from "../exceptions/internalException"; -import { JWTHelper } from "../helpers/jwtHelper"; import { StringHelper } from "../helpers/stringHelper"; import UserService from "../service/user/userService"; -import { JWTRefresh } from "../type/jwtTypes"; import { CreateRefreshCommand, DeleteRefreshCommand } from "./refreshCommand"; import ms from "ms"; @@ -16,10 +14,6 @@ export default abstract class RefreshCommandHandler { * @returns {Promise} */ static async create(createRefresh: CreateRefreshCommand): Promise { - // let createRefreshToken: JWTRefresh = { - // userId: createRefresh.userId, - // }; - // const refreshToken = await JWTHelper.create(createRefreshToken); const refreshToken = StringHelper.random(32); return await dataSource @@ -29,7 +23,9 @@ export default abstract class RefreshCommandHandler { .values({ token: refreshToken, user: await UserService.getById(createRefresh.userId), - expiry: new Date(Date.now() + ms(REFRESH_EXPIRATION)), + expiry: createRefresh.isFromPwa + ? new Date(Date.now() + ms(PWA_REFRESH_EXPIRATION)) + : new Date(Date.now() + ms(REFRESH_EXPIRATION)), }) .execute() .then((result) => { diff --git a/src/controller/authController.ts b/src/controller/authController.ts index dd4fee7..e3f5af2 100644 --- a/src/controller/authController.ts +++ b/src/controller/authController.ts @@ -8,9 +8,6 @@ import UserService from "../service/user/userService"; import speakeasy from "speakeasy"; import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException"; import RefreshService from "../service/refreshService"; -import UserPermissionService from "../service/user/userPermissionService"; -import PermissionHelper from "../helpers/permissionHelper"; -import RolePermissionService from "../service/user/rolePermissionService"; /** * @description Check authentication status by token @@ -39,6 +36,7 @@ export async function login(req: Request, res: Response): Promise { let refreshCommand: CreateRefreshCommand = { userId: id, + isFromPwa: req.isPWA, }; let refreshToken = await RefreshCommandHandler.create(refreshCommand); @@ -83,6 +81,7 @@ export async function refresh(req: Request, res: Response): Promise { let refreshCommand: CreateRefreshCommand = { userId: tokenUserId, + isFromPwa: req.isPWA, }; let refreshToken = await RefreshCommandHandler.create(refreshCommand); diff --git a/src/env.defaults.ts b/src/env.defaults.ts index a662555..159845b 100644 --- a/src/env.defaults.ts +++ b/src/env.defaults.ts @@ -13,6 +13,7 @@ export const SERVER_PORT = Number(process.env.SERVER_PORT ?? 5000); export const JWT_SECRET = process.env.JWT_SECRET ?? "my_jwt_secret_string_ilughfnadiuhgq§$IUZGFVRweiouarbt1oub3h5q4a"; export const JWT_EXPIRATION = process.env.JWT_EXPIRATION ?? "15m"; export const REFRESH_EXPIRATION = process.env.REFRESH_EXPIRATION ?? "1d"; +export const PWA_REFRESH_EXPIRATION = process.env.PWA_REFRESH_EXPIRATION ?? "5d"; export const MAIL_USERNAME = process.env.MAIL_USERNAME ?? ""; export const MAIL_PASSWORD = process.env.MAIL_PASSWORD ?? ""; @@ -35,6 +36,7 @@ export function configCheck() { if (JWT_SECRET == "" || typeof JWT_SECRET != "string") throw new Error("set valid value to JWT_SECRET"); checkMS(JWT_EXPIRATION, "JWT_EXPIRATION"); checkMS(REFRESH_EXPIRATION, "REFRESH_EXPIRATION"); + checkMS(PWA_REFRESH_EXPIRATION, "PWA_REFRESH_EXPIRATION"); if (MAIL_USERNAME == "" || typeof MAIL_USERNAME != "string") throw new Error("set valid value to MAIL_USERNAME"); if (MAIL_PASSWORD == "" || typeof MAIL_PASSWORD != "string") throw new Error("set valid value to MAIL_PASSWORD");