-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add internal package called @unkey/vault (#2279)
* chore: add internal package called @unkey/vault * fix: lint --------- Co-authored-by: chronark <[email protected]>
- Loading branch information
1 parent
eb15536
commit 26b587a
Showing
4 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "@unkey/vault", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "src/index.ts", | ||
"types": "src/index.ts", | ||
"keywords": [], | ||
"author": "", | ||
"devDependencies": { | ||
"@types/node": "^20.14.9", | ||
"@unkey/tsconfig": "workspace:*", | ||
"typescript": "^5.5.3" | ||
} | ||
} |
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,101 @@ | ||
export type EncryptRequest = { | ||
keyring: string; | ||
data: string; | ||
}; | ||
|
||
export type EncryptResponse = { | ||
encrypted: string; | ||
keyId: string; | ||
}; | ||
|
||
export type EncryptBulkRequest = { | ||
keyring: string; | ||
data: string[]; | ||
}; | ||
|
||
export type EncryptBulkResponse = { | ||
encrypted: EncryptResponse[]; | ||
}; | ||
|
||
export type DecryptRequest = { | ||
keyring: string; | ||
encrypted: string; | ||
}; | ||
|
||
export type DecryptResponse = { | ||
plaintext: string; | ||
}; | ||
|
||
export type RequestContext = { | ||
requestId: string; | ||
}; | ||
|
||
export class Vault { | ||
private readonly baseUrl: string; | ||
private readonly token: string; | ||
|
||
constructor(baseUrl: string, token: string) { | ||
this.baseUrl = baseUrl; | ||
this.token = token; | ||
} | ||
|
||
public async encrypt(c: RequestContext, req: EncryptRequest): Promise<EncryptResponse> { | ||
const url = `${this.baseUrl}/vault.v1.VaultService/Encrypt`; | ||
const res = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.token}`, | ||
"Unkey-Request-Id": c.requestId, | ||
}, | ||
body: JSON.stringify(req), | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Unable to encrypt, fetch error: ${await res.text()}`); | ||
} | ||
|
||
return res.json(); | ||
} | ||
|
||
public async encryptBulk( | ||
c: RequestContext, | ||
req: EncryptBulkRequest, | ||
): Promise<EncryptBulkResponse> { | ||
const url = `${this.baseUrl}/vault.v1.VaultService/EncryptBulk`; | ||
const res = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.token}`, | ||
"Unkey-Request-Id": c.requestId, | ||
}, | ||
body: JSON.stringify(req), | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Unable to encryptBulk, fetch error: ${await res.text()}`); | ||
} | ||
|
||
return res.json(); | ||
} | ||
|
||
public async decrypt(c: RequestContext, req: DecryptRequest): Promise<DecryptResponse> { | ||
const url = `${this.baseUrl}/vault.v1.VaultService/Decrypt`; | ||
const res = await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${this.token}`, | ||
"Unkey-Request-Id": c.requestId, | ||
}, | ||
body: JSON.stringify(req), | ||
}); | ||
|
||
if (!res.ok) { | ||
throw new Error(`Unable to decrypt, fetch error: ${await res.text()}`); | ||
} | ||
|
||
return res.json(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.