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

Trim Bearer Authentication Strings #7279

Merged
merged 2 commits into from
Jul 12, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, describe, expect, it } from '@jest/globals';

import { globalBeforeEach } from '../../../__jest__/before-each';
import { getBearerAuthHeader } from '../get-header';

describe('getBearerAuthHeader()', () => {
beforeEach(globalBeforeEach);

it('succeed with token and prefix', () => {
const header = getBearerAuthHeader('token', 'prefix');
expect(header).toEqual({
name: 'Authorization',
value: 'prefix token',
});
});

it('succeed with no prefix', () => {
const header = getBearerAuthHeader('token');
expect(header).toEqual({
name: 'Authorization',
value: 'Bearer token',
});
});

it('succeed with token with leading and trailing spaces', () => {
const header = getBearerAuthHeader(' token ', 'prefix');
expect(header).toEqual({
name: 'Authorization',
value: 'prefix token',
});
});

it('succeed with prefix with leading and trailing spaces', () => {
const header = getBearerAuthHeader('token', ' prefix ');
expect(header).toEqual({
name: 'Authorization',
value: 'prefix token',
});
});

it('succeed with token and prefix with leading and trailing spaces', () => {
const header = getBearerAuthHeader(' token ', ' prefix ');
expect(header).toEqual({
name: 'Authorization',
value: 'prefix token',
});
});
});
2 changes: 1 addition & 1 deletion packages/insomnia/src/network/bearer-auth/get-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { RequestHeader } from '../../models/request';

export function getBearerAuthHeader(token: string, prefix?: string) {
const name = 'Authorization';
const value = `${prefix || 'Bearer'} ${token}`;
const value = `${prefix?.trim() || 'Bearer'} ${token.trim()}`;
const requestHeader: RequestHeader = {
name,
value,
Expand Down
Loading