Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow wildcard and multi domain authHost option #190

Merged
merged 2 commits into from
Jul 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/channels/private-channel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var request = require('request');
import { Channel } from './channel';
import { Log } from './../log';
var url = require('url');

export class PrivateChannel {
/**
Expand All @@ -26,7 +27,7 @@ export class PrivateChannel {
*/
authenticate(socket: any, data: any): Promise<any> {
let options = {
url: this.authHost() + this.options.authEndpoint,
url: this.authHost(socket) + this.options.authEndpoint,
form: { channel_name: data.channel },
headers: (data.auth && data.auth.headers) ? data.auth.headers : {},
rejectUnauthorized: false
Expand All @@ -40,9 +41,26 @@ export class PrivateChannel {
*
* @return {string}
*/
protected authHost(): string {
return (this.options.authHost) ?
protected authHost(socket: any): string {
let referer: Object = url.parse(socket.request.headers.referer);
let authHostSelected: string = 'http://localhost';
let authHosts: any = (this.options.authHost) ?
this.options.authHost : this.options.host;

if(typeof authHosts === "string")
authHosts = [authHosts];

for(let authHost of authHosts)
{
authHostSelected = authHost;
if(referer.hostname.substr(referer.hostname.indexOf('.')) === authHostSelected || referer.protocol + "//" + referer.host === authHostSelected || referer.host === authHostSelected)
{
authHostSelected = referer.protocol+"//"+referer.host;
break;
}
}

return authHostSelected;
}

/**
Expand Down