Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team #34

Merged
merged 26 commits into from
Aug 30, 2024
Merged

Team #34

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 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
18 changes: 17 additions & 1 deletion src/app-auth/app-auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
Module,
NestModule,
RequestMethod,
forwardRef,
} from '@nestjs/common';

import { AppAuthService } from './services/app-auth.service';
Expand All @@ -24,23 +23,36 @@ import { SupportedServiceList } from 'src/supported-service/services/service-lis
import { JWTAuthorizeMiddleware } from 'src/utils/middleware/jwt-authorization.middleware';
import { UserModule } from 'src/user/user.module';
import { TwoFAAuthorizationMiddleware } from 'src/utils/middleware/2FA-jwt-authorization.middleware';
import { CreditModule } from 'src/credits/credits.module';
import { JWTAccessAccountMiddleware } from 'src/utils/middleware/jwt-accessAccount.middlerwere';
import { AdminPeopleRepository } from 'src/people/repository/people.repository';
import {
AdminPeople,
AdminPeopleSchema,
} from 'src/people/schema/people.schema';

@Module({
imports: [
MongooseModule.forFeature([{ name: App.name, schema: AppSchema }]),
MongooseModule.forFeature([
{ name: AdminPeople.name, schema: AdminPeopleSchema },
]),
HidWalletModule,
EdvModule,
UserModule,
JwtModule.register({}),
CreditModule,
],
providers: [
AppAuthService,
AppRepository,

HidWalletService,
AppAuthSecretService,
AppAuthApiKeyService,
SupportedServiceList,
SupportedServiceService,
AdminPeopleRepository,
],
controllers: [AppAuthController],

Expand All @@ -61,6 +73,10 @@ export class AppAuthModule implements NestModule {
.apply(JWTAuthorizeMiddleware)
.exclude({ path: '/api/v1/app/marketplace', method: RequestMethod.GET })
.forRoutes(AppAuthController);
consumer
.apply(JWTAccessAccountMiddleware)
.exclude({ path: '/api/v1/app/marketplace', method: RequestMethod.GET })
.forRoutes(AppAuthController);
consumer
.apply(TwoFAAuthorizationMiddleware)
.exclude({ path: '/api/v1/app/marketplace', method: RequestMethod.GET })
Expand Down
18 changes: 10 additions & 8 deletions src/app-auth/services/app-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
MSG_UPDATE_CREDENTIAL_STATUS,
MSG_UPDATE_DID_TYPEURL,
} from 'src/utils/authz';
import { AuthzCreditService } from 'src/credits/services/credits.service';

enum GRANT_TYPES {
access_service_kyc = 'access_service_kyc',
Expand All @@ -53,6 +54,7 @@ export class AppAuthService {
private readonly appAuthApiKeyService: AppAuthApiKeyService,
private readonly supportedServices: SupportedServiceService,
private readonly userRepository: UserRepository,
private readonly authzCreditService: AuthzCreditService,
) {}

async createAnApp(
Expand Down Expand Up @@ -149,9 +151,7 @@ export class AppAuthService {
'AppAuthService',
);
const subdomain = await this.getRandomSubdomain();

// AUTHZ

if (service.id == SERVICE_TYPES.SSI_API) {
// Perform AuthZ Grant
const authGrantTxnMsgAndFeeDID = await generateAuthzGrantTxnMessage(
Expand Down Expand Up @@ -188,7 +188,6 @@ export class AppAuthService {
this.authzWalletInstance.address,
this.config.get('BASIC_ALLOWANCE') || '5000000uhid',
);

await this.granterClient.signAndBroadcast(
this.authzWalletInstance.address,
[
Expand All @@ -202,6 +201,11 @@ export class AppAuthService {
authGrantTxnMsgAndFeeDID.fee,
);
}

await this.authzCreditService.createAuthzCredits({
userId,
appId,
});
// Finally stroring application in db
// const txns = {
// transactionHash: '',
Expand Down Expand Up @@ -682,8 +686,8 @@ export class AppAuthService {

async grantPermission(
grantType: string,
userId: string,
appId: string,
user,
): Promise<{ access_token; expiresIn; tokenType }> {
switch (grantType) {
case GRANT_TYPES.access_service_ssi:
Expand All @@ -700,16 +704,14 @@ export class AppAuthService {
}
}

const app = await this.getAppById(appId, userId);
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 = await this.userRepository.findOne({
userId: app.userId,
});
const userDetails = user;
if (!userDetails) {
throw new UnauthorizedException([
'You do not have access to this service',
Expand Down
4 changes: 2 additions & 2 deletions src/app-oauth/app-oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ export class AppOauthController {
@Req() request,
): Promise<{ access_token; expiresIn; tokenType }> {
const { user } = request;
const { userId } = user;
//
Logger.log('reGenerateAppSecretKey() method: starts', 'AppOAuthController');
return this.appAuthService.grantPermission(grantType, userId, serviceId);

return this.appAuthService.grantPermission(grantType, serviceId, user);
}
}
21 changes: 19 additions & 2 deletions src/app-oauth/app-oauth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,33 @@ import { AppOauthController } from './app-oauth.controller';
import { AppAuthModule } from 'src/app-auth/app-auth.module';
import { JWTAuthorizeMiddleware } from 'src/utils/middleware/jwt-authorization.middleware';
import { UserModule } from 'src/user/user.module';
import { JWTAccessAccountMiddleware } from 'src/utils/middleware/jwt-accessAccount.middlerwere';
import { MongooseModule } from '@nestjs/mongoose';
import { AdminPeopleRepository } from 'src/people/repository/people.repository';
import {
AdminPeople,
AdminPeopleSchema,
} from 'src/people/schema/people.schema';
@Module({
imports: [AppAuthModule, UserModule],
imports: [
AppAuthModule,
UserModule,
MongooseModule.forFeature([
{ name: AdminPeople.name, schema: AdminPeopleSchema },
]),
],
controllers: [AppOauthController],
providers: [],
providers: [AdminPeopleRepository],
})
export class AppOauthModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(JWTAuthorizeMiddleware)
.exclude({ path: 'api/v1/app/oauth', method: RequestMethod.POST })
.forRoutes(AppOauthController);
consumer
.apply(JWTAccessAccountMiddleware)
.exclude({ path: 'api/v1/app/oauth', method: RequestMethod.POST })
.forRoutes(AppOauthController);
}
}
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ import { UserModule } from './user/user.module';
import { SupportedServiceModule } from './supported-service/supported-service.module';
import { SocialLoginModule } from './social-login/social-login.module';
import { HypersignauthLoginModule } from './hypersignauth-login/hypersignauth-login.module';
import { CreditModule } from './credits/credits.module';
import { TeamModule } from './roles/role.module';
import { PeopleModule } from './people/people.module';

@Module({
imports: [
AppAuthModule,
CreditModule,
ConfigModule.forRoot({
envFilePath: '',
isGlobal: true,
Expand All @@ -27,6 +31,8 @@ import { HypersignauthLoginModule } from './hypersignauth-login/hypersignauth-lo
SupportedServiceModule,
SocialLoginModule,
HypersignauthLoginModule,
TeamModule,
PeopleModule,
],
controllers: [],
providers: [
Expand Down
25 changes: 25 additions & 0 deletions src/credits/controllers/credits.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { UseFilters, Controller, Get, Query, Req } from '@nestjs/common';
import { ApiBearerAuth, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';

Check warning on line 2 in src/credits/controllers/credits.controller.ts

View workflow job for this annotation

GitHub Actions / build

'ApiResponse' is defined but never used
import { AllExceptionsFilter } from 'src/utils/utils';
import { AuthzCreditService } from '../services/credits.service';
import { GetCreditsDto } from '../dtos/credits.dto';

@UseFilters(AllExceptionsFilter)
@ApiTags('Credits')
@Controller('/api/v1/credits')
export class CreditsController {
constructor(private readonly creditService: AuthzCreditService) {}
@ApiBearerAuth('Authorization')
@Get('/app')
@ApiQuery({
name: 'appId',
example: 'appId',
description: 'Provide appId',
})
async getCreditByAppId(@Req() req: any, @Query() query: GetCreditsDto) {
const userId = req.user.userId;

const appId = query.appId;
return this.creditService.getCreditDetails(appId, userId);
}
}
40 changes: 40 additions & 0 deletions src/credits/credits.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';
import { AuthZCredits, AuthZCreditsSchema } from './schemas/authz.schema';
import { AuthZCreditsRepository } from './repositories/authz.repository';
import { AuthzCreditService } from './services/credits.service';
import { CreditsController } from './controllers/credits.controller';
import { JWTAuthorizeMiddleware } from 'src/utils/middleware/jwt-authorization.middleware';
import { UserModule } from 'src/user/user.module';
import { JWTAccessAccountMiddleware } from 'src/utils/middleware/jwt-accessAccount.middlerwere';
import { AdminPeopleRepository } from 'src/people/repository/people.repository';
import {
AdminPeople,
AdminPeopleSchema,
} from 'src/people/schema/people.schema';

@Module({
imports: [
UserModule,
MongooseModule.forFeature([
{ name: AuthZCredits.name, schema: AuthZCreditsSchema },
]),
MongooseModule.forFeature([
{ name: AdminPeople.name, schema: AdminPeopleSchema },
]),
],
controllers: [CreditsController],
providers: [
AuthZCreditsRepository,
AdminPeopleRepository,
AuthzCreditService,
],

exports: [AuthZCreditsRepository, AuthzCreditService],
})
export class CreditModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(JWTAuthorizeMiddleware).forRoutes(CreditsController);
consumer.apply(JWTAccessAccountMiddleware).forRoutes(CreditsController);
}
}
12 changes: 12 additions & 0 deletions src/credits/dtos/credits.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';

export class GetCreditsDto {
@ApiProperty({
name: 'appId',
default: 'appId',
})
@IsNotEmpty()
@IsString()
appId: string;
}
20 changes: 20 additions & 0 deletions src/credits/repositories/authz.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Injectable } from '@nestjs/common';
import { FilterQuery, Model } from 'mongoose';
import { AuthZCredits, AuthZCreditsDocument } from '../schemas/authz.schema';
import { InjectModel } from '@nestjs/mongoose';

@Injectable()
export class AuthZCreditsRepository {
constructor(
@InjectModel(AuthZCredits.name)
private readonly authZCreditModel: Model<AuthZCreditsDocument>,
) {}
async create(authZCredits: AuthZCredits): Promise<AuthZCredits> {
const newAuthZCredits = new this.authZCreditModel(authZCredits);
return newAuthZCredits.save();
}

async find(authZCreditsFilterQuery: FilterQuery<AuthZCredits>) {
return this.authZCreditModel.find(authZCreditsFilterQuery);
}
}
60 changes: 60 additions & 0 deletions src/credits/schemas/authz.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { IsNotEmpty, IsString } from 'class-validator';

export enum scope {
MsgRegisterDID = 'MsgRegisterDID',
MsgUpdateDID = 'MsgUpdateDID',
MsgDeactivateDID = 'MsgDeactivateDID',
MsgRegisterCredentialSchema = 'MsgRegisterCredentialSchema',
MsgRegisterCredentialStatus = 'MsgRegisterCredentialStatus',
MsgUpdateCredentialStatus = 'MsgUpdateCredentialStatus',
}

export type AuthZCreditsDocument = AuthZCredits & Document;

@Schema()
class Amount {
@Prop({
type: String,
})
denom: string;

@Prop({
type: String,
})
amount: string;
}

@Schema({ timestamps: true })
export class AuthZCredits {
@IsNotEmpty()
@IsString()
@Prop({
required: true,
})
userId: string;

@IsNotEmpty()
@IsString()
@Prop({
required: true,
})
appId: string;

@Prop({
type: Date,
})
expires: string;

@Prop({
type: Amount,
})
credit: Amount;
@Prop({
type: [String],
enum: scope,
})
creditScope: Array<scope>;
}

export const AuthZCreditsSchema = SchemaFactory.createForClass(AuthZCredits);
Loading
Loading