Skip to content

Commit 742e09d

Browse files
author
UmmulkiramR
committed
updated valid value check secured api, bearer token extraction
1 parent f6aa662 commit 742e09d

6 files changed

Lines changed: 23 additions & 11 deletions

File tree

.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ CLIENT_SECRET=
2121
AUTH_WRITE_SCOPES=[]
2222
AUTH_STRATEGY= # valid values -> EGO or KEYCLOAK or NONE
2323
AUTH_SERVER_URL: # "https://ego.url" OR "http://localhost/realms/keycloak-realm". This property will be empty or absent for AUTH_STRATEGY = NONE
24+
25+
# SECURED_API can be used to specify which of the APIs need authentication.
26+
# Can be empty or absent for AUTH_STRATEGY = NONE.
27+
# If omitted for AUTH_STRATEGY != NONE then all apis will need authentication by default.
2428
SECURED_API = ["CREATE", "FIND"]
2529
AUTH_PUBLICKEY_CACHE = 1d
2630

src/config-validator.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ export const getArray = (name: string): ZodString['_output'][] => {
6767
}
6868
};
6969

70-
export const getEnum = <T extends [string, ...string[]]>(name: string, zEnum: ZodEnum<T>): z.infer<ZodEnum<T>> => {
71-
const value = process.env[name];
70+
export const getEnum = <T extends [string, ...string[]]>(name: string, zEnum: ZodEnum<T>, value: string | undefined = ''): z.infer<ZodEnum<T>> => {
71+
if(!value || value?.length==0) value = process.env[name];
7272
const stringValue = zEnum.safeParse(value);
7373
if (!stringValue.success) {
74-
throw new Error(`Environment variable ${name} is invalid value. Value should be one of ${zEnum.options}`);
74+
throw new Error(`Environment variable ${name} has an invalid value. Valid values are: ${zEnum.options}`);
7575
}
7676
return stringValue.data;
7777
};
7878

79+
7980
export const getRecord = (name: string): ZodRecord<ZodString, ZodString> => {
8081
const config_entry = `${name.toUpperCase()}_SEARCH`;
8182
return z.record(

src/config.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from './config-validator.js';
1313
import { SchemaInfo } from './middlewares/datasource.js';
1414
import {RecordType, z} from "zod";
15-
import {authList, AuthStrategy} from "./middlewares/autorization/auth-types.js";
15+
import {apiList, authList, AuthStrategy, SecuredApi} from "./middlewares/autorization/auth-types.js";
1616

1717
if (dotenv.config().error) {
1818
console.log(`Error loading environment variables, aborting.`);
@@ -41,9 +41,11 @@ export const logging: boolean = JSON.parse(process.env.DB_LOGGING || 'false');
4141
export const entityList = getRequiredArray('ENTITY_LIST');
4242

4343
export const scopes = getArray('SCOPES');
44-
export const securedApi = getArray('SECURED_API');
4544
export const dbSequences = getArray('DB_SEQUENCES');
4645

46+
export const securedApi = getArray('SECURED_API');
47+
securedApi.forEach(api => {getEnum('SECURED_API', z.enum(apiList), api)});
48+
4749
export const authStrategy: AuthStrategy = getEnum('AUTH_STRATEGY', z.enum(authList));
4850

4951
export const schemaDefinitions: Map<string, SchemaInfo> = new Map<string, SchemaInfo>();

src/middlewares/autorization/auth-types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {NextFunction, Request, Response} from "express";
22

33
export const authList = ['EGO', 'KEYCLOAK', 'NONE'] as const;
4-
export type AuthStrategy= 'EGO' | 'KEYCLOAK' | 'NONE';
4+
export type AuthStrategy = 'EGO' | 'KEYCLOAK' | 'NONE';
5+
6+
export const apiList = ['CREATE', 'FIND'] as const;
7+
export type SecuredApi = 'CREATE' | 'FIND';
58

69
export interface AuthorizationStrategy {
710
authHandler(req: Request, res: Response, next: NextFunction): Promise<void>;

src/middlewares/autorization/auth-util.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { NextFunction, Request, Response } from 'express';
22
import {authStrategy, securedApi} from '../../config.js';
33
import egoAuth from './ego-auth-handler.js';
44
import keycloakAuth from './keycloak-auth-handler.js';
5-
import { ForbiddenError, UnauthorizedError } from '../error-handler';
65

76
function getAuthStrategy() {
87
if (authStrategy === 'EGO') {
@@ -26,8 +25,12 @@ export function authorize(action: string){
2625

2726
export function extractHeaderToken(req: Request) {
2827
const authorization = req.headers.authorization||'';
29-
const token: string = authorization.split(' ')[1];
30-
return token;
28+
console.log(authorization);
29+
if (authorization.startsWith('Bearer ')) {
30+
const token = authorization.substring(7, authorization.length);
31+
return token;
32+
}
33+
return '';
3134
}
3235

3336
export function isJwt(tokenString: string) {
@@ -44,4 +47,4 @@ export function isJwt(tokenString: string) {
4447
return false;
4548
}
4649
return true;
47-
}
50+
}

src/services/id-service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ function validateSearchParams(searchCriteria: RecordType<string, string>) {
8282
}
8383

8484
function getSearchCriteria(entity: string, requestParams: Record<string, string>) {
85-
const property = `${entity.toUpperCase()}_SEARCH`;
8685
const search = config.searchCriterias.get(entity);
8786
const keyCriteria = {...search};
8887
for (const param in requestParams) {

0 commit comments

Comments
 (0)