Skip to content

Commit ee34245

Browse files
authoredApr 10, 2024··
fix(http): honor authType first (#28304)
1 parent 85dfa58 commit ee34245

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed
 

‎lib/util/http/auth.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,30 @@ describe('util/http/auth', () => {
189189
token: 'test',
190190
});
191191
});
192+
193+
it(`honors authType`, () => {
194+
const opts: GotOptions = {
195+
headers: {},
196+
token: 'test',
197+
context: {
198+
authType: 'Bearer',
199+
},
200+
hostType: 'custom',
201+
};
202+
203+
applyAuthorization(opts);
204+
205+
expect(opts).toEqual({
206+
context: {
207+
authType: 'Bearer',
208+
},
209+
headers: {
210+
authorization: 'Bearer test',
211+
},
212+
hostType: 'custom',
213+
token: 'test',
214+
});
215+
});
192216
});
193217

194218
describe('removeAuthorization', () => {

‎lib/util/http/auth.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ export function applyAuthorization<GotOptions extends AuthGotOptions>(
2929

3030
options.headers ??= {};
3131
if (options.token) {
32-
if (
32+
const authType = options.context?.authType;
33+
if (authType) {
34+
if (authType === 'Token-Only') {
35+
options.headers.authorization = options.token;
36+
} else {
37+
options.headers.authorization = `${authType} ${options.token}`;
38+
}
39+
} else if (
3340
options.hostType &&
3441
GITEA_API_USING_HOST_TYPES.includes(options.hostType)
3542
) {
@@ -61,14 +68,7 @@ export function applyAuthorization<GotOptions extends AuthGotOptions>(
6168
options.headers.authorization = `Bearer ${options.token}`;
6269
}
6370
} else {
64-
// Custom Auth type, eg `Basic XXXX_TOKEN`
65-
const type = options.context?.authType ?? 'Bearer';
66-
67-
if (type === 'Token-Only') {
68-
options.headers.authorization = options.token;
69-
} else {
70-
options.headers.authorization = `${type} ${options.token}`;
71-
}
71+
options.headers.authorization = `Bearer ${options.token}`;
7272
}
7373
delete options.token;
7474
} else if (options.password !== undefined) {

0 commit comments

Comments
 (0)
Please sign in to comment.