Skip to content

Commit

Permalink
added identity module
Browse files Browse the repository at this point in the history
  • Loading branch information
KDwevedi committed Dec 24, 2024
1 parent 8f33f88 commit fbd6173
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ lerna-debug.log*
!.vscode/launch.json
!.vscode/extensions.json
.env
.env.archive
secret.json
pdfs
data
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SchemaModule } from './schema/schema.module';
import { TemplateModule } from './template/template.module';
import { CertificateModule } from './certificate/certificate.module';
import { PrismaModule } from 'src/prisma/prisma.module';
import { IdentityModule } from './identity/identity.module';
import configuration from './config/configuration';

@Module({
Expand All @@ -23,6 +24,7 @@ import configuration from './config/configuration';
TemplateModule,
CertificateModule,
PrismaModule,
IdentityModule,
],
controllers: [AppController, SchemaController],
providers: [AppService],
Expand Down
25 changes: 25 additions & 0 deletions src/identity/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"content": [
{
"alsoKnownAs": [
"C4GT",
"https://www.codeforgovtech.in/"
],
"services": [
{
"id": "C4GT",
"type": "IdentityHub",
"serviceEndpoint": {
"@context": "schema.c4gt.acknowledgment",
"@type": "UserServiceEndpoint",
"instance": [
"https://www.codeforgovtech.in"
]
}
}
],
"method": "C4GT"
}
]
}

18 changes: 18 additions & 0 deletions src/identity/identity.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { IdentityController } from './identity.controller';

describe('IdentityController', () => {
let controller: IdentityController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [IdentityController],
}).compile();

controller = module.get<IdentityController>(IdentityController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
17 changes: 17 additions & 0 deletions src/identity/identity.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Controller, Post, Get, Body, Param } from '@nestjs/common';
import { IdentityService } from './identity.service';

@Controller('identity')
export class IdentityController {
constructor(private readonly identityService: IdentityService) {}

@Post('generate')
async generateIdentity() {
return this.identityService.generateIdentity();
}

@Get('resolve/:did')
async resolveIdentity(@Param('did') did: string) {
return this.identityService.resolveIdentity(did);
}
}
11 changes: 11 additions & 0 deletions src/identity/identity.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { IdentityService } from './identity.service';
import { IdentityController } from './identity.controller';
import { HttpModule } from '@nestjs/axios';

@Module({
imports: [HttpModule],
providers: [IdentityService],
controllers: [IdentityController]
})
export class IdentityModule {}
18 changes: 18 additions & 0 deletions src/identity/identity.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { IdentityService } from './identity.service';

describe('IdentityService', () => {
let service: IdentityService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [IdentityService],
}).compile();

service = module.get<IdentityService>(IdentityService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
52 changes: 52 additions & 0 deletions src/identity/identity.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
import * as fs from 'fs';
import * as path from 'path';

interface RCWIdentityConfig {
baseUrl: string;
}

@Injectable()
export class IdentityService {
private readonly config;

constructor(
private readonly configService: ConfigService,
private readonly httpService: HttpService
) {
this.config = this.configService.get<RCWIdentityConfig>('identityService');
}

async generateIdentity() {
try {
const configPath = path.resolve(__dirname, '../config.json');
const requestBody = JSON.parse(fs.readFileSync(configPath, 'utf8'));

const generateUrl = `${this.config.baseUrl}/did/generate`;

const response = await this.httpService.axiosRef.post(generateUrl, requestBody, {
headers: { 'Content-Type': 'application/json' },
});
return response.data;
} catch (error) {
console.error('Error generating identity:', error.message);
throw error.response?.data || error.message;
}
}

async resolveIdentity(did: string) {
try {
const url = `${this.config.baseUrl}/did/resolve/${did}`;
const response = await this.httpService.axiosRef.get(url, {
headers: { 'Content-Type': 'application/json' },
});
return response.data;
} catch (error) {
console.error('Error resolving identity:', error.message);
throw error.response?.data || error.message;
}
}
}

0 comments on commit fbd6173

Please sign in to comment.