Skip to content

Commit 3d45ecf

Browse files
feat(sdk): Export AttributeObject and others (#487)
- Remove duplicate AttributeObject interface, replacing with type - Removes unnecessary constructor function - Exports the type - Refactor of similar duplication with policy: - NanoTDF code `Policy` becomes `PolicyBuilder` - The Nano code's `PolicyObject` is removed in favor of the `tdf3.Policy` type - adds missing `mimeType` field to `Payload` type
1 parent 6ba9044 commit 3d45ecf

File tree

13 files changed

+71
-52
lines changed

13 files changed

+71
-52
lines changed

lib/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { attributeFQNsAsValues } from './policy/api.js';
44
export { version, clientType, tdfSpecVersion } from './version.js';
55
export * from './opentdf.js';
66
export * from './seekable.js';
7+
export * from '../tdf3/src/models/index.js';

lib/src/nanoclients.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import {
99
DefaultParams,
1010
} from './nanotdf/index.js';
1111
import { keyAgreement } from './nanotdf-crypto/index.js';
12-
import { Policy } from './tdf/Policy.js';
13-
import { createAttribute } from './tdf/AttributeObject.js';
12+
import { PolicyBuilder } from './tdf/Policy.js';
1413
import { fetchECKasPubKey } from './access.js';
15-
import { ClientConfig } from './nanotdf/Client.js';
14+
import { type ClientConfig } from './nanotdf/Client.js';
1615
import { ConfigurationError } from './errors.js';
16+
import { type AttributeObject } from '../tdf3/src/models/attribute.js';
1717

1818
// Define the EncryptOptions type
1919
export type EncryptOptions = {
@@ -112,11 +112,15 @@ export class NanoTDFClient extends Client {
112112
}
113113

114114
// Create a policy for the tdf
115-
const policy = new Policy();
115+
const policy = new PolicyBuilder();
116116

117117
// Add data attributes.
118118
for (const dataAttribute of this.dataAttributes) {
119-
const attribute = await createAttribute(dataAttribute, this.kasPubKey, this.kasUrl);
119+
const attribute: AttributeObject = {
120+
attribute: dataAttribute,
121+
pubKey: this.kasPubKey.publicKey,
122+
kasUrl: this.kasUrl,
123+
};
120124
policy.addAttribute(attribute);
121125
}
122126

@@ -243,11 +247,15 @@ export class NanoTDFDatasetClient extends Client {
243247
}
244248

245249
// Create a policy for the tdf
246-
const policy = new Policy();
250+
const policy = new PolicyBuilder();
247251

248252
// Add data attributes.
249253
for (const dataAttribute of this.dataAttributes) {
250-
const attribute = await createAttribute(dataAttribute, this.kasPubKey, this.kasUrl);
254+
const attribute = {
255+
attribute: dataAttribute,
256+
kasPubKey: this.kasPubKey.publicKey,
257+
kasUrl: this.kasUrl,
258+
};
251259
policy.addAttribute(attribute);
252260
}
253261

lib/src/opentdf.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import {
2929
type IntegrityAlgorithm,
3030
} from '../tdf3/src/tdf.js';
3131
import { base64 } from './encodings/index.js';
32-
import { PolicyObject } from './tdf/PolicyObject.js';
3332
import PolicyType from './nanotdf/enum/PolicyTypeEnum.js';
33+
import { Policy } from '../tdf3/src/models/policy.js';
3434

3535
export {
3636
type Assertion,
@@ -525,8 +525,8 @@ class NanoTDFReader {
525525
throw new Error('unsupported policy type');
526526
}
527527
const policyString = new TextDecoder().decode(nanotdf.header.policy.content);
528-
const policy = JSON.parse(policyString) as PolicyObject;
529-
return policy.body.dataAttributes.map((a) => a.attribute);
528+
const policy = JSON.parse(policyString) as Policy;
529+
return policy?.body?.dataAttributes.map((a) => a.attribute) || [];
530530
}
531531
}
532532

@@ -593,8 +593,8 @@ class ZTDFReader {
593593
async attributes(): Promise<string[]> {
594594
const manifest = await this.manifest();
595595
const policyJSON = base64.decode(manifest.encryptionInformation.policy);
596-
const policy = JSON.parse(policyJSON) as PolicyObject;
597-
return policy.body.dataAttributes.map((a) => a.attribute);
596+
const policy = JSON.parse(policyJSON) as Policy;
597+
return policy?.body?.dataAttributes.map((a) => a.attribute) || [];
598598
}
599599
}
600600

lib/src/tdf/Policy.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { type AttributeObject } from './AttributeObject.js';
21
import { v4 as uuid } from 'uuid';
32

4-
export class Policy {
3+
import { type AttributeObject } from '../../tdf3/src/models/attribute.js';
4+
import { type Policy } from '../../tdf3/src/models/policy.js';
5+
6+
export class PolicyBuilder {
57
static CURRENT_VERSION = '1.1.0';
68

79
private uuidStr = uuid();
@@ -33,18 +35,22 @@ export class Policy {
3335
this.dataAttributesList.push(attribute);
3436
}
3537

38+
toPolicy(): Policy {
39+
return {
40+
uuid: this.uuidStr,
41+
body: {
42+
dataAttributes: this.dataAttributesList,
43+
dissem: this.dissemList,
44+
},
45+
};
46+
}
47+
3648
/**
3749
* Returns the JSON string of Policy object
3850
*
3951
* @return {string} [The constructed Policy object as JSON string]
4052
*/
4153
toJSON(): string {
42-
return JSON.stringify({
43-
uuid: this.uuidStr,
44-
body: {
45-
dataAttributes: this.dataAttributesList,
46-
dissem: this.dissemList,
47-
},
48-
});
54+
return JSON.stringify(this.toPolicy());
4955
}
5056
}

lib/src/tdf/PolicyObject.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

lib/tdf3/src/models/attribute-set.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
import { decodeJwt } from 'jose';
22

3-
export type AttributeObject = {
4-
attribute: string;
5-
kasUrl?: string;
6-
kid?: string;
7-
pubKey?: string;
8-
displayName?: string;
9-
isDefault?: boolean;
10-
jwt?: string;
11-
};
3+
import { type AttributeObject } from './attribute.js';
124

135
export class AttributeSet {
146
attributes: AttributeObject[];

lib/tdf3/src/models/attribute.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Information about a data or entity attribute, its meaning and interpretation.
3+
* While usually we just refer to an attribute by its URL,
4+
* we often need to store additional information about it,
5+
* for display or analysis.
6+
*/
7+
export type AttributeObject = {
8+
// The fully qualified name of the attribute, generally a URL
9+
attribute: string;
10+
// Optional descriptive name of the attribute
11+
displayName?: string;
12+
// Indicates a default attribute, usually for all policies associated with a KAS
13+
isDefault?: boolean;
14+
15+
// Optional: A cryptographically bound version of the attribute. Deprecated: use a JWS with this as the payload.
16+
jwt?: string;
17+
18+
// A KAS that is associated with the attribute.
19+
kasUrl?: string;
20+
21+
// The preferred public key for the attribute
22+
kid?: string;
23+
24+
// The public key value for the attribute
25+
pubKey?: string;
26+
};

lib/tdf3/src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './attribute.js';
12
export * from './attribute-set.js';
23
export * from './encryption-information.js';
34
export * from './key-access.js';

lib/tdf3/src/models/payload.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export type Payload = {
33
url: string; // "0.payload"
44
protocol: string; // "zip"
55
isEncrypted: boolean; // true
6+
mimeType?: string; // e.g. "text/plain"
67
};

lib/tdf3/src/models/policy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ConfigurationError } from '../../../src/errors.js';
2-
import { AttributeObject } from './attribute-set.js';
2+
import { type AttributeObject } from './attribute.js';
33

44
export const CURRENT_VERSION = '1.1.0';
55

0 commit comments

Comments
 (0)