Skip to content

Commit

Permalink
feat(tenant-management): integrate auth0
Browse files Browse the repository at this point in the history
integrate auth0

GH-47
  • Loading branch information
Surbhi-sharma1 committed Sep 27, 2024
1 parent f14113b commit 0ecf67f
Show file tree
Hide file tree
Showing 22 changed files with 685 additions and 276 deletions.
44 changes: 44 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,52 @@ var path = require('path');
var Promise;

/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function (options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240925102459-add-table-tenant-configs-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
exports.up = function (db) {
var filePath = path.join(
__dirname,
'sqls',
'20240925102459-add-table-tenant-configs-up.sql',
);
return new Promise(function (resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
}).then(function (data) {
return db.runSql(data);
});
};

exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20240925102459-add-table-tenant-configs-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
exports.down = function (db) {
var filePath = path.join(
__dirname,
'sqls',
'20240925102459-add-table-tenant-configs-down.sql',
);
return new Promise(function (resolve, reject) {
fs.readFile(filePath, {encoding: 'utf-8'}, function (err, data) {
if (err) return reject(err);
console.log('received data: ' + data);

resolve(data);
});
})
.then(function(data) {
}).then(function (data) {
return db.runSql(data);
});
};

exports._meta = {
"version": 1
version: 1,
};
2 changes: 2 additions & 0 deletions services/tenant-management-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@
"@loopback/build": "^11.0.2",
"@loopback/eslint-config": "^15.0.2",
"@loopback/testlab": "^7.0.2",
"@types/auth0": "^3.3.10",
"@types/jsonwebtoken": "^9.0.5",
"@types/moment": "^2.13.0",
"@types/node": "^18.11.9",
"@types/pdfkit": "^0.13.4",
"auth0": "^4.10.0",
"eslint": "^8.57.0",
"nodemon": "^2.0.21",
"nyc": "^15.1.0",
Expand Down
25 changes: 21 additions & 4 deletions services/tenant-management-service/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ import {
SYSTEM_USER,
TenantManagementServiceBindings,
} from './keys';
import {ITenantManagementServiceConfig} from './types';
import {InvoiceController} from './controllers/invoice.controller';
import {
ContactController,
HomePageController,
LeadTenantController,
LeadController,
PingController,
TenantController,
TenantConfigController,
TenantConfigTenantController,
} from './controllers';
import {
Address,
Contact,
Expand Down Expand Up @@ -82,7 +94,8 @@ import {
OnboardingService,
ProvisioningService,
} from './services';
import { ITenantManagementServiceConfig } from './types';
import {IdpController} from './controllers/idp.controller';

export class TenantManagementServiceComponent implements Component {
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
Expand Down Expand Up @@ -122,7 +135,7 @@ export class TenantManagementServiceComponent implements Component {
ResourceRepository,
TenantRepository,
WebhookSecretRepository,
TenantConfigRepository
TenantConfigRepository,
];

this.models = [
Expand All @@ -139,7 +152,7 @@ export class TenantManagementServiceComponent implements Component {
TenantOnboardDTO,
VerifyLeadResponseDTO,
WebhookDTO,
TenantConfig
TenantConfig,
];

this.controllers = [
Expand All @@ -149,12 +162,16 @@ export class TenantManagementServiceComponent implements Component {
LeadTenantController,
LeadController,
PingController,
TenantController
TenantController,
IdpController,
TenantConfigController,
TenantConfigTenantController,
];

this.bindings = [
Binding.bind(LEAD_TOKEN_VERIFIER).toProvider(LeadTokenVerifierProvider),
Binding.bind(SYSTEM_USER).toProvider(SystemUserProvider),

createServiceBinding(ProvisioningService),
createServiceBinding(OnboardingService),
createServiceBinding(LeadAuthenticator),
Expand Down
110 changes: 55 additions & 55 deletions services/tenant-management-service/src/controllers/idp.controller.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
import { inject, intercept } from '@loopback/core';
import { getModelSchemaRef, post, requestBody } from '@loopback/rest';
import {inject, intercept} from '@loopback/core';
import {getModelSchemaRef, post, requestBody} from '@loopback/rest';
import {
CONTENT_TYPE,
OPERATION_SECURITY_SPEC,
rateLimitKeyGenPublic,
STATUS_CODE,
CONTENT_TYPE,
OPERATION_SECURITY_SPEC,
rateLimitKeyGenPublic,
STATUS_CODE,
} from '@sourceloop/core';
import { authorize } from 'loopback4-authorization';
import { ratelimit } from 'loopback4-ratelimiter';
import { TenantManagementServiceBindings, WEBHOOK_VERIFIER } from '../keys';
import { IdpDetailsDTO } from '../models/dtos/idp-details-dto.model';
import { ConfigureIdpFunc, IdPKey } from '../types';
import {authorize} from 'loopback4-authorization';
import {ratelimit} from 'loopback4-ratelimiter';
import {TenantManagementServiceBindings, WEBHOOK_VERIFIER} from '../keys';
import {IdpDetailsDTO} from '../models/dtos/idp-details-dto.model';
import {ConfigureIdpFunc, IdPKey} from '../types';

const basePath = '/manage/users';
export class IdpController {
constructor(
@inject(TenantManagementServiceBindings.IDP_KEYCLOAK)
private readonly idpKeycloakProvider:ConfigureIdpFunc<IdpDetailsDTO>
) { }
@intercept(WEBHOOK_VERIFIER)
@ratelimit(true, {
max: parseInt(process.env.WEBHOOK_API_MAX_ATTEMPTS ?? '10'),
keyGenerator: rateLimitKeyGenPublic,
})
@authorize({
permissions: ['*'],
})
@post(`${basePath}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'Webhook success',
},
constructor(
@inject(TenantManagementServiceBindings.IDP_KEYCLOAK)
private readonly idpKeycloakProvider: ConfigureIdpFunc<IdpDetailsDTO>,
@inject(TenantManagementServiceBindings.IDP_AUTH0)
private readonly idpAuth0Provider: ConfigureIdpFunc<IdpDetailsDTO>,
) {}
@intercept(WEBHOOK_VERIFIER)
@ratelimit(true, {
max: parseInt(process.env.WEBHOOK_API_MAX_ATTEMPTS ?? '10'),
keyGenerator: rateLimitKeyGenPublic,
})
@authorize({
permissions: ['*'],
})
@post(`${basePath}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'Webhook success',
},
},
})
async idpConfigure(
@requestBody({
content: {
[CONTENT_TYPE.JSON]: {
schema: getModelSchemaRef(IdpDetailsDTO, {
title: 'IdpDetailsDTO',
}),
},
},
})
async idpConfigure(
@requestBody({
content: {
[CONTENT_TYPE.JSON]: {
schema: getModelSchemaRef(IdpDetailsDTO, {
title: 'IdpDetailsDTO',
}),
},
},
})
payload: IdpDetailsDTO,
): Promise<void> {
switch (payload.identityProvider) {
case IdPKey.AUTH0:

break;
case IdPKey.COGNITO:

break;
case IdPKey.KEYCLOAK:
await this.idpKeycloakProvider(payload);
break;

default:
break;
}
payload: IdpDetailsDTO,
): Promise<void> {
switch (payload.identityProvider) {
case IdPKey.AUTH0:
await this.idpAuth0Provider(payload);
break;
case IdPKey.COGNITO:
break;
case IdPKey.KEYCLOAK:
await this.idpKeycloakProvider(payload);
break;

default:
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import {
repository,
} from '@loopback/repository';
import {
param,
get,
getModelSchemaRef,
} from '@loopback/rest';
import {
TenantConfig,
Tenant,
} from '../models';
import {repository} from '@loopback/repository';
import {param, get, getModelSchemaRef} from '@loopback/rest';
import {TenantConfig, Tenant} from '../models';
import {TenantConfigRepository} from '../repositories';

import {authenticate, STRATEGY} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {PermissionKey} from '../permissions';
import {OPERATION_SECURITY_SPEC, STATUS_CODE} from '@sourceloop/core';
const basePath = '/tenant-configs/{id}/tenant';
export class TenantConfigTenantController {
constructor(
@repository(TenantConfigRepository)
public tenantConfigRepository: TenantConfigRepository,
) { }

@get('/tenant-configs/{id}/tenant', {
) {}
@authorize({
permissions: [PermissionKey.ViewTenantConfig],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@get(`${basePath}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
'200': {
[STATUS_CODE.OK]: {
description: 'Tenant belonging to TenantConfig',
content: {
'application/json': {
Expand Down
Loading

0 comments on commit 0ecf67f

Please sign in to comment.