Skip to content

Commit

Permalink
Add allowCrossOriginAuthentication boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Apr 1, 2020
1 parent eca7d5d commit 31e7e62
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
12 changes: 8 additions & 4 deletions lib/handlers/basiccreds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import ifm = require('../Interfaces');
export class BasicCredentialHandler implements ifm.IRequestHandler {
username: string;
password: string;
host: string;
allowCrossOriginAuthentication: boolean;
origin: string;

constructor(username: string, password: string) {
constructor(username: string, password: string, allowCrossOriginAuthentication?: boolean) {
this.username = username;
this.password = password;
this.allowCrossOriginAuthentication = allowCrossOriginAuthentication;
}

// currently implements pre-authorization
// TODO: support preAuth = false where it hooks on 401
prepareRequest(options:any): void {
if (!this.origin) {
this.origin = options.host;
}
// If this is a redirection, don't set the Authorization header
if (!this.host || this.host === options.host) {
if (this.origin === options.host || this.allowCrossOriginAuthentication) {
options.headers['Authorization'] = `Basic ${Buffer.from(`${this.username}:${this.password}`).toString('base64')}`;
this.host = options.host;
}
options.headers['X-TFS-FedAuthRedirect'] = 'Suppress';
}
Expand Down
12 changes: 8 additions & 4 deletions lib/handlers/bearertoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import ifm = require('../Interfaces');

export class BearerCredentialHandler implements ifm.IRequestHandler {
token: string;
host: string;
allowCrossOriginAuthentication: boolean;
origin: string;

constructor(token: string) {
constructor(token: string, allowCrossOriginAuthentication?: boolean) {
this.token = token;
this.allowCrossOriginAuthentication = allowCrossOriginAuthentication;
}

// currently implements pre-authorization
// TODO: support preAuth = false where it hooks on 401
prepareRequest(options:any): void {
if (!this.origin) {
this.origin = options.host;
}
// If this is a redirection, don't set the Authorization header
if (!this.host || this.host === options.host) {
if (this.origin === options.host || this.allowCrossOriginAuthentication) {
options.headers['Authorization'] = `Bearer ${this.token}`;
this.host = options.host;
}
options.headers['X-TFS-FedAuthRedirect'] = 'Suppress';
}
Expand Down
12 changes: 8 additions & 4 deletions lib/handlers/personalaccesstoken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import ifm = require('../Interfaces');

export class PersonalAccessTokenCredentialHandler implements ifm.IRequestHandler {
token: string;
host: string;
allowCrossOriginAuthentication: boolean;
origin: string;

constructor(token: string) {
constructor(token: string, allowCrossOriginAuthentication?: boolean) {
this.token = token;
this.allowCrossOriginAuthentication = allowCrossOriginAuthentication;
}

// currently implements pre-authorization
// TODO: support preAuth = false where it hooks on 401
prepareRequest(options:any): void {
if (!this.origin) {
this.origin = options.host;
}
// If this is a redirection, don't set the Authorization header
if (!this.host || this.host === options.host) {
if (this.origin === options.host || this.allowCrossOriginAuthentication) {
options.headers['Authorization'] = `Basic ${Buffer.from(`PAT:${this.token}`).toString('base64')}`;
this.host = options.host;
}
options.headers['X-TFS-FedAuthRedirect'] = 'Suppress';
}
Expand Down

0 comments on commit 31e7e62

Please sign in to comment.