diff --git a/package-lock.json b/package-lock.json index 17f8343..1c1c5e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@openfort/shield-js", - "version": "0.0.3", + "version": "0.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@openfort/shield-js", - "version": "0.0.3", + "version": "0.0.4", "license": "MIT", "dependencies": { "axios": "^1.6.8", diff --git a/package.json b/package.json index 91fd993..4855a33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@openfort/shield-js", - "version": "0.0.3", + "version": "0.0.4", "description": "", "author": "Openfort", "repository": { diff --git a/src/core/ShieldSDK.ts b/src/core/ShieldSDK.ts index 4d99e4f..78629c2 100644 --- a/src/core/ShieldSDK.ts +++ b/src/core/ShieldSDK.ts @@ -1,10 +1,11 @@ import {ShieldOptions} from "../models/ShieldOptions"; import {ShieldAuthOptions} from "../models/ShieldAuthOptions"; import {OpenfortAuthOptions} from "../models/OpenfortAuthOptions"; -import axios, {AxiosHeaders} from "axios"; +import axios, {AxiosHeaders, AxiosRequestConfig} from "axios"; import {CustomAuthOptions} from "../models/CustomAuthOptions"; import {NoSecretFoundError} from "../errors/NoSecretFoundError"; import {SecretAlreadyExistsError} from "../errors/SecretAlreadyExistsError"; +import {Share} from "../models/Share"; export class ShieldSDK { private readonly _baseURL: string; @@ -14,12 +15,14 @@ export class ShieldSDK { this._baseURL = baseURL; } - public async getSecret(auth: ShieldAuthOptions): Promise { + public async getSecret(auth: ShieldAuthOptions): Promise { try { - const res = await axios.get(`${this._baseURL}/shares`, { - headers: this.getAuthHeaders(auth) - }); - return res.data.secret; + const res = await axios.get(`${this._baseURL}/shares`, this.getAuthHeaders(auth)); + return { + secret: res.data.secret, + userEntropy: res.data.user_entropy, + salt: res.data.salt + }; } catch (error: any) { if (error.response) { if (error.response.status === 404) { @@ -36,13 +39,13 @@ export class ShieldSDK { } } - public async storeSecret(secret: string, auth: ShieldAuthOptions): Promise { + public async storeSecret(share: Share, auth: ShieldAuthOptions): Promise { try { const res = await axios.post(`${this._baseURL}/shares`, { - "secret": secret - }, { - headers: this.getAuthHeaders(auth) - }); + "secret": share.secret, + "user_entropy": share.userEntropy, + "salt": share.salt + }, this.getAuthHeaders(auth)); return res.data.share; } catch (error: any) { if (error.response) { @@ -69,27 +72,31 @@ export class ShieldSDK { return 'customToken' in options; } - private getAuthHeaders(options: ShieldAuthOptions): AxiosHeaders { - const headers = new AxiosHeaders(); - headers.set("x-api-key", this._apiKey) - headers.set("x-auth-provider", options.authProvider) - headers.set("Access-Control-Allow-Origin", this._baseURL) + private getAuthHeaders(options: ShieldAuthOptions): AxiosRequestConfig { + const opts = { + headers: { + "x-api-key": this._apiKey, + "x-auth-provider": options.authProvider, + "Access-Control-Allow-Origin": this._baseURL + } + } if (this.isOpenfortAuthOptions(options)) { - headers.setAuthorization(`Bearer ${options.openfortOAuthToken}`) + opts.headers["Authorization"] = `Bearer ${options.openfortOAuthToken}` if (options.openfortOAuthProvider) { - headers.set("x-openfort-provider", options.openfortOAuthProvider) + opts.headers["x-openfort-provider"] = options.openfortOAuthProvider } if (options.openfortOAuthTokenType) { - headers.set("x-openfort-token-type", options.openfortOAuthTokenType) + opts.headers["x-openfort-token-type"] = options.openfortOAuthTokenType } } if (this.isCustomAuthOptions(options)) { - headers.setAuthorization(`Bearer ${options.customToken}`) + opts.headers["Authorization"] = `Bearer ${options.customToken}` } - return headers + + return opts; } } \ No newline at end of file diff --git a/src/models/Share.ts b/src/models/Share.ts new file mode 100644 index 0000000..05d3b84 --- /dev/null +++ b/src/models/Share.ts @@ -0,0 +1,5 @@ +export interface Share { + secret: string; + userEntropy: boolean; + salt?: string +} \ No newline at end of file diff --git a/src/version.ts b/src/version.ts index 5d20d02..0578096 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1,2 +1,2 @@ -export const VERSION = "0.0.3"; +export const VERSION = "0.0.4"; export const PACKAGE = "@openfort/shield-js";