Skip to content

Commit

Permalink
Merge pull request #48 from Coaktion/fix/bearer-auth
Browse files Browse the repository at this point in the history
Fix/bearer auth
  • Loading branch information
inaciogu authored Sep 27, 2023
2 parents 074771f + 6b8d59d commit 48d92ac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ export class BearerAuth implements AuthBasic {
const response = await this.client.request({
method: 'post',
url: this.authOptions.endpoint,
auth: {
username: this.authOptions.username,
password: this.authOptions.password
},
data: this.authOptions.bearer.data,
params: this.authOptions.bearer.params || {}
params: this.authOptions.bearer.params || {},
headers: this.authOptions.bearer.headers || {}
});
return 'Bearer ' + response.data.access_token;
} catch (error) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type Endpoints = {
export type BearerAuthOptions = {
data?: object;
params?: object;
headers?: object;
};

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ describe('BearerAuth', () => {
headerKey: 'Authorization',
bearer: {
data: {},
params: {}
params: {},
headers: {
Authorization: 'Basic 123'
}
}
});
const mock = new MockAdapter(auth.client);
mock.onPost('/auth').reply(200, { access_token: 'abc' });
expect(auth).toBeInstanceOf(BearerAuth);
expect(auth.authOptions.baseUrl).toBe('http://localhost');
expect(auth.authOptions.endpoint).toBe('/auth');
expect(auth.authOptions.bearer?.headers).toEqual({
Authorization: 'Basic 123'
});
expect(await auth.getToken()).toEqual({ Authorization: 'Bearer abc' });
});

Expand Down

0 comments on commit 48d92ac

Please sign in to comment.