forked from hypersign-protocol/entity-api-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from hypersign-protocol/authz-model
Authz model
- Loading branch information
Showing
10 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters