Skip to content

Commit

Permalink
feat: update share object
Browse files Browse the repository at this point in the history
  • Loading branch information
gllm-dev committed Mar 27, 2024
1 parent 867bafc commit 0876a59
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@openfort/shield-js",
"version": "0.0.3",
"version": "0.0.4",
"description": "",
"author": "Openfort",
"repository": {
Expand Down
49 changes: 28 additions & 21 deletions src/core/ShieldSDK.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,12 +15,14 @@ export class ShieldSDK {
this._baseURL = baseURL;
}

public async getSecret(auth: ShieldAuthOptions): Promise<string> {
public async getSecret(auth: ShieldAuthOptions): Promise<Share> {
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) {
Expand All @@ -36,13 +39,13 @@ export class ShieldSDK {
}
}

public async storeSecret(secret: string, auth: ShieldAuthOptions): Promise<void> {
public async storeSecret(share: Share, auth: ShieldAuthOptions): Promise<void> {
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) {
Expand All @@ -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;
}

}
5 changes: 5 additions & 0 deletions src/models/Share.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Share {
secret: string;
userEntropy: boolean;
salt?: string
}
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const VERSION = "0.0.3";
export const VERSION = "0.0.4";
export const PACKAGE = "@openfort/shield-js";

0 comments on commit 0876a59

Please sign in to comment.