Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
175 changes: 142 additions & 33 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,12 @@ 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 {
EXPIRY_CONFIG,
getSecondsFromUnit,
TIME_UNIT,
} from 'src/utils/time-constant';

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

async createAnApp(
createAppDto: CreateAppDto,
Expand Down Expand Up @@ -393,9 +400,8 @@ export class AppAuthService {
}

private async verifyDNS01(domain: URL, txt: string) {
const resolveDNSURL = `https://dns.google/resolve?name=${
new URL(domain).host
}&type=TXT`;
const resolveDNSURL = `https://dns.google/resolve?name=${new URL(domain).host
}&type=TXT`;
const actuaTxt = txt;
const res = await fetch(resolveDNSURL, {
headers: {
Expand Down Expand Up @@ -435,7 +441,7 @@ export class AppAuthService {
if (fetchedTxtRecord && fetchedTxtRecord.error) {
throw new BadRequestException([
fetchedTxtRecord.error?.message +
'. If you have already added then it may take a while to complete. Please try again in sometime.',
'. If you have already added then it may take a while to complete. Please try again in sometime.',
]);
}
if (fetchedTxtRecord.verified) {
Expand Down Expand Up @@ -541,7 +547,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 +667,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 +697,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 +735,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 +770,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 +793,37 @@ 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(
data,
time = 4,
unit: TIME_UNIT = TIME_UNIT.HOUR,
) {
const secret = this.config.get('JWT_SECRET');
const token = await this.jwt.signAsync(data, {
expiresIn: `${time}${unit}`,
secret,
});
const expiresIn = getSecondsFromUnit(time, unit);
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 +852,22 @@ 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' };
Logger.log('storeDataInRedis() method: ends....', 'AppAuthService');
redisClient.set(
sessionId,
JSON.stringify(payload),
'EX',
EXPIRY_CONFIG.SERVICE_ACCESS.redisExpiryTime,
);
}

//access_service_ssi
//access_service_kyc

async grantPermission(
grantType: string,
appId: string,
user,
): Promise<{ access_token; expiresIn; tokenType }> {
const sessionId = `${appId}_${Context.idDashboard}`;
const savedSession = await redisClient.get(sessionId);
switch (grantType) {
case GRANT_TYPES.access_service_ssi:
break;
Expand All @@ -796,20 +880,34 @@ export class AppAuthService {
default: {
throw new BadRequestException([
'Grant type not supported, supported grant types are: ' +
GRANT_TYPES.access_service_kyc +
',' +
GRANT_TYPES.access_service_ssi,
GRANT_TYPES.access_service_kyc +
',' +
GRANT_TYPES.access_service_ssi,
]);
}
}

if (savedSession) {
const app = JSON.parse(savedSession);
const dataToStore = {
appId,
appName: app.appName,
grantType,
subdomain: app.subdomain,
sessionId,
};
return this.getAccessToken(
dataToStore,
EXPIRY_CONFIG.SERVICE_ACCESS.jwtTime,
EXPIRY_CONFIG.SERVICE_ACCESS.jwtUnit,
);
}
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 +966,18 @@ 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,
EXPIRY_CONFIG.SERVICE_ACCESS.jwtTime,
EXPIRY_CONFIG.SERVICE_ACCESS.jwtUnit,
);
}
}
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