Skip to content

Commit

Permalink
refactor: move auth to http module (#6502)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jun 13, 2020
1 parent 9691122 commit 6c38eb3
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 42 deletions.
34 changes: 0 additions & 34 deletions lib/util/got/auth.ts

This file was deleted.

17 changes: 9 additions & 8 deletions lib/util/got/index.ts
@@ -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 changes: 26 additions & 0 deletions lib/util/http/auth.ts
@@ -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;
}
2 changes: 2 additions & 0 deletions lib/util/http/index.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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>;
Expand Down

0 comments on commit 6c38eb3

Please sign in to comment.