Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
142 changes: 116 additions & 26 deletions src/app-auth/services/app-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as url from 'url';
import { SupportedServiceService } from 'src/supported-service/services/supported-service.service';
import {
APP_ENVIRONMENT,
Context,
SERVICE_TYPES,
} from 'src/supported-service/services/iServiceList';
import { UserRepository } from 'src/user/repository/user.repository';
Expand All @@ -35,6 +36,8 @@ import { CustomerOnboarding } from 'src/customer-onboarding/schemas/customer-onb
import { Model } from 'mongoose';
import { getAccessListForModule } from 'src/utils/utils';
import { TokenModule } from 'src/config/access-matrix';
import { redisClient } from 'src/utils/redis.provider';
import { TIME } from 'src/utils/time-constant';

export enum GRANT_TYPES {
access_service_kyc = 'access_service_kyc',
Expand All @@ -61,7 +64,7 @@ export class AppAuthService {
@InjectModel(CustomerOnboarding.name)
private readonly onboardModel: Model<CustomerOnboarding>,
private readonly webpageConfigRepo: WebPageConfigRepository,
) {}
) { }

async createAnApp(
createAppDto: CreateAppDto,
Expand Down Expand Up @@ -541,7 +544,49 @@ export class AppAuthService {
{ appId, userId },
updataAppDto,
);
return this.getAppResponse(app);
const updatedapp = await this.getAppResponse(app);
// update redis

const baseKey = appId;
const dashboardRedisKey = `${appId}_${Context.idDashboard}`;

const updatedFields = {
whitelistedCors: updatedapp.whitelistedCors,
env: updatedapp.env ?? APP_ENVIRONMENT.dev,
appName: updatedapp.appName,
};

const [baseDataString, dashboardDataString] = await Promise.all([
redisClient.get(baseKey),
redisClient.get(dashboardRedisKey),
]);

const updatePromises = [];

if (baseDataString) {
const baseData = JSON.parse(baseDataString);
const updatedBase = { ...baseData, ...updatedFields };
updatePromises.push(
redisClient.set(baseKey, JSON.stringify(updatedBase), 'KEEPTTL'),
);
}

if (dashboardDataString) {
const dashboardData = JSON.parse(dashboardDataString);
const updatedDashboard = { ...dashboardData, ...updatedFields };
updatePromises.push(
redisClient.set(
dashboardRedisKey,
JSON.stringify(updatedDashboard),
'KEEPTTL',
),
);
}

if (updatePromises.length > 0) {
await Promise.all(updatePromises);
}
return updatedapp;
}

async deleteApp(appId: string, userId: string): Promise<DeleteAppResponse> {
Expand Down Expand Up @@ -619,6 +664,12 @@ export class AppAuthService {
}
this.authzCreditRepository.deleteAuthzDetail({ appId });
appDetail = await this.appRepository.findOneAndDelete({ appId, userId });
// delete from redis
await Promise.all([
redisClient.del(appId),
redisClient.del(`${appId}_${Context.idDashboard}`),
]);
Logger.debug(`Redis cache cleaned for appId: ${appId}`);
return { appId: appDetail.appId };
}

Expand All @@ -643,7 +694,6 @@ export class AppAuthService {
grantType,
): Promise<{ access_token; expiresIn; tokenType }> {
Logger.log('generateAccessToken() method: starts....', 'AppAuthService');

const apikeyIndex = appSecreatKey.split('.')[0];
const appDetail = await this.appRepository.findOne({
apiKeyPrefix: apikeyIndex,
Expand Down Expand Up @@ -682,11 +732,25 @@ export class AppAuthService {
const serviceType = appDetail.services[0]?.id; // TODO: remove this later
let grant_type = '';
let accessList = [];
const redisKey = appDetail.appId;
const savedSession = await redisClient.get(redisKey);
if (savedSession) {
Logger.log('Using redis cached session', 'AppAuthService');
const sessionJson = JSON.parse(savedSession);
const jwtPayload = {
appId: sessionJson.appId,
appName: sessionJson.appName,
grantType: sessionJson.grantType,
subdomain: sessionJson.subdomain,
sessionId: redisKey,
};
return this.getAccessToken(jwtPayload, expiresin);
}
switch (serviceType) {
case SERVICE_TYPES.SSI_API: {
grant_type = GRANT_TYPES.access_service_ssi;
accessList = getAccessListForModule(
TokenModule.VERIFIER,
TokenModule.APP_AUTH,
SERVICE_TYPES.SSI_API,
);
break;
Expand All @@ -703,15 +767,15 @@ export class AppAuthService {
}
grant_type = grantType || GRANT_TYPES.access_service_kyc;
accessList = getAccessListForModule(
TokenModule.VERIFIER,
TokenModule.APP_AUTH,
SERVICE_TYPES.CAVACH_API,
);
break;
}
case SERVICE_TYPES.QUEST: {
grant_type = GRANT_TYPES.access_service_quest;
accessList = getAccessListForModule(
TokenModule.VERIFIER,
TokenModule.APP_AUTH,
SERVICE_TYPES.QUEST,
);
break;
Expand All @@ -726,15 +790,33 @@ export class AppAuthService {
`You are not authorized to access service of type ${serviceType}`,
]);
}

return this.getAccessToken(grant_type, appDetail, expiresin, accessList);
const jwtPayload = {
appId: appDetail.appId,
appName: appDetail.appName,
grantType: grant_type,
subdomain: appDetail.subdomain,
sessionId: redisKey,
};
await this.storeDataInRedis(grant_type, appDetail, accessList, redisKey);
return this.getAccessToken(jwtPayload, expiresin);
}

public async getAccessToken(
public async getAccessToken(data, expiresin = 4) {
const secret = this.config.get('JWT_SECRET');
const token = await this.jwt.signAsync(data, {
expiresIn: expiresin.toString() + 'h',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if I want to issue tokens in like min , I can not do becuae the expiry is always in hourh?

secret,
});
const expiresIn = (expiresin * 1 * 60 * 60 * 1000) / 1000;
Logger.log('generateAccessToken() method: ends....', 'AppAuthService');

return { access_token: token, expiresIn, tokenType: 'Bearer' };
}
public async storeDataInRedis(
grantType,
appDetail,
expiresin = 4,
accessList = [],
sessionId,
) {
const payload = {
appId: appDetail.appId,
Expand Down Expand Up @@ -763,27 +845,18 @@ export class AppAuthService {
) {
payload['dependentServices'] = appDetail.dependentServices;
}

const secret = this.config.get('JWT_SECRET');

const token = await this.jwt.signAsync(payload, {
expiresIn: expiresin.toString() + 'h',
secret,
});
const expiresIn = (expiresin * 1 * 60 * 60 * 1000) / 1000;
Logger.log('generateAccessToken() method: ends....', 'AppAuthService');

return { access_token: token, expiresIn, tokenType: 'Bearer' };
redisClient.set(sessionId, JSON.stringify(payload), 'EX', TIME.WEEK);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep expiry in one place for reddis

}

//access_service_ssi
//access_service_kyc

async grantPermission(
grantType: string,
appId: string,
user,
): Promise<{ access_token; expiresIn; tokenType }> {
const context = Context.idDashboard;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any where else in this function , use of this variable context ??

I can see its used in const sessionId = ${appId}_${context};

but if we do not use it anywhere elese then we can directly use this like const sessionId = ${appId}_${Context.idDashboard}` ?

const sessionId = `${appId}_${context}`;
const savedSession = await redisClient.get(sessionId);
switch (grantType) {
case GRANT_TYPES.access_service_ssi:
break;
Expand All @@ -803,13 +876,23 @@ export class AppAuthService {
}
}

if (savedSession) {
const app = JSON.parse(savedSession);
const dataToStore = {
appId,
appName: app.appName,
grantType,
subdomain: app.subdomain,
sessionId,
};
return this.getAccessToken(dataToStore, 12);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep this expiry 12 some where in one place? so that we can look at that file and would know whats this exipry for?

}
const app = await this.getAppById(appId, user.userId);
if (!app) {
throw new BadRequestException([
'Invalid service id or you do not have access of this service',
]);
}

const userDetails = user;
if (!userDetails) {
throw new UnauthorizedException([
Expand Down Expand Up @@ -868,7 +951,14 @@ export class AppAuthService {
`You are not authorized to access service of type ${serviceType}`,
]);
}

return this.getAccessToken(grantType, app, 12, accessList);
const tokenPayload = {
appId,
appName: app.appName,
grantType,
subdomain: app.subdomain,
sessionId,
};
await this.storeDataInRedis(grantType, app, accessList, sessionId);
return this.getAccessToken(tokenPayload, 12);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep this expiry 12 some where in one place? so that we can look at that file and would know whats this exipry for?

}
}
2 changes: 2 additions & 0 deletions src/config/access-matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ export const KYC_ACCESS_MATRIX = {
SERVICES.CAVACH_API.ACCESS_TYPES.CHECK_LIVE_STATUS,
SERVICES.CAVACH_API.ACCESS_TYPES.READ_WIDGET_CONFIG,
SERVICES.CAVACH_API.ACCESS_TYPES.READ_USER_CONSENT,
SERVICES.CAVACH_API.ACCESS_TYPES.WRITE_AUTH,
],
[TokenModule.APP_AUTH]: [
SERVICES.CAVACH_API.ACCESS_TYPES.WRITE_SESSION,
SERVICES.CAVACH_API.ACCESS_TYPES.WRITE_USER_CONSENT,
SERVICES.CAVACH_API.ACCESS_TYPES.WRITE_PASSIVE_LIVELINESS,
SERVICES.CAVACH_API.ACCESS_TYPES.WRITE_DOC_OCR,
Expand Down
8 changes: 7 additions & 1 deletion src/credits/credits.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ export class CreditModule implements NestModule {
method: RequestMethod.GET,
})
.forRoutes(CreditsController);
consumer.apply(JWTAccessAccountMiddleware).forRoutes(CreditsController);
consumer
.apply(JWTAccessAccountMiddleware)
.exclude({
path: '/api/v1/credits/authz/:appId',
method: RequestMethod.GET,
})
.forRoutes(CreditsController);
consumer.apply(RateLimitMiddleware).forRoutes(CreditsController);
}
}
5 changes: 2 additions & 3 deletions src/customer-onboarding/dto/create-customer-onboarding.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '../constants/enum';
import { IsPhoneNumberByCountry } from 'src/utils/customDecorator/validate-phone-no-country.decorator';
import { Type } from 'class-transformer';
import { IsUrlOrBase64Image } from 'src/utils/customDecorator/IsUrlOrBase64Image.decorator';

export class CustomerOnboardingBasicDto {
@ApiProperty({
Expand All @@ -47,9 +48,7 @@ export class CustomerOnboardingBasicDto {
@IsOptional()
@IsNotEmpty()
@IsString()
@IsUrl({
require_protocol: true,
})
@IsUrlOrBase64Image()
companyLogo?: string;
@ApiProperty({
name: 'customerEmail',
Expand Down
Loading