Commit 1e7ae146 authored by Hidde-Jan Jongsma's avatar Hidde-Jan Jongsma

Add request creation

parent 499f075a
This diff is collapsed.
import { Organization } from 'src/organizations/organization.entity';
import { CredentialIssueRequest } from 'src/requests/credential-issue-request.entity';
import { CredentialVerifyRequest } from 'src/requests/credential-verify-request.entity';
/**
* Interface describing a Connector that can be used by the SSI-service
......@@ -13,6 +15,30 @@ export interface ConnectorService {
*/
registerOrganization(org: Organization): Promise<void>;
/**
* Check if the connector can issue a credential for this request.
*/
canIssueCredentialRequest(
request: CredentialIssueRequest,
): Promise<boolean> | boolean;
/**
* Check if the connector can verify a credential for this request.
*/
canVerifyCredentialRequest(
request: CredentialVerifyRequest,
): Promise<boolean> | boolean;
/**
* Handle fullfillment of a issue request.
*/
handleIssueCredentialRequest(request: CredentialIssueRequest): Promise<any>;
/**
* Handle fullfillment of a verify request.
*/
handleVerifyCredentialRequest(request: CredentialIssueRequest): Promise<any>;
// registerRoutes(root: string, app: Express): void;
// processCredentialIssueRequest(request: CredentialIssueRequest): Promise<void>;
......
......@@ -3,6 +3,8 @@ import { JolocomService } from './jolocom/jolocom.service';
import { IrmaService } from './irma/irma.service';
import { ConnectorService } from './connector-service.interface';
import { Organization } from '../organizations/organization.entity';
import { CredentialIssueRequest } from 'src/requests/credential-issue-request.entity';
import { CredentialVerifyRequest } from 'src/requests/credential-verify-request.entity';
@Injectable()
export class ConnectorsService {
......@@ -27,4 +29,28 @@ export class ConnectorsService {
),
);
}
async availableIssueConnectors(request: CredentialIssueRequest) {
const results = await Promise.all(
this.connectors.map(async connector => {
return {
connector,
available: await connector.canIssueCredentialRequest(request),
};
}),
);
return results.filter(r => r.available).map(r => r.connector);
}
async availableVerifyConnectors(request: CredentialVerifyRequest) {
const results = await Promise.all(
this.connectors.map(async connector => {
return {
connector,
available: await connector.canVerifyCredentialRequest(request),
};
}),
);
return results.filter(r => r.available).map(r => r.connector);
}
}
import { Injectable } from '@nestjs/common';
import { ConnectorService } from '../connector-service.interface';
import { Organization } from 'src/organizations/organization.entity';
import { CredentialIssueRequest } from 'src/requests/credential-issue-request.entity';
import { CredentialVerifyRequest } from 'src/requests/credential-verify-request.entity';
@Injectable()
export class IrmaService implements ConnectorService {
......@@ -10,4 +12,25 @@ export class IrmaService implements ConnectorService {
// We don't need to do anything for IRMA.
return;
}
canIssueCredentialRequest(request: CredentialIssueRequest) {
// We cannot issue IRMA credentials right now.
return false;
}
canVerifyCredentialRequest(request: CredentialVerifyRequest) {
if (!request.type) {
throw Error('Could not check type');
}
return !!request.type.irmaType;
}
async handleIssueCredentialRequest(request: CredentialIssueRequest) {
return null;
}
async handleVerifyCredentialRequest(request: CredentialIssueRequest) {
return null;
}
}
......@@ -2,7 +2,7 @@ import { Controller, Get, Post, Body } from '@nestjs/common';
import { JolocomService } from './jolocom.service';
import { CreateJolocomTypeDto } from './create-jolocom-type.dto';
@Controller('connectors/jolocom')
@Controller('api/connectors/jolocom')
export class JolocomController {
constructor(private jolocomService: JolocomService) {}
......
......@@ -8,6 +8,8 @@ import { ConnectorService } from '../connector-service.interface';
import { Organization } from '../../organizations/organization.entity';
import { JolocomWallet } from './jolocom-wallet.entity';
import { JolocomCredentialType } from './jolocom-credential-type.entity';
import { CredentialIssueRequest } from 'src/requests/credential-issue-request.entity';
import { CredentialVerifyRequest } from 'src/requests/credential-verify-request.entity';
@Injectable()
export class JolocomService implements ConnectorService {
......@@ -34,8 +36,34 @@ export class JolocomService implements ConnectorService {
await this.registerWallet(wallet);
}
canIssueCredentialRequest(request: CredentialIssueRequest) {
if (!request.type) {
throw Error('Could not check type');
}
return !!request.type.jolocomType;
}
canVerifyCredentialRequest(request: CredentialVerifyRequest) {
if (!request.type) {
throw Error('Could not check type');
}
return !!request.type.jolocomType;
}
async handleIssueCredentialRequest(request: CredentialIssueRequest) {
return null;
}
async handleVerifyCredentialRequest(request: CredentialIssueRequest) {
return null;
}
/* JolocomService specific */
/* JolocomCredentialType methods */
async findAllTypes() {
return this.typesRepository.find();
}
......
import { Controller, Get, Param, Query } from '@nestjs/common';
import { Controller, Get, Query, Param } from '@nestjs/common';
import {
DecodeIssueRequestPipe,
GetIssueRequestPipe,
} from '../requests/requests.pipe';
import { CredentialIssueRequest } from '../requests/credential-issue-request.entity';
import { ConnectorsService } from '../connectors/connectors.service';
import { GetConnectorPipe } from '../connectors/get-connector.pipe';
import { ConnectorService } from '../connectors/connector-service.interface';
import { GetIssueRequestPipe } from '../requests/get-request.pipe';
import { CredentialIssueRequest } from '../requests/credential-issue-request.entity';
@Controller('issue')
@Controller('api/issue')
export class IssueController {
@Get(':connector')
constructor(private connectorsService: ConnectorsService) {}
@Get()
async receiveCredentialIssueRequest(
@Query('token', DecodeIssueRequestPipe)
issueRequest: CredentialIssueRequest,
) {
return {
issueRequest,
availableConnectors: await this.connectorsService
.availableIssueConnectors(issueRequest)
.then(cs => cs.map(c => c.type)),
};
}
@Get(':connector')
async handleCredentialVerifyRequest(
@Param('connector', GetConnectorPipe) connectorService: ConnectorService,
@Query('token', GetIssueRequestPipe)
@Query('issueRequestId', GetIssueRequestPipe)
issueRequest: CredentialIssueRequest,
) {
return issueRequest;
return { issueRequest, connectorService };
}
}
......@@ -2,7 +2,7 @@ import { Controller, Get, Body, Post } from '@nestjs/common';
import { OrganizationsService } from './organizations.service';
import { CreateOrganizationDto } from './create-organization.dto';
@Controller('organizations')
@Controller('api/organizations')
export class OrganizationsController {
constructor(private organizationsService: OrganizationsService) {}
......
import { Test, TestingModule } from '@nestjs/testing';
import { RequestsGateway } from './requests.gateway';
describe('RequestsGateway', () => {
let gateway: RequestsGateway;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [RequestsGateway],
}).compile();
gateway = module.get<RequestsGateway>(RequestsGateway);
});
it('should be defined', () => {
expect(gateway).toBeDefined();
});
});
import {
SubscribeMessage,
WebSocketGateway,
MessageBody,
} from '@nestjs/websockets';
import { CredentialVerifyRequest } from './credential-verify-request.entity';
import { DecodeVerifyRequestPipe } from './get-request.pipe';
@WebSocketGateway()
export class RequestsGateway {
@SubscribeMessage('message')
handleMessage(@MessageBody() message: string): string {
return `${message.toLocaleUpperCase()}!`;
}
@SubscribeMessage('verify-request')
handleVerifyRequest(
@MessageBody(DecodeVerifyRequestPipe)
verifyRequest: CredentialVerifyRequest,
): CredentialVerifyRequest {
return verifyRequest;
}
}
......@@ -7,7 +7,13 @@ import { TypesModule } from 'src/types/types.module';
import { CredentialIssueRequest } from './credential-issue-request.entity';
import { CredentialVerifyRequest } from './credential-verify-request.entity';
import { RequestsService } from './requests.service';
import { GetIssueRequestPipe, GetVerifyRequestPipe } from './get-request.pipe';
import {
DecodeIssueRequestPipe,
DecodeVerifyRequestPipe,
GetIssueRequestPipe,
GetVerifyRequestPipe,
} from './requests.pipe';
import { RequestsGateway } from './requests.gateway';
@Module({
imports: [
......@@ -15,7 +21,21 @@ import { GetIssueRequestPipe, GetVerifyRequestPipe } from './get-request.pipe';
OrganizationsModule,
TypesModule,
],
providers: [RequestsService, GetIssueRequestPipe, GetVerifyRequestPipe],
exports: [RequestsService, GetIssueRequestPipe, GetVerifyRequestPipe],
providers: [
RequestsService,
DecodeIssueRequestPipe,
GetIssueRequestPipe,
DecodeVerifyRequestPipe,
GetVerifyRequestPipe,
RequestsGateway,
],
exports: [
RequestsService,
DecodeIssueRequestPipe,
GetIssueRequestPipe,
DecodeVerifyRequestPipe,
GetVerifyRequestPipe,
RequestsGateway,
],
})
export class RequestsModule {}
......@@ -7,7 +7,7 @@ import {
import { RequestsService } from './requests.service';
@Injectable()
export class GetVerifyRequestPipe implements PipeTransform {
export class DecodeVerifyRequestPipe implements PipeTransform {
constructor(
@Inject(RequestsService) private requestsService: RequestsService,
) {}
......@@ -17,7 +17,19 @@ export class GetVerifyRequestPipe implements PipeTransform {
}
}
export class GetIssueRequestPipe implements PipeTransform {
@Injectable()
export class GetVerifyRequestPipe implements PipeTransform {
constructor(
@Inject(RequestsService) private requestsService: RequestsService,
) {}
transform(requestId: string, metatdata: ArgumentMetadata) {
return this.requestsService.findVerifyRequestByRequestId(requestId);
}
}
@Injectable()
export class DecodeIssueRequestPipe implements PipeTransform {
constructor(
@Inject(RequestsService) private requestsService: RequestsService,
) {}
......@@ -26,3 +38,14 @@ export class GetIssueRequestPipe implements PipeTransform {
return this.requestsService.decodeIssueRequestToken(jwtToken);
}
}
@Injectable()
export class GetIssueRequestPipe implements PipeTransform {
constructor(
@Inject(RequestsService) private requestsService: RequestsService,
) {}
transform(requestId: string, metatdata: ArgumentMetadata) {
return this.requestsService.findIssueRequestByRequestId(requestId);
}
}
......@@ -87,10 +87,15 @@ export class RequestsService {
CredentialVerifyRequestData
>(jwt);
const type = await this.typesRepository.findOneOrFail({
organization: requestor,
type: request.type,
});
const type = await this.typesRepository.findOneOrFail(
{
organization: requestor,
type: request.type,
},
{
relations: ['jolocomType'],
},
);
const verifyRequest = new CredentialVerifyRequest();
......@@ -106,10 +111,15 @@ export class RequestsService {
CredentialIssueRequestData
>(jwt);
const type = await this.typesRepository.findOneOrFail({
organization: requestor,
type: request.type,
});
const type = await this.typesRepository.findOneOrFail(
{
organization: requestor,
type: request.type,
},
{
relations: ['jolocomType'],
},
);
const issueRequest = new CredentialIssueRequest();
......
......@@ -2,7 +2,7 @@ import { Controller, Post, Body, Get } from '@nestjs/common';
import { TypesService } from './types.service';
import { CreateTypeDto } from './create-type.dto';
@Controller('types')
@Controller('api/types')
export class TypesController {
constructor(private typesService: TypesService) {}
@Get()
......
......@@ -2,7 +2,7 @@ import { Controller, Post, Body, Param } from '@nestjs/common';
import { OrganizationsService } from 'src/organizations/organizations.service';
import { UtilsService } from './utils.service';
@Controller('utils')
@Controller('api/utils')
export class UtilsController {
constructor(
private organizationsService: OrganizationsService,
......
import { Controller, Get, Param, Query } from '@nestjs/common';
import { GetConnectorPipe } from '../connectors/get-connector.pipe';
import { ConnectorService } from 'src/connectors/connector-service.interface';
import { GetVerifyRequestPipe } from 'src/requests/get-request.pipe';
import { CredentialVerifyRequest } from 'src/requests/credential-verify-request.entity';
import { ConnectorService } from '../connectors/connector-service.interface';
import { ConnectorsService } from '../connectors/connectors.service';
import {
DecodeVerifyRequestPipe,
GetVerifyRequestPipe,
} from '../requests/requests.pipe';
import { CredentialVerifyRequest } from '../requests/credential-verify-request.entity';
import { RequestsGateway } from '../requests/requests.gateway';
@Controller('verify')
@Controller('api/verify')
export class VerifyController {
@Get(':connector')
constructor(
private gateway: RequestsGateway,
private connectorsService: ConnectorsService,
) {
console.log(this.gateway);
}
@Get()
async receiveCredentialVerifyRequest(
@Query('token', DecodeVerifyRequestPipe)
verifyRequest: CredentialVerifyRequest,
) {
return {
verifyRequest,
availableConnectors: await this.connectorsService
.availableVerifyConnectors(verifyRequest)
.then(cs => cs.map(c => c.type)),
};
}
@Get(':connector')
async handleCredentialVerifyRequest(
@Param('connector', GetConnectorPipe) connectorService: ConnectorService,
@Query('token', GetVerifyRequestPipe)
@Query('verifyRequestId', GetVerifyRequestPipe)
verifyRequest: CredentialVerifyRequest,
) {
return verifyRequest;
return { verifyRequest, connectorService };
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment