Skip to content

Commit

Permalink
allow prefixing
Browse files Browse the repository at this point in the history
  • Loading branch information
ipbyrne committed Jun 7, 2023
1 parent 2ad73cd commit 508f080
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 20 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,31 @@ This is a simple SDK that can be used to encrypt all of your data in MongoDB in
This package is intended to be layed over any implementation using MongoDB by utilizing 3 functions whenever you are interacting into the database.

### Encrypt Data (`encryptData`)
Parameters:
- Data: This is the data you want to encrypt.
- Private Key JWK: This is the key you want to use for encryption

This function is to be used whenever you are saving data into the database. You are expected to pass into this function the data to be saved and the private key you are going to use to encrypt the data to make the data queryable.

When saving to MongoDB you are expected to pass in an object but it can encrypt any data you pass in (string, number, array, etc).

Once encrypted you can save the data as is, or under any key value like `data` or something similiar. If you do save the data under a key value, you MUST pass that key to the `encryptQuery` function in order for the query to work.

### Encrypt Query (`encryptQuery`)
Parameters:
- Query: This is a MongoDB query written as if the data was not encrypted.
- Private Key JWK: This is the key you used to encrypt the data you saved to MongoDB.
- Prefix: This is the key the data is saved under. If you are saving the data to MongoDB as it comes out of `encryptData` you do not need to provide this parameter.

This function is to be used to format any query you want to use when querying the database. This function takes in the traditional MongoDB query, along with the private key used when saving the data you are trying to query.

As mentioned above, if you save your encrypted data under a specific key value (like `data`), you must pass this key to the `encryptQuery` function in order for the returned query to work.

### Decrypt Data (`decryptData`)
Parameters:
- Data: This is the encrypted data you are trying to decrypt.
- Private Key JWK: This is the key you used to encrypt the data you saved to MongoDB.

This function is used to decrypt the data returned from MongoDB. This function is expecting the data returned from MongoDB, the private key you will use to decrypt the data.

This will then return the decrypted data.
Expand Down
2 changes: 1 addition & 1 deletion coverage/badge-branches.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion coverage/badge-functions.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion coverage/badge-lines.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion coverage/badge-statements.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{"total": {"lines":{"total":139,"covered":135,"skipped":0,"pct":97.12},"statements":{"total":162,"covered":157,"skipped":0,"pct":96.91},"functions":{"total":33,"covered":32,"skipped":0,"pct":96.96},"branches":{"total":32,"covered":24,"skipped":0,"pct":75},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/index.ts": {"lines":{"total":18,"covered":18,"skipped":0,"pct":100},"functions":{"total":5,"covered":5,"skipped":0,"pct":100},"statements":{"total":21,"covered":21,"skipped":0,"pct":100},"branches":{"total":4,"covered":2,"skipped":0,"pct":50}}
{"total": {"lines":{"total":165,"covered":155,"skipped":0,"pct":93.93},"statements":{"total":190,"covered":177,"skipped":0,"pct":93.15},"functions":{"total":34,"covered":32,"skipped":0,"pct":94.11},"branches":{"total":45,"covered":38,"skipped":0,"pct":84.44},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/index.ts": {"lines":{"total":32,"covered":28,"skipped":0,"pct":87.5},"functions":{"total":6,"covered":4,"skipped":0,"pct":66.66},"statements":{"total":37,"covered":31,"skipped":0,"pct":83.78},"branches":{"total":6,"covered":4,"skipped":0,"pct":66.66}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/types.ts": {"lines":{"total":5,"covered":5,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":5,"covered":5,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/cipher/cipher.ts": {"lines":{"total":27,"covered":27,"skipped":0,"pct":100},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":31,"covered":31,"skipped":0,"pct":100},"branches":{"total":1,"covered":1,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/hasher/index.ts": {"lines":{"total":43,"covered":39,"skipped":0,"pct":90.69},"functions":{"total":11,"covered":10,"skipped":0,"pct":90.9},"statements":{"total":49,"covered":45,"skipped":0,"pct":91.83},"branches":{"total":19,"covered":14,"skipped":0,"pct":73.68}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/cipher/cipher.ts": {"lines":{"total":23,"covered":23,"skipped":0,"pct":100},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":27,"covered":27,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/src/utils/index.ts": {"lines":{"total":58,"covered":52,"skipped":0,"pct":89.65},"functions":{"total":13,"covered":13,"skipped":0,"pct":100},"statements":{"total":64,"covered":58,"skipped":0,"pct":90.62},"branches":{"total":23,"covered":19,"skipped":0,"pct":82.6}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/database.ts": {"lines":{"total":13,"covered":13,"skipped":0,"pct":100},"functions":{"total":6,"covered":6,"skipped":0,"pct":100},"statements":{"total":19,"covered":19,"skipped":0,"pct":100},"branches":{"total":3,"covered":3,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/keys.ts": {"lines":{"total":2,"covered":2,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":2,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/VerifiableCredentials/findByQuery.ts": {"lines":{"total":19,"covered":19,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":22,"covered":21,"skipped":0,"pct":95.45},"branches":{"total":2,"covered":1,"skipped":0,"pct":50}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/VerifiableCredentials/saveVC.ts": {"lines":{"total":12,"covered":12,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":13,"covered":13,"skipped":0,"pct":100},"branches":{"total":1,"covered":1,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/keys.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/VerifiableCredentials/findByQuery.ts": {"lines":{"total":19,"covered":19,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":22,"covered":21,"skipped":0,"pct":95.45},"branches":{"total":6,"covered":5,"skipped":0,"pct":83.33}}
,"/home/ipbyrne/projects/mongo-encryption-query/testing/VerifiableCredentials/saveVC.ts": {"lines":{"total":12,"covered":12,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":13,"covered":13,"skipped":0,"pct":100},"branches":{"total":3,"covered":3,"skipped":0,"pct":100}}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Isaac Byrne (me@ipbyrne.com)",
"module": "dist/index.esm.js",
"license": "Apache-2.0",
"version": "0.0.9",
"version": "1.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"keywords": [
Expand Down
17 changes: 14 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,20 @@ import { decrypt, encrypt, generate } from "./cipher/cipher";
import { PrivateKeyJwk, Data } from "./types";
import * as Types from "./types";

export const encryptQuery = (query: any, privateKeyJwk: PrivateKeyJwk) => {
const hashedQuery = createEntrypedQuery(query, privateKeyJwk);
return hashedQuery;
export const encryptQuery = (
query: any,
privateKeyJwk: PrivateKeyJwk,
prefix?: string
) => {
const encryptedQuery = createEntrypedQuery(query, privateKeyJwk);
if (prefix) {
const prefixedEncryptedQuery: any = {};
Object.keys(encryptedQuery).forEach((key: string) => {
prefixedEncryptedQuery[`${prefix}.${key}`] = encryptedQuery[key];
});
return prefixedEncryptedQuery;
}
return encryptedQuery;
};

export const encryptData = (
Expand Down
10 changes: 10 additions & 0 deletions testing/VerifiableCredentials/encryptedSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,15 @@ describe("Can save and search encrypted data", () => {
expect(results[0].credentialSubject.nestedArrayData[0].index).toBe("3");
expect(results[1].credentialSubject.nestedArrayData[0].index).toBe("1");
});

it("should be able to query VC by multiple values", async () => {
const results = (await findByQuery({
"credentialSubject.address.zipCode": "77379",
"credentialSubject.id": "did:example:234",
name: "Test 3",
})) as any[];
expect(results[0].credentialSubject.id).toBe("did:example:234");
expect(results[0].name).toBe("Test 3");
});
});
});

0 comments on commit 508f080

Please sign in to comment.