Commit e013aaec authored by Hidde-Jan Jongsma's avatar Hidde-Jan Jongsma

Add connector registration in organization service

parent 7e829cbc
import { Module } from '@nestjs/common';
import { JolocomModule } from './jolocom/jolocom.module';
import { IrmaModule } from './irma/irma.module';
import { ConnectorsService } from './connectors.service';
......
......@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { JolocomService } from './jolocom/jolocom.service';
import { IrmaService } from './irma/irma.service';
import { ConnectorService } from './connector-service.interface';
import { Organization } from '../organizations/organization.entity';
@Injectable()
export class ConnectorsService {
......@@ -18,4 +19,12 @@ export class ConnectorsService {
getConnector(type: string) {
return this.connectors.find(connector => connector.type === type);
}
async registerOrganization(organization: Organization) {
await Promise.all(
this.connectors.map(
async connector => await connector.registerOrganization(organization),
),
);
}
}
......@@ -7,6 +7,7 @@ export class IrmaService implements ConnectorService {
type = 'irma';
async registerOrganization(organization: Organization) {
// We don't need to do anything for IRMA.
return;
}
}
import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
ManyToOne,
Column,
} from 'typeorm';
import { JolocomWallet } from './jolocom-wallet.entity';
import { BaseMetadata } from 'cred-types-jolocom-core/js/types';
@Entity()
export class JolocomCredentialType extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne(
() => JolocomWallet,
wallet => wallet.credentialOffers,
)
wallet: JolocomWallet;
@Column({ unique: true })
type: string;
@Column()
name: string;
@Column('simple-json')
context: BaseMetadata['context'];
@Column('simple-json')
claimInterface: BaseMetadata['claimInterface'];
}
import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
Column,
OneToOne,
JoinColumn,
OneToMany,
} from 'typeorm';
import { randomBytes } from 'crypto';
import { Organization } from '../../organizations/organization.entity';
import { JolocomCredentialType } from './jolocom-credential-type.entity';
const JOLOCOM_WALLET_SEED_BYTES = 32;
const JOLOCOM_WALLET_PASSWORD_BYTES = 16;
@Entity()
export class JolocomWallet extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ update: false })
encryptedSeedHex: string;
@Column({ update: false })
password: string;
@OneToOne(() => Organization)
@JoinColumn()
organization: Organization;
@OneToMany(
() => JolocomCredentialType,
credentialOffer => credentialOffer.wallet,
)
credentialOffers: JolocomCredentialType[];
static randomPassword() {
return randomBytes(JOLOCOM_WALLET_PASSWORD_BYTES).toString('hex');
}
static randomSeed() {
return randomBytes(JOLOCOM_WALLET_SEED_BYTES);
}
get encryptedSeed(): Buffer {
return Buffer.from(this.encryptedSeedHex, 'hex');
}
set encryptedSeed(seed: Buffer) {
this.encryptedSeedHex = seed.toString('hex');
}
}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JolocomService } from './jolocom.service';
import { JolocomWallet } from './jolocom-wallet.entity';
import { JolocomCredentialType } from './jolocom-credential-type.entity';
@Module({
imports: [TypeOrmModule.forFeature([JolocomWallet, JolocomCredentialType])],
providers: [JolocomService],
exports: [JolocomService],
})
......
This diff is collapsed.
......@@ -4,9 +4,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { OrganizationsController } from './organizations.controller';
import { OrganizationsService } from './organizations.service';
import { Organization } from './organization.entity';
import { ConnectorsModule } from 'src/connectors/connectors.module';
@Module({
imports: [TypeOrmModule.forFeature([Organization])],
imports: [TypeOrmModule.forFeature([Organization]), ConnectorsModule],
controllers: [OrganizationsController],
providers: [OrganizationsService],
})
......
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Organization } from './organization.entity';
import { ConnectorsService } from 'src/connectors/connectors.service';
@Injectable()
export class OrganizationsService {
logger: Logger;
constructor(
@InjectRepository(Organization)
private organizationsRepository: Repository<Organization>,
) {}
private connectorsService: ConnectorsService,
) {
this.logger = new Logger(Organization.name);
}
findAll() {
async findAll() {
return this.organizationsRepository.find();
}
createFromName(name: string) {
async findByIdentifier(uuid: string) {
const results = await this.organizationsRepository.find({ take: 1 });
return results[0];
}
async createFromName(name: string) {
this.logger.log(`Creating organization with name ${name}`);
const organization = new Organization();
organization.name = name;
organization.sharedSecret = Organization.randomSecret();
return this.organizationsRepository.save(organization);
await this.organizationsRepository.save(organization);
await this.connectorsService.registerOrganization(organization);
this.logger.log(`Created organization (id: ${organization.id})`);
return organization;
}
}
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