mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-25 15:01:49 +00:00
853290db9c
`lib/util/http/auth.ts` and `lib/util/http/host-rules.ts` were the last readers of the `*_API_USING_HOST_TYPES` constants, which the previous commits left as aliases of `PLATFORM_FAMILIES`. Both now read the table directly and the aliases are deleted, so each family's host types are declared in exactly one place. Also covers the `forgejo` fallback in `findMatchingRule()`, which had no test: the shared setup only registers a `gitea` host rule, so the forgejo branch was never entered. Behaviour changes: none. The aliases pointed at the same arrays.
81 lines
2.7 KiB
TypeScript
81 lines
2.7 KiB
TypeScript
import { isNonEmptyString } from '@sindresorhus/is';
|
|
import { PLATFORM_FAMILIES } from '../../constants/index.ts';
|
|
import type { GotOptions } from './types.ts';
|
|
|
|
export type AuthGotOptions = Pick<
|
|
GotOptions,
|
|
| 'hostType'
|
|
| 'headers'
|
|
| 'noAuth'
|
|
| 'context'
|
|
| 'token'
|
|
| 'username'
|
|
| 'password'
|
|
>;
|
|
|
|
export function applyAuthorization<GotOptions extends AuthGotOptions>(
|
|
inOptions: GotOptions,
|
|
): GotOptions {
|
|
const options: GotOptions = { ...inOptions };
|
|
|
|
if (isNonEmptyString(options.headers?.authorization) || options.noAuth) {
|
|
return options;
|
|
}
|
|
|
|
options.headers ??= {};
|
|
if (options.token) {
|
|
const authType = options.context?.authType;
|
|
if (authType) {
|
|
if (authType === 'Token-Only') {
|
|
options.headers.authorization = options.token;
|
|
} else {
|
|
options.headers.authorization = `${authType} ${options.token}`;
|
|
}
|
|
} else if (options.token.startsWith('x-access-token:')) {
|
|
// GitHub App installation token — prefix is set by the GitHub platform init
|
|
const appToken = options.token.replace('x-access-token:', '');
|
|
options.headers.authorization = `Bearer ${appToken}`;
|
|
} else if (
|
|
options.hostType &&
|
|
PLATFORM_FAMILIES.forgejo.apiUsingHostTypes.includes(options.hostType)
|
|
) {
|
|
options.headers.authorization = `Bearer ${options.token}`;
|
|
} else if (
|
|
options.hostType &&
|
|
PLATFORM_FAMILIES.gitea.apiUsingHostTypes.includes(options.hostType)
|
|
) {
|
|
// Gitea v1.8.0 and later support `Bearer` as alternate to `token`
|
|
// https://github.com/go-gitea/gitea/pull/5378
|
|
options.headers.authorization = `Bearer ${options.token}`;
|
|
} else if (
|
|
options.hostType &&
|
|
PLATFORM_FAMILIES.github.apiUsingHostTypes.includes(options.hostType)
|
|
) {
|
|
options.headers.authorization = `token ${options.token}`;
|
|
} else if (
|
|
options.hostType &&
|
|
PLATFORM_FAMILIES.gitlab.apiUsingHostTypes.includes(options.hostType)
|
|
) {
|
|
// GitLab versions earlier than 12.2 only support authentication with
|
|
// a personal access token, which is 20 characters long.
|
|
if (options.token.length === 20) {
|
|
options.headers['Private-token'] = options.token;
|
|
} else {
|
|
options.headers.authorization = `Bearer ${options.token}`;
|
|
}
|
|
} else {
|
|
options.headers.authorization = `Bearer ${options.token}`;
|
|
}
|
|
delete options.token;
|
|
} else if (options.password !== undefined) {
|
|
// Otherwise got will add username and password to url and header
|
|
const auth = Buffer.from(
|
|
`${options.username ?? ''}:${options.password}`,
|
|
).toString('base64');
|
|
options.headers.authorization = `Basic ${auth}`;
|
|
delete options.username;
|
|
delete options.password;
|
|
}
|
|
return options;
|
|
}
|