-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POST /import/encryption-key generating RSA keypair for imports
- Loading branch information
1 parent
977928b
commit 599249c
Showing
8 changed files
with
142 additions
and
6 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
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
12 changes: 12 additions & 0 deletions
12
apps/vault/src/vault/http/rest/dto/generate-encryption-key-response.dto.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,12 @@ | ||
import { RsaPublicKeyDto } from '@narval/policy-engine-shared' | ||
import { RsaPublicKey } from '@narval/signature' | ||
import { ApiProperty } from '@nestjs/swagger' | ||
|
||
export class GenerateEncryptionKeyResponseDto { | ||
constructor(publicKey: RsaPublicKey) { | ||
this.publicKey = publicKey | ||
} | ||
|
||
@ApiProperty() | ||
publicKey: RsaPublicKeyDto | ||
} |
50 changes: 50 additions & 0 deletions
50
apps/vault/src/vault/persistence/repository/import.repository.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,50 @@ | ||
import { RsaPrivateKey, rsaPrivateKeySchema } from '@narval/signature' | ||
import { Injectable } from '@nestjs/common' | ||
import { z } from 'zod' | ||
import { EncryptKeyValueService } from '../../../shared/module/key-value/core/service/encrypt-key-value.service' | ||
|
||
const importKeySchema = z.object({ | ||
jwk: rsaPrivateKeySchema, | ||
createdAt: z.number() // epoch in seconds | ||
}) | ||
export type ImportKey = z.infer<typeof importKeySchema> | ||
|
||
@Injectable() | ||
export class ImportRepository { | ||
private KEY_PREFIX = 'import:' | ||
|
||
constructor(private keyValueService: EncryptKeyValueService) {} | ||
|
||
getKey(clientId: string, id: string): string { | ||
return `${this.KEY_PREFIX}:${clientId}:${id}` | ||
} | ||
|
||
async findById(clientId: string, id: string): Promise<ImportKey | null> { | ||
const value = await this.keyValueService.get(this.getKey(clientId, id)) | ||
|
||
if (value) { | ||
return this.decode(value) | ||
} | ||
|
||
return null | ||
} | ||
|
||
async save(clientId: string, privateKey: RsaPrivateKey): Promise<ImportKey> { | ||
const createdAt = Date.now() / 1000 | ||
const importKey: ImportKey = { | ||
jwk: privateKey, | ||
createdAt | ||
} | ||
await this.keyValueService.set(this.getKey(clientId, privateKey.kid), this.encode(importKey)) | ||
|
||
return importKey | ||
} | ||
|
||
private encode(importKey: ImportKey): string { | ||
return JSON.stringify(importKey) | ||
} | ||
|
||
private decode(value: string): ImportKey { | ||
return importKeySchema.parse(JSON.parse(value)) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from './base-action-request.dto' | ||
export * from './base-action.dto' | ||
export * from './rsa-public-key.dto' | ||
export * from './sign-message-request-data-dto' | ||
export * from './sign-transaction-request-data.dto' | ||
export * from './signature.dto' |
46 changes: 46 additions & 0 deletions
46
packages/policy-engine-shared/src/lib/dto/rsa-public-key.dto.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,46 @@ | ||
import { ApiProperty } from '@nestjs/swagger' | ||
import { IsDefined, IsIn, IsOptional, IsString } from 'class-validator' | ||
|
||
export class RsaPublicKeyDto { | ||
@IsString() | ||
@IsDefined() | ||
@ApiProperty() | ||
kid: string | ||
|
||
@IsString() | ||
@IsDefined() | ||
@ApiProperty({ | ||
enum: ['RSA'], | ||
default: 'RSA' | ||
}) | ||
kty: 'RSA' | ||
|
||
@IsString() | ||
@IsDefined() | ||
@ApiProperty({ | ||
enum: ['RS256'], | ||
default: 'RS256' | ||
}) | ||
alg: 'RS256' | ||
|
||
@IsString() | ||
@IsDefined() | ||
@ApiProperty({ | ||
description: 'A base64Url-encoded value' | ||
}) | ||
n: string | ||
|
||
@IsString() | ||
@IsDefined() | ||
@ApiProperty({ | ||
description: 'A base64Url-encoded value' | ||
}) | ||
e: string | ||
|
||
@IsIn(['enc', 'sig']) | ||
@IsOptional() | ||
@ApiProperty({ | ||
enum: ['enc', 'sig'] | ||
}) | ||
use?: 'enc' | 'sig' | undefined | ||
} |
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