Skip to content

Commit

Permalink
Merge pull request #32 from hypersign-protocol/authz-model
Browse files Browse the repository at this point in the history
Authz model
  • Loading branch information
Pratap2018 authored Aug 20, 2024
2 parents 48e8218 + 3678c77 commit 320c14e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 4 deletions.
4 changes: 3 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,6 +23,7 @@ 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';

@Module({
imports: [
Expand All @@ -32,10 +32,12 @@ import { TwoFAAuthorizationMiddleware } from 'src/utils/middleware/2FA-jwt-autho
EdvModule,
UserModule,
JwtModule.register({}),
CreditModule,
],
providers: [
AppAuthService,
AppRepository,

HidWalletService,
AppAuthSecretService,
AppAuthApiKeyService,
Expand Down
10 changes: 7 additions & 3 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
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ 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';

@Module({
imports: [
AppAuthModule,
CreditModule,
ConfigModule.forRoot({
envFilePath: '',
isGlobal: true,
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';
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);
}
}
27 changes: 27 additions & 0 deletions src/credits/credits.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 { UserRepository } from 'src/user/repository/user.repository';
import { UserModule } from 'src/user/user.module';

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

exports: [AuthZCreditsRepository, AuthzCreditService],
})
export class CreditModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(JWTAuthorizeMiddleware).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);
}
}
45 changes: 45 additions & 0 deletions src/credits/schemas/authz.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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({ 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()
creditAmmountInUhid: string;
@Prop({
type: [String],
enum: scope,
})
creditScope: Array<scope>;
}

export const AuthZCreditsSchema = SchemaFactory.createForClass(AuthZCredits);
37 changes: 37 additions & 0 deletions src/credits/services/credits.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common';
import { AuthZCreditsRepository } from '../repositories/authz.repository';
import { scope } from '../../credits/schemas/authz.schema';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class AuthzCreditService {
constructor(
private readonly authzCreditsRepository: AuthZCreditsRepository,
private readonly config: ConfigService,
) {}

async createAuthzCredits(authz: { userId; appId }) {
return await this.authzCreditsRepository.create({
userId: authz.userId,
appId: authz.appId,
expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toISOString(),
// created: new Date().toISOString(),
creditAmmountInUhid: this.config.get('BASIC_ALLOWANCE') || '5000000uhid',
creditScope: [
scope.MsgRegisterDID,
scope.MsgDeactivateDID,
scope.MsgRegisterCredentialSchema,
scope.MsgUpdateDID,
scope.MsgUpdateCredentialStatus,
scope.MsgRegisterCredentialStatus,
],
});
}

async getCreditDetails(appId, userId) {
return this.authzCreditsRepository.find({
userId,
appId,
});
}
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { UserModule } from './user/user.module';
import { randomUUID } from 'crypto';
import { SupportedServiceModule } from './supported-service/supported-service.module';
import { SocialLoginModule } from './social-login/social-login.module';
import { CreditModule } from './credits/credits.module';

// eslint-disable-next-line
const HypersignAuth = require('hypersign-auth-node-sdk');
Expand Down Expand Up @@ -130,6 +131,7 @@ async function bootstrap() {
const orgDocuments = SwaggerModule.createDocument(app, orgDocConfig, {
include: [
AppAuthModule,
CreditModule,
AppOauthModule,
UserModule,
SupportedServiceModule,
Expand Down

0 comments on commit 320c14e

Please sign in to comment.