refactor: move auth to http module (#6502)

This commit is contained in:
Rhys Arkins 2020-06-13 06:17:13 +02:00 committed by GitHub
parent 9691122d9b
commit 6c38eb359a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 42 deletions

View file

@ -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);
},
});

View file

@ -1,14 +1,15 @@
import got from 'got'; import got from 'got';
import auth from './auth'; import { create, mergeInstances } from './util';
import { mergeInstances } from './util';
export * from './common'; export * from './common';
/* const dummy = create({
* This is the default got instance for Renovate. options: {},
* - Cache all GET requests for the lifetime of the repo handler: (options, next) => {
* return next(options);
*/ },
export const api = mergeInstances(got, auth); });
export const api = mergeInstances(got, dummy);
export default api; export default api;

26
lib/util/http/auth.ts Normal file
View 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;
}

View file

@ -4,6 +4,7 @@ import { GotPromise } from 'got';
import * as runCache from '../cache/run'; import * as runCache from '../cache/run';
import { clone } from '../clone'; import { clone } from '../clone';
import got from '../got'; import got from '../got';
import { applyAuthorization } from './auth';
import { applyHostRules } from './host-rules'; import { applyHostRules } from './host-rules';
interface OutgoingHttpHeaders { interface OutgoingHttpHeaders {
@ -90,6 +91,7 @@ export class Http<GetOptions = HttpOptions, PostOptions = HttpPostOptions> {
}; };
options = applyHostRules(url, options); options = applyHostRules(url, options);
options = applyAuthorization(options);
// Cache GET requests unless useCache=false // Cache GET requests unless useCache=false
let promisedRes: GotPromise<any>; let promisedRes: GotPromise<any>;