Skip to content

Commit

Permalink
feat: implement native auth
Browse files Browse the repository at this point in the history
Fixes jdalrymple#1098

These are the issues I encountered - please help:

- [ ] fix TODOs
- [ ] lint & fix (for me, there were a lot of unrelated changes after
running lint:fix, so I didn't.)
- [ ] verify this works because I couldn't test it because of jdalrymple#1105
  • Loading branch information
Kipras Melnikovas authored and kiprasmel committed Feb 5, 2021
1 parent 49660c5 commit bdb5978
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
34 changes: 34 additions & 0 deletions packages/gitbeaker-requester-utils/src/BaseService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { RequesterType, DefaultServiceOptions } from './RequesterUtils';

export interface NativeAuth {
gitlabSessionCookieKey?: string;
gitlabSessionCookieValue: string;
gitlabCSRFTokenKey?: string;
gitlabCSRFTokenValue: string;
}

export interface BaseServiceOptions {
oauthToken?: string;
token?: string;
jobToken?: string;
nativeAuth?: NativeAuth;
host?: string;
prefixUrl?: string;
version?: 3 | 4;
Expand All @@ -29,10 +37,18 @@ export class BaseService {

public readonly rejectUnauthorized: boolean;

public readonly additionalBody: FormData | object;

constructor({
token,
jobToken,
oauthToken,
nativeAuth = {
gitlabSessionCookieKey: '_gitlab_session',
gitlabSessionCookieValue: '',
gitlabCSRFTokenKey: 'authenticity_token',
gitlabCSRFTokenValue: '',
},
sudo,
profileToken,
requesterFn,
Expand All @@ -54,12 +70,30 @@ export class BaseService {
this.rejectUnauthorized = rejectUnauthorized;
this.camelize = camelize;
this.requestTimeout = requestTimeout;
this.additionalBody = {};

// Handle auth tokens
if (oauthToken) this.headers.authorization = `Bearer ${oauthToken}`;
else if (jobToken) this.headers['job-token'] = jobToken;
else if (token) this.headers['private-token'] = token;

else if (nativeAuth.gitlabSessionCookieValue && nativeAuth.gitlabCSRFTokenValue) {
const {
gitlabSessionCookieKey,
gitlabSessionCookieValue,
gitlabCSRFTokenKey,
gitlabCSRFTokenValue,
} = nativeAuth;

if (!this.headers.cookie) {
this.headers.cookie = 'cookie: ';
}

this.headers.cookie += `${gitlabSessionCookieKey}=${gitlabSessionCookieValue}; `;

this.additionalBody = {...this.additionalBody, [gitlabCSRFTokenKey]: gitlabCSRFTokenValue}
}

// Profiling
if (profileToken) {
this.headers['X-Profile-Token'] = profileToken;
Expand Down
6 changes: 4 additions & 2 deletions packages/gitbeaker-requester-utils/src/RequesterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type DefaultServiceOptions = {
requestTimeout: number;
url: string;
rejectUnauthorized: boolean;
additionalBody?: FormData | object;
};

export type DefaultRequestOptions = {
Expand Down Expand Up @@ -51,16 +52,17 @@ export function defaultOptionsHandler(
serviceOptions: DefaultServiceOptions,
{ body, query, sudo, method = 'get' }: DefaultRequestOptions = {},
): DefaultRequestReturn {
const { headers, requestTimeout, url } = serviceOptions;
const { headers, requestTimeout, url, additionalBody = {} } = serviceOptions;
let bod: FormData | string;

if (sudo) headers.sudo = sudo;

// FIXME: Not the best comparison, but...it will have to do for now.
if (typeof body === 'object' && body.constructor.name !== 'FormData') {
bod = JSON.stringify(decamelizeKeys(body));
bod = JSON.stringify(decamelizeKeys({ ...body, ...additionalBody }));
headers['content-type'] = 'application/json';
} else {
/** TODO - what do I do here with the additionalBody? */
bod = body as FormData;
}

Expand Down

0 comments on commit bdb5978

Please sign in to comment.