Skip to content

Commit

Permalink
validate API key for config APIs (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakprabhakara authored Jan 8, 2022
1 parent 9d43298 commit 230df55
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
16 changes: 16 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { NextApiRequest } from 'next';
import env from '@lib/env';

export const validateApiKey = (token) => {
return env.apiKeys.includes(token);
};

export const extractAuthToken = (req: NextApiRequest) => {
const authHeader = req.headers['authorization'];
const parts = (authHeader || '').split(' ');
if (parts.length > 1) {
return parts[1];
}

return null;
};
13 changes: 0 additions & 13 deletions npm/src/controller/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
import { Request } from 'express';

export const extractAuthToken = (req: Request): string | null => {
const authHeader = req.get('authorization');
const parts = (authHeader || '').split(' ');

if (parts.length > 1) {
return parts[1];
}

return null;
};

export enum IndexNames {
EntityID = 'entityID',
TenantProduct = 'tenantProduct',
Expand Down
11 changes: 1 addition & 10 deletions pages/api/oauth/userinfo.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { NextApiRequest, NextApiResponse } from 'next';

import jackson from '@lib/jackson';

const extractAuthToken = (req: NextApiRequest) => {
const authHeader = req.headers['authorization'];
const parts = (authHeader || '').split(' ');
if (parts.length > 1) {
return parts[1];
}

return null;
};
import { extractAuthToken } from '@lib/utils';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
Expand Down
12 changes: 8 additions & 4 deletions pages/api/v1/saml/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { NextApiRequest, NextApiResponse } from 'next';

import jackson from '@lib/jackson';
import { extractAuthToken, validateApiKey } from '@lib/utils';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const apiKey = extractAuthToken(req);
if (!validateApiKey(apiKey)) {
res.status(401).json({ message: 'Unauthorized' });
return;
}

const { apiController } = await jackson();
if (req.method === 'POST') {
res.json(await apiController.config(req.body));
Expand Down

0 comments on commit 230df55

Please sign in to comment.