<commit_before>import {Injectable} from "@angular/core";
import {Http, ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response, Headers} from "@angular/http";
import {Observable} from "rxjs";
import {Auth} from "./security.auth";
export const JWT_RESPONSE_HEADER = 'X-Auth-Token';

@Injectable()
export class AuthHttpService extends Http {
	constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
		super(backend, defaultOptions);
	}

	request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.request(url, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	get(url: string, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.get(url, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.post(url, body, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.put(url, body, this.appendAuthHeader(options));
		request.subscribe(this.saveToken);
		return request;
	}

	delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.delete(url, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	private appendAuthHeader(options?: RequestOptionsArgs): RequestOptionsArgs {
		let mergedOptions: RequestOptionsArgs;
		if (!options) {
			mergedOptions = { };
		} else {
			mergedOptions = options;
		}

		if (!mergedOptions.headers)
			mergedOptions.headers = new Headers();

		if (mergedOptions.withCredentials == null)
			mergedOptions.withCredentials = true;

		var authz = Auth.factory().getAuthz();
		if (authz != null && authz.token) {
				const token = authz.token;
				if (token) mergedOptions.headers.append('Authorization', `Bearer ${token}`);
		}
		return mergedOptions;
	}

	private saveToken(res: Response): void {
		const token = res.headers.get(JWT_RESPONSE_HEADER);
		if (token) localStorage.setItem(JWT_RESPONSE_HEADER, token);
	}
}<commit_msg>No longer adds authorization header if withCredentials is set to false.<commit_after>import {Injectable} from "@angular/core";
import {Http, ConnectionBackend, RequestOptions, Request, RequestOptionsArgs, Response, Headers} from "@angular/http";
import {Observable} from "rxjs";
import {Auth} from "./security.auth";
export const JWT_RESPONSE_HEADER = 'X-Auth-Token';

@Injectable()
export class AuthHttpService extends Http {
	constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
		super(backend, defaultOptions);
	}

	request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.request(url, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	get(url: string, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.get(url, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.post(url, body, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.put(url, body, this.appendAuthHeader(options));
		request.subscribe(this.saveToken);
		return request;
	}

	delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
		const request = super.delete(url, this.appendAuthHeader(options));
		request.map(this.saveToken);
		return request;
	}

	private appendAuthHeader(options?: RequestOptionsArgs): RequestOptionsArgs {
		let mergedOptions: RequestOptionsArgs;
		if (!options) {
			mergedOptions = { };
		} else {
			mergedOptions = options;
		}

		if (!mergedOptions.headers)
			mergedOptions.headers = new Headers();

		if (mergedOptions.withCredentials == null)
			mergedOptions.withCredentials = true;

		if (mergedOptions.withCredentials) {
			var authz = Auth.factory().getAuthz();
			if (authz != null && authz.token) {
				const token = authz.token;
				if (token) mergedOptions.headers.append('Authorization', `Bearer ${token}`);
			}
		}
		return mergedOptions;
	}

	private saveToken(res: Response): void {
		const token = res.headers.get(JWT_RESPONSE_HEADER);
		if (token) localStorage.setItem(JWT_RESPONSE_HEADER, token);
	}
}
{'fraction_non_alphanumeric': 0.1266261925411969, 'fraction_numerical': 0.0, 'mean_word_length': 5.5339943342776206, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6348490', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1370, 'n_words': 324}