mirror of
https://github.com/renovatebot/renovate.git
synced 2025-01-13 15:36:25 +00:00
refactor: move auth to http module (#6502)
This commit is contained in:
parent
9691122d9b
commit
6c38eb359a
4 changed files with 37 additions and 42 deletions
|
@ -1,34 +0,0 @@
|
|||
import {
|
||||
PLATFORM_TYPE_GITEA,
|
||||
PLATFORM_TYPE_GITHUB,
|
||||
PLATFORM_TYPE_GITLAB,
|
||||
} from '../../constants/platforms';
|
||||
import { logger } from '../../logger';
|
||||
import { create } from './util';
|
||||
|
||||
export default create({
|
||||
options: {},
|
||||
handler: (options, next) => {
|
||||
if (options.auth || options.headers.authorization) {
|
||||
return next(options);
|
||||
}
|
||||
if (options.token) {
|
||||
logger.trace(
|
||||
{ hostname: options.hostname },
|
||||
'Converting token to Bearer auth'
|
||||
);
|
||||
if (
|
||||
options.hostType === PLATFORM_TYPE_GITHUB ||
|
||||
options.hostType === PLATFORM_TYPE_GITEA
|
||||
) {
|
||||
options.headers.authorization = `token ${options.token}`; // eslint-disable-line no-param-reassign
|
||||
} else if (options.hostType === PLATFORM_TYPE_GITLAB) {
|
||||
options.headers['Private-token'] = options.token; // eslint-disable-line no-param-reassign
|
||||
} else {
|
||||
options.headers.authorization = `Bearer ${options.token}`; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
delete options.token; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
return next(options);
|
||||
},
|
||||
});
|
|
@ -1,14 +1,15 @@
|
|||
import got from 'got';
|
||||
import auth from './auth';
|
||||
import { mergeInstances } from './util';
|
||||
import { create, mergeInstances } from './util';
|
||||
|
||||
export * from './common';
|
||||
|
||||
/*
|
||||
* This is the default got instance for Renovate.
|
||||
* - Cache all GET requests for the lifetime of the repo
|
||||
*
|
||||
*/
|
||||
export const api = mergeInstances(got, auth);
|
||||
const dummy = create({
|
||||
options: {},
|
||||
handler: (options, next) => {
|
||||
return next(options);
|
||||
},
|
||||
});
|
||||
|
||||
export const api = mergeInstances(got, dummy);
|
||||
|
||||
export default api;
|
||||
|
|
26
lib/util/http/auth.ts
Normal file
26
lib/util/http/auth.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import {
|
||||
PLATFORM_TYPE_GITEA,
|
||||
PLATFORM_TYPE_GITHUB,
|
||||
PLATFORM_TYPE_GITLAB,
|
||||
} from '../../constants/platforms';
|
||||
|
||||
export function applyAuthorization(inOptions: any): any {
|
||||
const options = { ...inOptions };
|
||||
if (options.auth || options.headers?.authorization) {
|
||||
return options;
|
||||
}
|
||||
if (options.token) {
|
||||
if (
|
||||
options.hostType === PLATFORM_TYPE_GITHUB ||
|
||||
options.hostType === PLATFORM_TYPE_GITEA
|
||||
) {
|
||||
options.headers.authorization = `token ${options.token}`;
|
||||
} else if (options.hostType === PLATFORM_TYPE_GITLAB) {
|
||||
options.headers['Private-token'] = options.token;
|
||||
} else {
|
||||
options.headers.authorization = `Bearer ${options.token}`;
|
||||
}
|
||||
delete options.token;
|
||||
}
|
||||
return options;
|
||||
}
|
|
@ -4,6 +4,7 @@ import { GotPromise } from 'got';
|
|||
import * as runCache from '../cache/run';
|
||||
import { clone } from '../clone';
|
||||
import got from '../got';
|
||||
import { applyAuthorization } from './auth';
|
||||
import { applyHostRules } from './host-rules';
|
||||
|
||||
interface OutgoingHttpHeaders {
|
||||
|
@ -90,6 +91,7 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
|
|||
};
|
||||
|
||||
options = applyHostRules(url, options);
|
||||
options = applyAuthorization(options);
|
||||
|
||||
// Cache GET requests unless useCache=false
|
||||
let promisedRes: GotPromise<any>;
|
||||
|
|
Loading…
Reference in a new issue