From bd4670c3da7fd195b630c742239b95d8faae5e01 Mon Sep 17 00:00:00 2001 From: theborakompanioni Date: Wed, 4 Oct 2023 11:51:55 +0200 Subject: [PATCH] dev(auth): add distinct method to parse auth props --- src/components/CreateWallet.jsx | 8 +------- src/components/ImportWallet.tsx | 16 ++-------------- src/components/Wallets.jsx | 9 ++------- src/context/WalletContext.tsx | 9 ++------- src/libs/JmWalletApi.ts | 16 +++++++++++++++- 5 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/components/CreateWallet.jsx b/src/components/CreateWallet.jsx index 0db20859..784d3e49 100644 --- a/src/components/CreateWallet.jsx +++ b/src/components/CreateWallet.jsx @@ -100,13 +100,7 @@ export default function CreateWallet({ parentRoute, startWallet }) { const body = await (res.ok ? res.json() : Api.Helper.throwError(res)) const { seedphrase, walletname: createdWalletFileName } = body - const auth = { - token: body.token, - token_type: body.token_type, - expires_in: body.expires_in, - scope: body.scope, - refresh_token: body.refresh_token, - } + const auth = Api.Helper.parseAuthProps(body) setCreatedWallet({ walletFileName: createdWalletFileName, seedphrase, password, auth }) } catch (e) { const message = t('create_wallet.error_creating_failed', { diff --git a/src/components/ImportWallet.tsx b/src/components/ImportWallet.tsx index c3a1a78e..d6cfc1b9 100644 --- a/src/components/ImportWallet.tsx +++ b/src/components/ImportWallet.tsx @@ -441,13 +441,7 @@ export default function ImportWallet({ parentRoute, startWallet }: ImportWalletP const recoverBody = await (recoverResponse.ok ? recoverResponse.json() : Api.Helper.throwError(recoverResponse)) const { walletname: importedWalletFileName } = recoverBody - let auth: Api.ApiAuthContext = { - token: recoverBody.token, - token_type: recoverBody.token_type, - expires_in: recoverBody.token_type, - scope: recoverBody.token_type, - refresh_token: recoverBody.token_type, - } + let auth: Api.ApiAuthContext = Api.Helper.parseAuthProps(recoverBody) setRecoveredWallet({ walletFileName: importedWalletFileName, auth }) // Step #2: update the gaplimit config value if necessary @@ -482,13 +476,7 @@ export default function ImportWallet({ parentRoute, startWallet }: ImportWalletP const unlockResponse = await Api.postWalletUnlock({ walletName: importedWalletFileName }, { password }) const unlockBody = await (unlockResponse.ok ? unlockResponse.json() : Api.Helper.throwError(unlockResponse)) - auth = { - token: unlockBody.token, - token_type: unlockBody.token_type, - expires_in: unlockBody.expires_in, - scope: unlockBody.scope, - refresh_token: unlockBody.refresh_token, - } + auth = Api.Helper.parseAuthProps(unlockBody) // Step #4: reset `gaplimit´ to previous value if necessary if (gaplimitUpdateNecessary) { diff --git a/src/components/Wallets.jsx b/src/components/Wallets.jsx index 6e5fca85..c9c9ea95 100644 --- a/src/components/Wallets.jsx +++ b/src/components/Wallets.jsx @@ -65,13 +65,8 @@ export default function Wallets({ currentWallet, startWallet, stopWallet }) { setUnlockWalletName(undefined) - const auth = { - token: body.token, - token_type: body.token_type, - expires_in: body.expires_in, - scope: body.scope, - refresh_token: body.refresh_token, - } + const auth = Api.Helper.parseAuthProps(body) + startWallet(body.walletname, auth) navigate(routes.wallet) } catch (e) { diff --git a/src/context/WalletContext.tsx b/src/context/WalletContext.tsx index 1995160e..f0f43f60 100644 --- a/src/context/WalletContext.tsx +++ b/src/context/WalletContext.tsx @@ -328,13 +328,8 @@ const WalletProvider = ({ children }: PropsWithChildren) => { ) .then((res) => (res.ok ? res.json() : Api.Helper.throwError(res))) .then((body) => { - const auth = { - token: body.token, - token_type: body.token_type, - expires_in: body.expires_in, - scope: body.scope, - refresh_token: body.refresh_token, - } + const auth = Api.Helper.parseAuthProps(body) + setSession({ name: currentWallet.name, auth }) currentWallet.updateToken(auth.token) console.debug('Successfully renewed auth token.') diff --git a/src/libs/JmWalletApi.ts b/src/libs/JmWalletApi.ts index 5509530e..f87e4052 100644 --- a/src/libs/JmWalletApi.ts +++ b/src/libs/JmWalletApi.ts @@ -75,7 +75,7 @@ interface ApiError { type WalletType = 'sw-fb' interface TokenRequest { - grant_type: string + grant_type: 'refresh_token' | string refresh_token: string } @@ -224,11 +224,25 @@ const Helper = (() => { return { 'x-jm-authorization': `Bearer ${token}` } } + // Simple helper method to parse auth properties. + // TODO: This can be removed when the API methods + // return typed responses (see #670) + const parseAuthProps = (body: any): ApiAuthContext => { + return { + token: body.token, + token_type: body.token_type, + expires_in: body.expires_in, + scope: body.scope, + refresh_token: body.refresh_token, + } + } + return { throwError, throwResolved, extractErrorMessage, buildAuthHeader, + parseAuthProps, } })()