Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/routes/aggregate/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,31 @@ export function registerAggregateRoutes(
f => f.supportedAggregation && f.supportedAggregation.length > 0,
);

// API unique idenfier
const aggregateAPIIdentifier = `aggregate->${model.name}->get_aggregation`;
const webhookConfig =
config.apis?.[aggregateAPIIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[aggregateAPIIdentifier]?.ssp ?? [];
// construct the api identifier
const apiIdentifier = `aggregate->${model.name}->get_aggregation`;

// extract the api configs based on the api identifier
const webhookConfig = config.apis?.[apiIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[apiIdentifier]?.ssp ?? [];
// calculating the authroization based on auth flag, it can be true
// if the api level auth is enabled, or if the app level auth is enabled
const authorization =
config.apis?.[aggregateAPIIdentifier]?.authorization ?? false;
config.apis?.[apiIdentifier]?.authorization ??
config.auth?.enableAuth ??
false;

// for each aggregatable field, we create a GET route
// /<model_name>/aggregation/<field_name>
for (const field of aggregatableFields) {
const operations = field.supportedAggregation!;

// generating the schema for the route
const schema: Record<string, unknown> = generateSchema(
config,
field,
model,
operations,
authorization,
);

app.get(
Expand Down Expand Up @@ -189,14 +196,23 @@ function generateSchema(
field: ModelFieldConfig,
model: ModelConfig,
operations: SupportedAggregationOperation[],
authorization: boolean,
) {
const security: Array<{[key: string]: string[]}> = [];

if (config.auth?.enableAuth && config.auth?.authEngine === 'up-auth') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'up-auth' &&
authorization
) {
security.push({bearerAuth: []});
}

if (config.auth?.enableAuth && config.auth?.authEngine === 'api-key') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'api-key' &&
authorization
) {
security.push({apiKeyAuth: []});
}

Expand Down
28 changes: 24 additions & 4 deletions src/routes/custom-queries/custom-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,18 @@ export function registerCustomQueryRoutes(
const queryProperties: Record<string, object> = {};
const bodyProperties: Record<string, object> = {};

// uniqie api identifier
// constructing the api identifier
const apiIdentifier = `customAPIs->customQueries->${cq.name}`;

// extracting the api configs based on the api identifier
const webhookConfig = config.apis?.[apiIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[apiIdentifier]?.ssp ?? [];
const authorization = config.apis?.[apiIdentifier]?.authorization ?? false;
// calculating the authroization based on auth flag, it can be true
// if the api level auth is enabled, or if the app level auth is enabled
const authorization =
config.apis?.[apiIdentifier]?.authorization ??
config.auth?.enableAuth ??
false;

// body parameters are always in between @@
// path parameters are always in between $$
Expand Down Expand Up @@ -195,18 +202,31 @@ export function registerCustomQueryRoutes(

const security: Array<{[key: string]: string[]}> = [];

if (config.auth?.enableAuth && config.auth?.authEngine === 'up-auth') {
// adding the security based on the auth flag and auth engine
// if the auth flag is enabled and the auth engine is up-auth, then add the bearerAuth
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'up-auth' &&
authorization
) {
security.push({bearerAuth: []});
}

if (config.auth?.enableAuth && config.auth?.authEngine === 'api-key') {
// if the auth flag is enabled and the auth engine is api-key, then add the apiKeyAuth
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'api-key' &&
authorization
) {
security.push({apiKeyAuth: []});
}

// if there is any security configutaion required then add it to the swagger schema
if (security.length > 0) {
schema.security = security;
}

// registering the route
app.route({
method: cq.method,
url: routePath,
Expand Down
27 changes: 23 additions & 4 deletions src/routes/operations/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,19 @@ export function registerDeleteRoutes(
f.supportedOperations?.includes('deletable'),
);

// Unique api identifier
// constructing the api identifier
const apiIdentifier = `modelAPIs->delete->${model.name}`;

// extracting the api configs based on the api identifier
const webhookConfig = config.apis?.[apiIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[apiIdentifier]?.ssp ?? [];
const authorization = config.apis?.[apiIdentifier]?.authorization ?? false;

// calculating the authroization based on auth flag, it can be true
// if the api level auth is enabled, or if the app level auth is enabled
const authorization =
config.apis?.[apiIdentifier]?.authorization ??
config.auth?.enableAuth ??
false;

// If we have deletable fields, we register a DELETE route for each.
for (const field of deletableFields) {
Expand All @@ -48,6 +56,7 @@ export function registerDeleteRoutes(
field,
model,
config,
authorization,
);

app.delete(
Expand All @@ -56,6 +65,7 @@ export function registerDeleteRoutes(
schema,
preValidation: async request => enforceSSP(sspConfig, request),
preHandler: async (request, reply) => {
// checking the authorization
if (config.auth?.enableAuth && authorization) {
try {
await request.jwtVerify();
Expand Down Expand Up @@ -115,6 +125,7 @@ function generateSchema(
field: ModelFieldConfig,
model: ModelConfig,
config: AppConfig,
authorization: boolean,
) {
const paramSchema = mapDataTypeToJsonSchema(field.type);

Expand Down Expand Up @@ -143,11 +154,19 @@ function generateSchema(

const security: Array<{[key: string]: string[]}> = [];

if (config.auth?.enableAuth && config.auth?.authEngine === 'up-auth') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'up-auth' &&
authorization
) {
security.push({bearerAuth: []});
}

if (config.auth?.enableAuth && config.auth?.authEngine === 'api-key') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'api-key' &&
authorization
) {
security.push({apiKeyAuth: []});
}

Expand Down
24 changes: 20 additions & 4 deletions src/routes/operations/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,19 @@ export function registerEditRoutes(
f.supportedOperations?.includes('editable'),
);

// unique api identifier
// constructing the api identifier
const apiIdentifier = `modelAPIs->edit->${model.name}`;

// extracting the api configs based on the api identifier
const webhookConfig = config.apis?.[apiIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[apiIdentifier]?.ssp ?? [];
const authorization = config.apis?.[apiIdentifier]?.authorization ?? false;

// calculating the authroization based on auth flag, it can be true
// if the api level auth is enabled, or if the app level auth is enabled
const authorization =
config.apis?.[apiIdentifier]?.authorization ??
config.auth?.enableAuth ??
false;

for (const field of editableFields) {
const isUnique = field.primaryKey || field.unique;
Expand Down Expand Up @@ -122,11 +130,19 @@ export function registerEditRoutes(

const security: Array<{[key: string]: string[]}> = [];

if (config.auth?.enableAuth && config.auth?.authEngine === 'up-auth') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'up-auth' &&
authorization
) {
security.push({bearerAuth: []});
}

if (config.auth?.enableAuth && config.auth?.authEngine === 'api-key') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'api-key' &&
authorization
) {
security.push({apiKeyAuth: []});
}

Expand Down
39 changes: 33 additions & 6 deletions src/routes/operations/get-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,36 @@ export function registerGetAllRoutes(
const {models} = config;

for (const model of models) {
// unique api identifier
// constructing the api identifier
const apiIdentifier = `modelAPIs->getAll->${model.name}`;

// extracting the api configs based on the api identifier
const webhookConfig = config.apis?.[apiIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[apiIdentifier]?.ssp ?? [];
const authorization = config.apis?.[apiIdentifier]?.authorization ?? false;

const schema: Record<string, unknown> = generateSchema(model, config);
// calculating the authroization based on auth flag, it can be true
// if the api level auth is enabled, or if the app level auth is enabled
const authorization =
config.apis?.[apiIdentifier]?.authorization ??
config.auth?.enableAuth ??
false;

const schema: Record<string, unknown> = generateSchema(
model,
config,
authorization,
);

app.get(
`/${model.name}/`,
{
schema,
preValidation: async request => enforceSSP(sspConfig, request),
preHandler: async (request, reply) => {
console.log('I am here');
if (config.auth?.enableAuth && authorization) {
try {
console.log('I am running to verify JWT?');
await request.jwtVerify();
} catch {
return reply
Expand All @@ -77,6 +91,7 @@ export function registerGetAllRoutes(
},
},
async (request: FastifyRequest, reply: FastifyReply) => {
console.log(request.user);
const queryParams = request.query as Record<string, unknown>;
const tableName = model.name;

Expand Down Expand Up @@ -146,7 +161,11 @@ export function registerGetAllRoutes(
);
}
}
function generateSchema(model: ModelConfig, config: AppConfig) {
function generateSchema(
model: ModelConfig,
config: AppConfig,
authorization: boolean,
) {
const queryProperties: Record<string, object> = {};

// Add filter params for each field based on its supportedOperations
Expand Down Expand Up @@ -205,11 +224,19 @@ function generateSchema(model: ModelConfig, config: AppConfig) {

const security: Array<{[key: string]: string[]}> = [];

if (config.auth?.enableAuth && config.auth?.authEngine === 'up-auth') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'up-auth' &&
authorization
) {
security.push({bearerAuth: []});
}

if (config.auth?.enableAuth && config.auth?.authEngine === 'api-key') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'api-key' &&
authorization
) {
security.push({apiKeyAuth: []});
}

Expand Down
27 changes: 22 additions & 5 deletions src/routes/operations/index-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ export function registerIndexRoutes(
);
});

// unique api identifier
// constructing the api identifier
const apiIdentifier = `modelAPIs->index->${model.name}`;

// extracting the api configs based on the api identifier
const webhookConfig = config.apis?.[apiIdentifier]?.webhooks ?? null;
const sspConfig = config.apis?.[apiIdentifier]?.ssp ?? [];
const authorization = config.apis?.[apiIdentifier]?.authorization ?? false;

// calculating the authroization based on auth flag, it can be true
// if the api level auth is enabled, or if the app level auth is enabled
const authorization =
config.apis?.[apiIdentifier]?.authorization ??
config.auth?.enableAuth ??
false;

// index apis means for these APIs, we can fetch data using the indexable fields
// for example, if we have a field user_id in the users table, and it is indexed,
Expand All @@ -57,7 +65,7 @@ export function registerIndexRoutes(
schema,
isUnique,
}: {schema: Record<string, unknown>; isUnique: boolean | undefined} =
generateSchema(field, model, config);
generateSchema(field, model, config, authorization);

app.get(
`/${model.name}/${field.name}/:${field.name}`,
Expand Down Expand Up @@ -198,6 +206,7 @@ function generateSchema(
field: ModelFieldConfig,
model: ModelConfig,
config: AppConfig,
authorization: boolean,
) {
const isUnique = field.primaryKey || field.unique;
// converting the data type of the indexable field to json schema supported type
Expand Down Expand Up @@ -302,11 +311,19 @@ function generateSchema(

const security: Array<{[key: string]: string[]}> = [];

if (config.auth?.enableAuth && config.auth?.authEngine === 'up-auth') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'up-auth' &&
authorization
) {
security.push({bearerAuth: []});
}

if (config.auth?.enableAuth && config.auth?.authEngine === 'api-key') {
if (
config.auth?.enableAuth &&
config.auth?.authEngine === 'api-key' &&
authorization
) {
security.push({apiKeyAuth: []});
}

Expand Down
Loading
Loading