Commit 07d163cf authored by Hidde-Jan Jongsma's avatar Hidde-Jan Jongsma

Add request entities

parent 8de542ab
......@@ -1538,6 +1538,14 @@
"integrity": "sha1-7ihweulOEdK4J7y+UnC86n8+ce4=",
"dev": true
},
"@types/jsonwebtoken": {
"version": "8.5.0",
"resolved": "https://registry.npmjs.org/@types/jsonwebtoken/-/jsonwebtoken-8.5.0.tgz",
"integrity": "sha512-9bVao7LvyorRGZCw0VmH/dr7Og+NdjYSsKAxB43OQoComFbBgsEpoR9JW6+qSq/ogwVBg8GI2MfAlk4SYI4OLg==",
"requires": {
"@types/node": "*"
}
},
"@types/mime": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.1.tgz",
......@@ -1647,6 +1655,11 @@
}
}
},
"@types/uuid": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-7.0.3.tgz",
"integrity": "sha512-PUdqTZVrNYTNcIhLHkiaYzoOIaUi5LFg/XLerAdgvwQrUCx+oSbtoBze1AMyvYbcwzUSNC+Isl58SM4Sm/6COw=="
},
"@types/validator": {
"version": "13.0.0",
"resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.0.0.tgz",
......
......@@ -26,7 +26,10 @@ export class JolocomWallet extends BaseEntity {
@Column({ update: false })
password: string;
@OneToOne(() => Organization)
@OneToOne(
() => Organization,
organization => organization.jolocomWallet,
)
@JoinColumn()
organization: Organization;
......
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
import {
Entity,
PrimaryGeneratedColumn,
Column,
OneToOne,
OneToMany,
} from 'typeorm';
import { randomBytes } from 'crypto';
import { JolocomWallet } from '../connectors/jolocom/jolocom-wallet.entity';
import { CredentialVerifyRequest } from 'src/requests/credential-verify-request.entity';
import { CredentialIssueRequest } from 'src/requests/credential-issue-request.entity';
const JWT_SECRET_BITS = 32;
......@@ -14,6 +23,24 @@ export class Organization {
@Column()
sharedSecret: string;
@OneToOne(
() => JolocomWallet,
wallet => wallet.organization,
)
jolocomWallet: JolocomWallet;
@OneToMany(
() => CredentialVerifyRequest,
request => request.requestor,
)
verifyRequests: CredentialVerifyRequest[];
@OneToMany(
() => CredentialIssueRequest,
request => request.requestor,
)
issueRequests: CredentialIssueRequest[];
static randomSecret(): string {
return randomBytes(JWT_SECRET_BITS).toString('hex');
}
......
import {
Entity,
PrimaryGeneratedColumn,
Column,
Generated,
ManyToOne,
} from 'typeorm';
import { CredentialRequest } from './credential-request.interface';
import { Organization } from '../organizations/organization.entity';
interface CredentialData {
[key: string]: string | number | boolean | null;
}
export interface CredentialIssueRequestData {
iss: string;
type: string;
data: CredentialData;
callbackUrl: string; // the REST api of the verifier where to deliver the credential data
}
@Entity()
export class CredentialIssueRequest implements CredentialRequest {
@PrimaryGeneratedColumn()
id: number;
@Column()
callbackUrl: string;
@Column()
type: string;
@Column()
@Generated('uuid')
uuid: string;
@Column('simple-json')
data: CredentialData;
@ManyToOne(
() => Organization,
organization => organization.issueRequests,
)
requestor: Organization;
get requestId() {
return `credential-issue-request:${this.uuid}`;
}
get issuer() {
return this.requestor;
}
set issuer(issuer: Organization) {
this.requestor = issuer;
}
}
import uuidv4 from 'uuid/v4';
interface CredentialData {
[key: string]: string | number | boolean | null;
}
export interface CredentialIssueRequestData {
iss: string;
type: string;
data: CredentialData;
callbackUrl: string; // the REST api of the verifier where to deliver the credential data
}
export class CredentialIssueRequest {
requestId: string;
constructor(
protected issuerId: string,
public credentialType: string,
public credentialData: CredentialData,
public callbackUrl: string,
) {
this.requestId = `credential-issue-request-${uuidv4()}`;
}
}
import { Organization } from '../organizations/organization.entity';
export interface CredentialRequest {
requestId: string;
type: string;
callbackUrl: string;
requestor: Organization;
}
import {
Entity,
PrimaryGeneratedColumn,
Column,
Generated,
ManyToOne,
} from 'typeorm';
import { CredentialRequest } from './credential-request.interface';
import { Organization } from '../organizations/organization.entity';
export interface CredentialVerifyRequestData {
iss: string;
type: string;
callbackUrl: string; // the REST api of the verifier where to deliver the credential data
}
@Entity()
export class CredentialVerifyRequest implements CredentialRequest {
@PrimaryGeneratedColumn()
id: number;
@Column()
callbackUrl: string;
@Column()
type: string;
@Column()
@Generated('uuid')
uuid: string;
@ManyToOne(
() => Organization,
organization => organization.verifyRequests,
)
requestor: Organization;
get requestId() {
return `credential-verify-request:${this.uuid}`;
}
get verifier() {
return this.requestor;
}
set verifier(verifier: Organization) {
this.requestor = verifier;
}
}
import uuidv4 from 'uuid/v4';
export interface CredentialVerifyRequestData {
iss: string;
type: string;
callbackUrl: string; // the REST api of the verifier where to deliver the credential data
}
export class CredentialVerifyRequest {
requestId: string;
constructor(
protected verifierId: string,
public credentialType: string,
public callbackUrl: string,
) {
this.requestId = `credential-verify-request-${uuidv4()}`;
}
}
......@@ -6,11 +6,11 @@ import { Organization } from 'src/organizations/organization.entity';
import {
CredentialVerifyRequest,
CredentialVerifyRequestData,
} from './credential-verify-request';
} from './credential-verify-request.entity';
import {
CredentialIssueRequest,
CredentialIssueRequestData,
} from './credential-issue-request';
} from './credential-issue-request.entity';
export class InvalidRequestJWT extends Error {}
......@@ -25,14 +25,15 @@ export class RequestsService {
CredentialVerifyRequestData
>(jwt);
return {
verifyRequest: new CredentialVerifyRequest(
request.iss,
request.type,
request.callbackUrl,
),
verifier: requestor,
};
const verifyRequest = new CredentialVerifyRequest();
verifyRequest.requestor = requestor;
verifyRequest.type = request.type;
verifyRequest.callbackUrl = request.callbackUrl;
// TODO: Save to db
return verifyRequest;
}
async decodeIssueRequestToken(jwt: string) {
......@@ -40,18 +41,19 @@ export class RequestsService {
CredentialIssueRequestData
>(jwt);
return {
issueRequest: new CredentialIssueRequest(
request.iss,
request.type,
request.data,
request.callbackUrl,
),
issuer: requestor,
};
const issueRequest = new CredentialIssueRequest();
issueRequest.requestor = requestor;
issueRequest.type = request.type;
issueRequest.callbackUrl = request.callbackUrl;
issueRequest.data = request.data;
// TODO: Save to db
return issueRequest;
}
async decodeAndVerifyJwt<T>(
async decodeAndVerifyJwt<T = unknown>(
jwt: string,
): Promise<{ request: T; requestor: Organization }> {
try {
......@@ -80,7 +82,7 @@ export class RequestsService {
throw new Error(`String returned '${request}'. Expecting json object`);
}
return { request, requestor };
return { request: (request as unknown) as T, requestor };
} catch (e) {
throw new InvalidRequestJWT('Could not decode request JWT');
}
......
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