-
Notifications
You must be signed in to change notification settings - Fork 0
fix-code #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix-code #11
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { Request, Response } from 'express'; | ||
| import { successResponse, errorResponse } from '../utils/response'; | ||
| import UserRepository from '../repositories/user-repository'; | ||
| import { generateApiKey } from '../utils/auth'; | ||
| import Users from '../models/user'; | ||
| import { InferAttributes } from 'sequelize'; | ||
|
|
||
| type UserType = InferAttributes<Users>; | ||
|
|
||
| interface AuthRequest extends Request { | ||
| user?: UserType; | ||
| } | ||
|
|
||
| export const getApiKey = async (req: AuthRequest, res: Response) => { | ||
| try { | ||
| if (!req.user) { | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
|
|
||
| return successResponse( | ||
| res, | ||
| { apikey: req.user.apikey }, | ||
| 'User details retrieved', | ||
| ); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
| }; | ||
|
|
||
| export const regenerateApiKey = async (req: AuthRequest, res: Response) => { | ||
| try { | ||
| if (!req.user) { | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
|
|
||
| const newApiKey = await generateApiKey(); | ||
| await UserRepository.update({ id: req.user.id }, { apikey: newApiKey }); | ||
|
|
||
| return successResponse( | ||
| res, | ||
| { apikey: newApiKey }, | ||
| 'User details retrieved', | ||
| ); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Request, Response } from 'express'; | ||
| import { sequelize } from '../config/database'; | ||
| import { QueryTypes } from 'sequelize'; | ||
| import { errorResponse, successResponse } from '../utils/response'; | ||
|
|
||
| export const executeQuery = async (req: Request, res: Response) => { | ||
| try { | ||
| const { query } = req.body; | ||
|
|
||
| if (!query || typeof query !== 'string') { | ||
| return errorResponse(res, 'Query is required and must be a string', 400); | ||
| } | ||
|
|
||
| const forbiddenPatterns = [ | ||
| /DROP\s+TABLE/i, | ||
| /ALTER\s+/i, | ||
| /DELETE\s+FROM\s+[^\s]+(\s*;|$)/i, | ||
| ]; | ||
| if (forbiddenPatterns.some((pattern) => pattern.test(query))) { | ||
| return errorResponse(res, 'Query contains forbidden operations', 403); | ||
| } | ||
|
|
||
| const result = await sequelize.query(query, { type: QueryTypes.RAW }); | ||
| return successResponse(res, result, 'Query executed successfully'); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return errorResponse(res, 'Failed to execute query', 500); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,15 @@ | ||
| import { Request, Response } from 'express'; | ||
| import SchemaRepository from '../repositories/schema-repository'; | ||
| import { errorResponse, successResponse } from '../utils/response'; | ||
|
|
||
| export const schema = async (req: Request, res: Response) => { | ||
| try { | ||
| const tables = await SchemaRepository.getSchemas(); | ||
|
|
||
| return res.json({ success: true, tables }); | ||
| return successResponse(res, tables); | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : 'Unknown error'; | ||
| res.status(500).json({ error: errorMessage }); | ||
| return errorResponse(res, errorMessage, 500); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { Request, Response, NextFunction } from 'express'; | ||
| import { errorResponse } from '../utils/response'; | ||
| import UserRepository from '../repositories/user-repository'; | ||
|
|
||
| export const apiKeyMiddleware = async ( | ||
| req: Request, | ||
| res: Response, | ||
| next: NextFunction, | ||
| ) => { | ||
| try { | ||
| const apiKey = req.headers['x-api-key'] as string; | ||
|
|
||
| if (!apiKey) { | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
|
|
||
| const user = await UserRepository.findOne( | ||
| { apikey: apiKey }, | ||
| { attributes: { exclude: ['password'] } }, | ||
| ); | ||
| if (!user) { | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
|
|
||
| (req as any).user = user; | ||
| next(); | ||
| } catch (error) { | ||
| console.error('Error:', error); | ||
| return errorResponse(res, 'Unauthorized', 401); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Router } from 'express'; | ||
| import { authMiddleware } from '../middlewares/auth-middleware'; | ||
| import { getApiKey, regenerateApiKey } from '../controllers/apikey-controller'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.get('/', authMiddleware, getApiKey); | ||
| router.put('/', authMiddleware, regenerateApiKey); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { Router } from 'express'; | ||
| const { migrate } = require('../controllers/ddl-controller'); | ||
| import { apiKeyMiddleware } from '../middlewares/apikey-middleware'; | ||
| import { migrate } from '../controllers/ddl-controller'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.post('/', migrate); | ||
| router.post('/', apiKeyMiddleware, migrate); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { Router } from 'express'; | ||
| const { execute } = require('../controllers/dml-controller'); | ||
| import { apiKeyMiddleware } from '../middlewares/apikey-middleware'; | ||
| import { execute } from '../controllers/dml-controller'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.post('/', execute); | ||
| router.post('/', apiKeyMiddleware, execute); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { Router } from 'express'; | ||
| import { apiKeyMiddleware } from '../middlewares/apikey-middleware'; | ||
| import { executeQuery } from '../controllers/query-controller'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.post('/', apiKeyMiddleware, executeQuery); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| import { Router } from 'express'; | ||
| const { schema } = require('../controllers/schema-controller'); | ||
| import { apiKeyMiddleware } from '../middlewares/apikey-middleware'; | ||
| import { schema } from '../controllers/schema-controller'; | ||
|
|
||
| const router = Router(); | ||
|
|
||
| router.get('/', schema); | ||
| router.get('/', apiKeyMiddleware, schema); | ||
|
|
||
| export default router; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.