Skip to content

Commit

Permalink
🎨 Move KAS methods in nano code
Browse files Browse the repository at this point in the history
Small refactor to consolidate KAS methods
  • Loading branch information
dmihalcik-virtru committed Jul 31, 2024
1 parent 5e234ee commit 41cb5f4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
11 changes: 11 additions & 0 deletions lib/src/kas.ts → lib/src/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,14 @@ export async function fetchWrappedKey(

return response.json();
}

export async function fetchECKasPubKey(kasEndpoint: string): Promise<string> {
const kasPubKeyResponse = await fetch(`${kasEndpoint}/kas_public_key?algorithm=ec:secp256r1`);
if (!kasPubKeyResponse.ok) {
throw new Error(
`Unable to validate KAS [${kasEndpoint}]. Received [${kasPubKeyResponse.status}:${kasPubKeyResponse.statusText}]`
);
}
return kasPubKeyResponse.json();
}

6 changes: 4 additions & 2 deletions lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from './nanotdf/index.js';
import { keyAgreement } from './nanotdf-crypto/index.js';
import { TypedArray, createAttribute, Policy } from './tdf/index.js';
import { AuthProvider } from './auth/auth.js';
import { fetchECKasPubKey } from './access.js';
import { ClientConfig } from './nanotdf/Client.js';
import { pemToCryptoPublicKey } from './utils.js';

Expand Down Expand Up @@ -132,7 +134,7 @@ export class NanoTDFClient extends Client {
delete this.iv;

if (!this.kasPubKey) {
this.kasPubKey = await fetchKasPubKey(this.kasUrl);
this.kasPubKey = await fetchECKasPubKey(this.kasUrl);
}

// Create a policy for the tdf
Expand Down Expand Up @@ -259,7 +261,7 @@ export class NanoTDFDatasetClient extends Client {
const ephemeralKeyPair = await this.ephemeralKeyPair;

if (!this.kasPubKey) {
this.kasPubKey = await fetchKasPubKey(this.kasUrl);
this.kasPubKey = await fetchECKasPubKey(this.kasUrl);
}

// Create a policy for the tdf
Expand Down
2 changes: 1 addition & 1 deletion lib/src/nanotdf/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as base64 from '../encodings/base64.js';
import { generateKeyPair, keyAgreement } from '../nanotdf-crypto/index.js';
import getHkdfSalt from './helpers/getHkdfSalt.js';
import DefaultParams from './models/DefaultParams.js';
import { fetchWrappedKey } from '../kas.js';
import { fetchWrappedKey } from '../access.js';
import { AuthProvider, isAuthProvider, reqSignature } from '../auth/providers.js';
import {
cryptoPublicToPem,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/tdf/AttributeObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export async function createAttribute(
kasUrl: string
): Promise<AttributeObject> {
return {
attribute: attribute,
attribute,
isDefault: false,
displayName: '',
pubKey: await cryptoPublicToPem(pubKey),
kasUrl: kasUrl,
kasUrl,
schemaVersion: '1.1.0',
};
}

0 comments on commit 41cb5f4

Please sign in to comment.