Last active
July 12, 2024 01:15
-
-
Save skysign/bf783d5df1b62260c5e0db0f3d119f6c to your computer and use it in GitHub Desktop.
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
const router = express.Router() | |
async function myValidationResult(req: Request, res: Response, next: NextFunction) { | |
const result = validationResult(req) | |
if (!result.isEmpty()) { | |
return res.status(400).json({ errors: result.array() }) | |
} | |
return next(); | |
} | |
function validationRules(): any[] { | |
return [ | |
query('api_key', 'api_key is invalid.') <- Adding parameter here, to make api_key like github action secret | |
.custom(async (api_key) => { | |
const userldapDN = getLDAPDNfromAPIkey(api_key) <- This is our own logic for authentication | |
const b = await IsValidAPIKey(api_key, userldapDN) | |
return new Promise((resolve, reject) => { | |
if (b) { | |
resolve(b) | |
} | |
else { | |
reject() | |
} | |
}) | |
}) | |
] | |
} | |
router.get('/example-endpoint', validationRules(), myValidationResult, doExampleEndpoint) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment