Skip to content

Commit 5bcc7c4

Browse files
feat: add support for custom kms hosts
1 parent b6ac744 commit 5bcc7c4

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import {
99
serverSessionKeys,
1010
} from "./utils/crypto";
1111

12-
const PH_VERSION = "v1";
1312
type PhaseCiphertext = `ph:${string}:${string}:${string}:${string}`;
1413
type PhaseAppId = `phApp:${string}:${string}`;
1514
type PhaseAppSecret = `pss:${string}:${string}:${string}${string}`;
1615

16+
const PH_VERSION = "v1";
17+
const DEFAULT_KMS_HOST = "https://kms.phase.dev";
18+
1719
export default class Phase {
1820
appId: string;
1921
appPubKey: string;
@@ -24,10 +26,10 @@ export default class Phase {
2426
keyshare0: string;
2527
keyshare1UnwrapKey: string;
2628
};
29+
kmsHost: string;
2730

28-
constructor(appId: string, appSecret: string) {
31+
constructor(appId: string, appSecret: string, kmsHost?: string) {
2932
const appIdRegex = /^phApp:v(\d+):([a-fA-F0-9]{64})$/;
30-
// Update regex after switching to XOR based shares
3133
const appSecretRegex =
3234
/^pss:v(\d+):([a-fA-F0-9]{64}):([a-fA-F0-9]{64,128}):([a-fA-F0-9]{64})/gm;
3335

@@ -37,6 +39,7 @@ export default class Phase {
3739

3840
this.appId = appId;
3941
this.appPubKey = appId.split(":")[2];
42+
this.kmsHost = kmsHost ? `${kmsHost}/kms` : DEFAULT_KMS_HOST;
4043

4144
const appSecretSegments = appSecret.split(":");
4245

@@ -111,7 +114,8 @@ export default class Phase {
111114
this.appSecret.appToken,
112115
this.appSecret.keyshare1UnwrapKey,
113116
this.appId,
114-
ciphertext.data.length / 2
117+
ciphertext.data.length / 2,
118+
this.kmsHost
115119
);
116120

117121
const appPrivKey = await reconstructSecret([

src/utils/wrappedShare.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ export const fetchAppKeyShare = async (
1313
appToken: string,
1414
wrapKey: string,
1515
appId: string,
16-
dataSize: number
16+
dataSize: number,
17+
host: string
1718
) => {
1819
await _sodium.ready;
1920
const sodium = _sodium;
2021

21-
const PHASE_KMS_URI = `https://kms.phase.dev/${appId}`;
22+
const PHASE_KMS_URI = `${host}/${appId}`;
2223

2324
const headers = {
2425
Authorization: `Bearer ${appToken}`,

0 commit comments

Comments
 (0)