Skip to content

Commit

Permalink
refactor(thirdweb): dry storage and authentication auth token retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
ElasticBottle committed Jan 24, 2024
1 parent a48fcbe commit b19ddde
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,18 @@ export const pre2FA = async (
arg: MultiStepAuthProviderType & { storage: AuthTokenStorageType },
): Promise<InitiateAuthResultType> => {
const { ROUTE_INITIATE_2FA_AUTH } = await import("./routes.js");
const { AuthenticationError } = await import("./authentication.error.js");
const { THIRDWEB_AUTH_TOKEN_KEY } = await import(
"./authentication.constant.js"
);

const token = await arg.storage.fetchToken({
key: THIRDWEB_AUTH_TOKEN_KEY,
});
if (!token) {
throw new AuthenticationError("No authenticated user found!");
}

switch (arg.provider) {
case "email": {
const { email } = arg;
Expand All @@ -178,9 +187,7 @@ export const pre2FA = async (
email,
}),
headers: {
Authorization: `Bearer ${await arg.storage.fetchToken({
key: THIRDWEB_AUTH_TOKEN_KEY,
})}`,
Authorization: `Bearer ${token}`,
},
});
const result = await resp.json();
Expand All @@ -194,9 +201,7 @@ export const pre2FA = async (
phone,
}),
headers: {
Authorization: `Bearer ${await arg.storage.fetchToken({
key: THIRDWEB_AUTH_TOKEN_KEY,
})}`,
Authorization: `Bearer ${token}`,
},
});
const result = await resp.json();
Expand Down
27 changes: 23 additions & 4 deletions packages/thirdweb/src/wallets/embedded-wallet/core/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import type {
} from "./storage.type.js";
import type { WalletDetailType } from "./wallet.type.js";

export const getUserAuthToken = async (authUser?: AuthUserType) => {
const { StorageError } = await import("./storage.error.js");

const token = authUser?.authToken;
if (!token) {
throw new StorageError(
"An authenticated user is required to save the key material with thirdweb",
);
}
return token;
};

/** Sends an encrypted share / key to thirdweb for storage
* @throws if developer is not on thirdweb managed storage
*/
Expand All @@ -33,12 +45,13 @@ export const saveEncryptedInThirdweb = (arg: {
"Invalid encryption. The encrypted value must not be the same as the original value",
);
}
const token = await getUserAuthToken(authUser);

const saveResp = await fetch(ROUTE_STORAGE_ENCRYPTED(), {
method: "POST",
headers: {
"x-secret-key": secretKey ?? "",
"x-auth-user-token": authUser?.authToken ?? "",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
// TODO: figure out what goes here
Expand Down Expand Up @@ -78,11 +91,13 @@ export const saveInThirdweb = (): SaveKeyType => {
);
}

const token = await getUserAuthToken(authUser);

const saveResp = await fetch(ROUTE_STORAGE_BASIC(), {
method: "POST",
headers: {
"x-secret-key": secretKey ?? "",
"x-auth-user-token": authUser?.authToken ?? "",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
// walletId: walletDetail.,
Expand Down Expand Up @@ -137,11 +152,13 @@ export const loadEncryptedFromThirdweb = (arg: {
);
}

const token = await getUserAuthToken(authUser);

const encryptedKeyMaterialResp = await fetch(ROUTE_STORAGE_ENCRYPTED(), {
method: "GET",
headers: {
"x-secret-key": secretKey ?? "",
"x-auth-user-token": authUser?.authToken ?? "",
Authorization: `Bearer ${token}`,
},
});
if (!encryptedKeyMaterialResp.ok) {
Expand Down Expand Up @@ -178,11 +195,13 @@ export const loadFromThirdweb = (): LoadKeyType => {
);
}

const token = await getUserAuthToken(authUser);

const keyMaterialResp = await fetch(ROUTE_STORAGE_BASIC(), {
method: "GET",
headers: {
"x-secret-key": secretKey ?? "",
"x-auth-user-token": authUser?.authToken ?? "",
Authorization: `Bearer ${token}`,
},
});

Expand Down

0 comments on commit b19ddde

Please sign in to comment.