Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support basic auth #3079

Merged
merged 6 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-tools-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

feat: support basic auth
43 changes: 43 additions & 0 deletions packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,49 @@ const getCustomFetch =
* @group node
*/
describe('Provider', () => {
it('supports basic auth', async () => {
using launched = await setupTestProviderAndWallets();
const {
provider: { url },
} = launched;

const usernameAndPassword = 'securest:ofpasswords';
const parsedUrl = new URL(url);
const hostAndPath = `${parsedUrl.host}${parsedUrl.pathname}`;
const authUrl = `http://${usernameAndPassword}@${hostAndPath}`;
const provider = await Provider.create(authUrl);

const fetchSpy = vi.spyOn(global, 'fetch');

await provider.operations.getChain();

const expectedAuthToken = `Basic ${btoa(usernameAndPassword)}`;
const [requestUrl, request] = fetchSpy.mock.calls[0];
expect(requestUrl).toEqual(url);
expect(request?.headers).toMatchObject({
Authorization: expectedAuthToken,
});
});

it('custom requestMiddleware is not overwritten by basic auth', async () => {
using launched = await setupTestProviderAndWallets();
const {
provider: { url },
} = launched;

const usernameAndPassword = 'securest:ofpasswords';
const parsedUrl = new URL(url);
const hostAndPath = `${parsedUrl.host}${parsedUrl.pathname}`;
const authUrl = `http://${usernameAndPassword}@${hostAndPath}`;

const requestMiddleware = vi.fn();
await Provider.create(authUrl, {
requestMiddleware,
});

expect(requestMiddleware).toHaveBeenCalled();
});

it('should throw an error when retrieving a transaction with an unknown transaction type', async () => {
using launched = await setupTestProviderAndWallets();
const { provider } = launched;
Expand Down
28 changes: 27 additions & 1 deletion packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,20 @@ export default class Provider {
}
}

private static extractBasicAuth(url: string): { url: string; auth: string | undefined } {
const parsedUrl = new URL(url);

const username = parsedUrl.username;
const password = parsedUrl.password;
const urlNoBasicAuth = `${parsedUrl.origin}${parsedUrl.pathname}`;
if (!(username && password)) {
return { url, auth: undefined };
}

const auth = `Basic ${btoa(`${username}:${password}`)}`;
return { url: urlNoBasicAuth, auth };
}

/**
* Creates a new instance of the Provider class. This is the recommended way to initialize a Provider.
*
Expand All @@ -460,8 +474,20 @@ export default class Provider {
* @returns A promise that resolves to a Provider instance.
*/
static async create(url: string, options: ProviderOptions = {}): Promise<Provider> {
const provider = new Provider(url, options);
const { url: urlToUse, auth } = this.extractBasicAuth(url);
const provider = new Provider(urlToUse, {
...options,
requestMiddleware: async (request) => {
if (auth) {
request.headers ??= {};
(request.headers as Record<string, string>).Authorization = auth;
}
return options.requestMiddleware?.(request) ?? request;
},
});

await provider.fetchChainAndNodeInfo();

return provider;
}

Expand Down