1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 19:44:35 +00:00

fix(web): ability to use get/patch User API in desktop mode.

This commit is contained in:
Saad Jutt
2022-05-27 17:01:14 +05:00
parent b066734398
commit 2c259fe1de
6 changed files with 79 additions and 14 deletions

View File

@@ -3,12 +3,11 @@ import { Request, Security, Route, Tags, Post, Body } from 'tsoa'
import { ExecuteReturnJson, ExecutionController } from './internal' import { ExecuteReturnJson, ExecutionController } from './internal'
import { ExecuteReturnJsonResponse } from '.' import { ExecuteReturnJsonResponse } from '.'
import { import {
getDesktopUserAutoExecPath,
getPreProgramVariables, getPreProgramVariables,
getUserAutoExec,
ModeType, ModeType,
parseLogToArray parseLogToArray
} from '../utils' } from '../utils'
import { readFile } from '@sasjs/utils'
interface ExecuteSASCodePayload { interface ExecuteSASCodePayload {
/** /**
@@ -43,7 +42,7 @@ const executeSASCode = async (
const userAutoExec = const userAutoExec =
process.env.MODE === ModeType.Server process.env.MODE === ModeType.Server
? user?.autoExec ? user?.autoExec
: await readFile(getDesktopUserAutoExecPath()) : await getUserAutoExec()
try { try {
const { webout, log, httpHeaders } = const { webout, log, httpHeaders } =

View File

@@ -14,8 +14,10 @@ import {
Hidden, Hidden,
Request Request
} from 'tsoa' } from 'tsoa'
import { desktopUser } from '../middlewares'
import User, { UserPayload } from '../model/User' import User, { UserPayload } from '../model/User'
import { getUserAutoExec, updateUserAutoExec, ModeType } from '../utils'
export interface UserResponse { export interface UserResponse {
id: number id: number
@@ -86,6 +88,10 @@ export class UserController {
@Request() req: express.Request, @Request() req: express.Request,
@Path() userId: number @Path() userId: number
): Promise<UserDetailsResponse> { ): Promise<UserDetailsResponse> {
const { MODE } = process.env
if (MODE === ModeType.Desktop) return getDesktopAutoExec()
const { user } = req const { user } = req
const getAutoExec = user!.isAdmin || user!.userId == userId const getAutoExec = user!.isAdmin || user!.userId == userId
return getUser(userId, getAutoExec) return getUser(userId, getAutoExec)
@@ -108,6 +114,11 @@ export class UserController {
@Path() userId: number, @Path() userId: number,
@Body() body: UserPayload @Body() body: UserPayload
): Promise<UserDetailsResponse> { ): Promise<UserDetailsResponse> {
const { MODE } = process.env
if (MODE === ModeType.Desktop)
return updateDesktopAutoExec(body.autoExec ?? '')
return updateUser(userId, body) return updateUser(userId, body)
} }
@@ -181,6 +192,14 @@ const getUser = async (
} }
} }
const getDesktopAutoExec = async () => {
return {
...desktopUser,
id: desktopUser.userId,
autoExec: await getUserAutoExec()
}
}
const updateUser = async ( const updateUser = async (
id: number, id: number,
data: Partial<UserPayload> data: Partial<UserPayload>
@@ -216,6 +235,15 @@ const updateUser = async (
} }
} }
const updateDesktopAutoExec = async (autoExec: string) => {
await updateUserAutoExec(autoExec)
return {
...desktopUser,
id: desktopUser.userId,
autoExec
}
}
const deleteUser = async ( const deleteUser = async (
id: number, id: number,
isAdmin: boolean, isAdmin: boolean,

View File

@@ -1,15 +1,22 @@
import { RequestHandler, Request, Response, NextFunction } from 'express' import { RequestHandler, Request, Response, NextFunction } from 'express'
import jwt from 'jsonwebtoken' import jwt from 'jsonwebtoken'
import { csrfProtection } from '../app' import { csrfProtection } from '../app'
import { fetchLatestAutoExec, verifyTokenInDB } from '../utils' import { fetchLatestAutoExec, ModeType, verifyTokenInDB } from '../utils'
import { desktopUser } from './desktop'
export const authenticateAccessToken: RequestHandler = async ( export const authenticateAccessToken: RequestHandler = async (
req, req,
res, res,
next next
) => { ) => {
const { MODE } = process.env
if (MODE === ModeType.Server) {
req.user = desktopUser
return next()
}
// if request is coming from web and has valid session // if request is coming from web and has valid session
// we can validate the request and check for CSRF Token // it can be validated.
if (req.session?.loggedIn) { if (req.session?.loggedIn) {
if (req.session.user) { if (req.session.user) {
const user = await fetchLatestAutoExec(req.session.user) const user = await fetchLatestAutoExec(req.session.user)

View File

@@ -1,20 +1,42 @@
import { RequestHandler } from 'express' import { RequestHandler, Request } from 'express'
import { RequestUser } from '../types'
import { ModeType } from '../utils'
const regexUser = /^\/SASjsApi\/user\/[0-9]*$/ // /SASjsApi/user/1
const allowedInDesktopMode: { [key: string]: RegExp[] } = {
GET: [regexUser],
PATCH: [regexUser]
}
const reqAllowedInDesktopMode = (request: Request): boolean => {
const { method, originalUrl: url } = request
return !!allowedInDesktopMode[method]?.find((urlRegex) => urlRegex.test(url))
}
export const desktopRestrict: RequestHandler = (req, res, next) => { export const desktopRestrict: RequestHandler = (req, res, next) => {
const { MODE } = process.env const { MODE } = process.env
if (MODE?.trim() !== 'server')
if (MODE === ModeType.Desktop) {
if (!reqAllowedInDesktopMode(req))
return res.status(403).send('Not Allowed while in Desktop Mode.') return res.status(403).send('Not Allowed while in Desktop Mode.')
}
next() next()
} }
export const desktopUsername: RequestHandler = (req, res, next) => { export const desktopUsername: RequestHandler = (req, res, next) => {
const { MODE } = process.env const { MODE } = process.env
if (MODE?.trim() !== 'server') if (MODE === ModeType.Desktop) return res.status(200).send(desktopUser)
return res.status(200).send({
userId: 12345,
username: 'DESKTOPusername',
displayName: 'DESKTOP User'
})
next() next()
} }
export const desktopUser: RequestUser = {
userId: 12345,
clientId: 'desktop_app',
username: 'DESKTOPusername',
displayName: 'DESKTOP User',
isAdmin: true,
isActive: true
}

View File

@@ -0,0 +1,8 @@
import { createFile, readFile } from '@sasjs/utils'
import { getDesktopUserAutoExecPath } from './file'
export const getUserAutoExec = async (): Promise<string> =>
readFile(getDesktopUserAutoExecPath())
export const updateUserAutoExec = async (autoExecContent: string) =>
createFile(getDesktopUserAutoExecPath(), autoExecContent)

View File

@@ -1,6 +1,7 @@
export * from './appStreamConfig' export * from './appStreamConfig'
export * from './connectDB' export * from './connectDB'
export * from './copySASjsCore' export * from './copySASjsCore'
export * from './desktopAutoExec'
export * from './extractHeaders' export * from './extractHeaders'
export * from './file' export * from './file'
export * from './generateAccessToken' export * from './generateAccessToken'