-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added tenant aware credential store
- Loading branch information
Showing
20 changed files
with
671 additions
and
126 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
60 changes: 60 additions & 0 deletions
60
packages/data-store/src/__tests__/uniformCredential.entities.test.ts
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,60 @@ | ||
import { DataSource } from 'typeorm' | ||
import { DataStoreUniformCredentialEntities, uniformCredentialEntityFromAddArgs } from '../index' | ||
import { DataStoreUniformCredentialMigrations } from '../migrations' | ||
import { UniformCredentialEntity } from '../entities/uniformCredential/UniformCredentialEntity' | ||
import { CredentialCorrelationType, CredentialDocumentFormat, CredentialTypeEnum } from '../types/uniformCredential/uniformCredential' | ||
import { computeEntryHash } from '@veramo/utils' | ||
import { AddUniformCredentialArgs } from '../../dist/types/credential/IAbstractCredentialStore' | ||
|
||
describe('Database entities tests', (): void => { | ||
let dbConnection: DataSource | ||
|
||
beforeEach(async (): Promise<void> => { | ||
dbConnection = await new DataSource({ | ||
type: 'sqlite', | ||
database: ':memory:', | ||
//logging: 'all', | ||
migrationsRun: false, | ||
migrations: DataStoreUniformCredentialMigrations, | ||
synchronize: false, | ||
entities: [...DataStoreUniformCredentialEntities], | ||
}).initialize() | ||
await dbConnection.runMigrations() | ||
expect(await dbConnection.showMigrations()).toBeFalsy() | ||
}) | ||
|
||
afterEach(async (): Promise<void> => { | ||
await (await dbConnection).destroy() | ||
}) | ||
|
||
it('should save uniform credential to database', async (): Promise<void> => { | ||
console.log(`going to save the credential...`) | ||
const rawCredential: string = | ||
'eyJraWQiOiJkaWQ6a2V5Ono2TWtyaGt5M3B1c20yNk1laUZhWFUzbjJuZWtyYW13RlVtZ0dyZUdHa0RWNnpRaiN6Nk1rcmhreTNwdXNtMjZNZWlGYVhVM24ybmVrcmFtd0ZVbWdHcmVHR2tEVjZ6UWoiLCJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSIsImh0dHBzOi8vc3BoZXJlb24tb3BlbnNvdXJjZS5naXRodWIuaW8vc3NpLW1vYmlsZS13YWxsZXQvY29udGV4dC9zcGhlcmVvbi13YWxsZXQtaWRlbnRpdHktdjEuanNvbmxkIl0sInR5cGUiOlsiVmVyaWZpYWJsZUNyZWRlbnRpYWwiLCJTcGhlcmVvbldhbGxldElkZW50aXR5Q3JlZGVudGlhbCJdLCJjcmVkZW50aWFsU3ViamVjdCI6eyJmaXJzdE5hbWUiOiJTIiwibGFzdE5hbWUiOiJLIiwiZW1haWxBZGRyZXNzIjoic0BrIn19LCJzdWIiOiJ1cm46dXVpZDpkZGE3YmYyNC04ZTdhLTQxZjgtYjY2Yy1hNDhkYmM1YjEwZmEiLCJqdGkiOiJ1cm46dXVpZDpkZGE3YmYyNC04ZTdhLTQxZjgtYjY2Yy1hNDhkYmM1YjEwZmEiLCJuYmYiOjE3MDg0NDA4MDgsImlzcyI6ImRpZDprZXk6ejZNa3Joa3kzcHVzbTI2TWVpRmFYVTNuMm5la3JhbXdGVW1nR3JlR0drRFY2elFqIn0.G0M84XVAxSmzGY-NQuB9NBofNrINSn6lvxW6761Vlq6ypvYgtc2xNdpiRmw8ryVNfnpzrr4Z5cB1RlrC05rJAw' | ||
const uniformCredential: AddUniformCredentialArgs = { | ||
credentialType: CredentialTypeEnum.VC, | ||
documentFormat: CredentialDocumentFormat.JWT, | ||
raw: rawCredential, | ||
issuerCorrelationType: CredentialCorrelationType.DID, | ||
subjectCorrelationType: CredentialCorrelationType.DID, | ||
issuerCorrelationId: 'did:key:z6Mkrhky3pusm26MeiFaXU3n2nekramwFUmgGreGGkDV6zQj', | ||
subjectCorrelationId: 'did:key:z6Mkrhky3pusm26MeiFaXU3n2nekramwFUmgGreGGkDV6zQj', | ||
tenantId: 'urn:uuid:nnag4b43-1e7a-98f8-a32c-a48dbc5b10mj', | ||
} | ||
|
||
const uniformCredentialEntity: UniformCredentialEntity = uniformCredentialEntityFromAddArgs(uniformCredential) | ||
const fromDb: UniformCredentialEntity = await dbConnection.getRepository(UniformCredentialEntity).save(uniformCredentialEntity) | ||
console.log(`saved uniformCredential: ${JSON.stringify(fromDb, null, 2)}`) | ||
expect(fromDb).toBeDefined() | ||
expect(fromDb?.id).not.toBeNull() | ||
expect(fromDb?.credentialType).toEqual(CredentialTypeEnum.VC) | ||
expect(fromDb?.documentFormat).toEqual(CredentialDocumentFormat.JWT) | ||
expect(fromDb?.raw).toEqual(rawCredential) | ||
expect(fromDb?.hash).toEqual(computeEntryHash(rawCredential)) | ||
expect(fromDb?.issuerCorrelationType).toEqual(CredentialCorrelationType.DID) | ||
expect(fromDb?.subjectCorrelationType).toEqual(CredentialCorrelationType.DID) | ||
expect(fromDb?.issuerCorrelationId).toEqual('did:key:z6Mkrhky3pusm26MeiFaXU3n2nekramwFUmgGreGGkDV6zQj') | ||
expect(fromDb?.subjectCorrelationId).toEqual('did:key:z6Mkrhky3pusm26MeiFaXU3n2nekramwFUmgGreGGkDV6zQj') | ||
expect(fromDb?.tenantId).toEqual('urn:uuid:nnag4b43-1e7a-98f8-a32c-a48dbc5b10mj') | ||
}) | ||
}) |
214 changes: 214 additions & 0 deletions
214
packages/data-store/src/__tests__/uniformCredential.store.test.ts
Large diffs are not rendered by default.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
packages/data-store/src/credential/AbstractCredentialStore.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
66 changes: 66 additions & 0 deletions
66
packages/data-store/src/migrations/generic/6-CreateUniformCredential.ts
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,66 @@ | ||
import { DatabaseType, MigrationInterface, QueryRunner } from 'typeorm' | ||
import Debug, { Debugger } from 'debug' | ||
import { CreateUniformCredential1708525189001 } from '../postgres/1708525189001-CreateUniformCredential' | ||
import { CreateUniformCredential1708525189002 } from '../sqlite/1708525189002-CreateUniformCredential' | ||
|
||
const debug: Debugger = Debug('sphereon:ssi-sdk:migrations') | ||
|
||
export class CreateUniformCredential1708525189000 implements MigrationInterface { | ||
name: string = 'CreateUniformCredential1708525189000' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
debug('migration: creating UniformCredential tables') | ||
const dbType: DatabaseType = queryRunner.connection.driver.options.type | ||
|
||
switch (dbType) { | ||
case 'postgres': { | ||
debug('using postgres migration file for UniformCredential') | ||
const mig: CreateUniformCredential1708525189001 = new CreateUniformCredential1708525189001() | ||
await mig.up(queryRunner) | ||
debug('Postgres Migration statements for UniformCredential executed') | ||
return | ||
} | ||
case 'sqlite': | ||
case 'expo': | ||
case 'react-native': { | ||
debug('using sqlite/react-native migration file for UniformCredential') | ||
const mig: CreateUniformCredential1708525189002 = new CreateUniformCredential1708525189002() | ||
await mig.up(queryRunner) | ||
debug('SQLite Migration statements for UniformCredential executed') | ||
return | ||
} | ||
default: | ||
return Promise.reject( | ||
`Migrations are currently only supported for sqlite, react-native, expo, and postgres for UniformCredential. Was ${dbType}. Please run your database without migrations and with 'migrationsRun: false' and 'synchronize: true' for now` | ||
) | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
debug('migration: reverting UniformCredential tables') | ||
const dbType: DatabaseType = queryRunner.connection.driver.options.type | ||
|
||
switch (dbType) { | ||
case 'postgres': { | ||
debug('using postgres migration file for UniformCredential') | ||
const mig: CreateUniformCredential1708525189001 = new CreateUniformCredential1708525189001() | ||
await mig.down(queryRunner) | ||
debug('Postgres Migration statements for UniformCredential reverted') | ||
return | ||
} | ||
case 'sqlite': | ||
case 'expo': | ||
case 'react-native': { | ||
debug('using sqlite/react-native migration file for UniformCredential') | ||
const mig: CreateUniformCredential1708525189002 = new CreateUniformCredential1708525189002() | ||
await mig.down(queryRunner) | ||
debug('SQLite Migration statements for UniformCredential reverted') | ||
return | ||
} | ||
default: | ||
return Promise.reject( | ||
`Migrations are currently only supported for sqlite, react-native, expo, and postgres for UniformCredential. Was ${dbType}. Please run your database without migrations and with 'migrationsRun: false' and 'synchronize: true' for now` | ||
) | ||
} | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
packages/data-store/src/migrations/postgres/1708525189001-CreateUniformCredential.ts
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,43 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class CreateUniformCredential1708525189001 implements MigrationInterface { | ||
name = 'CreateUniformCredential1708525189001' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TYPE "uniform_credential_credential_type_enum" AS ENUM('vc', 'vp')`) | ||
await queryRunner.query(`CREATE TYPE "uniform_credential_document_format_enum" AS ENUM('JSON-LD', 'JWT', 'SD-JWT', 'MDOC')`) | ||
await queryRunner.query(`CREATE TYPE "uniform_credential_correlation_type_enum" AS ENUM('did')`) | ||
await queryRunner.query(`CREATE TYPE "uniform_credential_state_type_enum" AS ENUM('revoked', 'verified', 'expired')`) | ||
|
||
await queryRunner.query(` | ||
CREATE TABLE "UniformCredential" ( | ||
"id" uuid NOT NULL DEFAULT uuid_generate_v4(), | ||
"credential_type" "uniform_credential_credential_type_enum" NOT NULL, | ||
"document_format" "uniform_credential_document_format_enum" NOT NULL, | ||
"raw" text NOT NULL, | ||
"uniform_document" text NOT NULL, | ||
"hash" text NOT NULL UNIQUE, | ||
"issuer_correlation_type" "uniform_credential_correlation_type_enum" NOT NULL, | ||
"subject_correlation_type" "uniform_credential_correlation_type_enum", | ||
"issuer_correlation_id" text NOT NULL, | ||
"subject_correlation_id" text, | ||
"last_verified_state" "uniform_credential_state_type_enum", | ||
"tenant_id" text, | ||
"created_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
"last_updated_at" TIMESTAMP NOT NULL DEFAULT now(), | ||
"expires_at" DATE, | ||
"verification_date" DATE, | ||
"revocation_date" DATE, | ||
PRIMARY KEY ("id") | ||
) | ||
`) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "UniformCredential"`) | ||
await queryRunner.query(`DROP TYPE "uniform_credential_state_type_enum"`) | ||
await queryRunner.query(`DROP TYPE "uniform_credential_correlation_type_enum"`) | ||
await queryRunner.query(`DROP TYPE "uniform_credential_document_format_enum"`) | ||
await queryRunner.query(`DROP TYPE "uniform_credential_credential_type_enum"`) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/data-store/src/migrations/sqlite/1708525189002-CreateUniformCredential.ts
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,33 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class CreateUniformCredential1708525189002 implements MigrationInterface { | ||
name = 'CreateUniformCredential1708525189002'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(` | ||
CREATE TABLE "UniformCredential" ( | ||
"id" varchar PRIMARY KEY NOT NULL, | ||
"credential_type" varchar CHECK( "credential_type" IN ('vc', 'vp') ) NOT NULL, | ||
"document_format" varchar CHECK( "document_format" IN ('JSON-LD', 'JWT', 'SD-JWT', 'MDOC') ) NOT NULL, | ||
"raw" text NOT NULL, | ||
"uniform_document" text NOT NULL, | ||
"hash" text NOT NULL UNIQUE, | ||
"issuer_correlation_type" varchar CHECK( "issuer_correlation_type" IN ('did') ) NOT NULL, | ||
"subject_correlation_type" varchar CHECK( "subject_correlation_type" IN ('did') ), | ||
"issuer_correlation_id" text NOT NULL, | ||
"subject_correlation_id" text, | ||
"last_verified_state" varchar CHECK( "last_verified_state" IN ('revoked', 'verified', 'expired') ), | ||
"tenant_id" text, | ||
"created_at" datetime NOT NULL DEFAULT (datetime('now')), | ||
"last_updated_at" datetime NOT NULL DEFAULT (datetime('now')), | ||
"expires_at" datetime, | ||
"verification_date" datetime, | ||
"revocation_date" datetime | ||
) | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "UniformCredential"`); | ||
} | ||
} |
Oops, something went wrong.