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

Commit fda40bc

Browse files
Allow wildcard and multi domain authHost option
As described in #185 as well as #135: - allow for wildcard domain declaration in authHost option (e.g. authHost = '.myapp.com') so one can use the laravel-echo-server with a multi tenant/subdomain system - as well as a multi domain system, where different authentication servers are used. The currently appropriate authentication domain is retrived from the referer and matched with the (optional: wildcard) domain declaration from the authHost option as a whitelist (which can be either an array or a string). In case none is matched with the referer, the last one in the array is taken (in case of string, the only one). @tlaverdure @barryvdh please check. I have done my best to write this up in TS, but I don't have a compile system for this setup, so I have only tested it in vanilla JS and then afterwards added the TS specific types for this PR. Thanks!
1 parent dea0d33 commit fda40bc

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/channels/private-channel.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ export class PrivateChannel {
2626
* @return {Promise<any>}
2727
*/
2828
authenticate(socket: any, data: any): Promise<any> {
29-
var authHost = this.authHost();
30-
var referer = url.parse(socket.request.headers.referer);
31-
32-
if(referer.hostname.substr(referer.hostname.indexOf('.'))===authHost)
33-
authHost = referer.protocol+"//"+referer.host;
34-
3529
let options = {
36-
url: authHost + this.options.authEndpoint,
30+
url: this.authHost(socket) + this.options.authEndpoint,
3731
form: { channel_name: data.channel },
3832
headers: (data.auth && data.auth.headers) ? data.auth.headers : {},
3933
rejectUnauthorized: false
@@ -47,9 +41,26 @@ export class PrivateChannel {
4741
*
4842
* @return {string}
4943
*/
50-
protected authHost(): string {
51-
return (this.options.authHost) ?
44+
protected authHost(socket: any): string {
45+
let referer: Object = url.parse(socket.request.headers.referer);
46+
let authHostSelected: string = 'http://localhost';
47+
let authHosts: any = (this.options.authHost) ?
5248
this.options.authHost : this.options.host;
49+
50+
if(typeof authHosts === "string")
51+
authHosts = [authHosts];
52+
53+
for(let authHost of authHosts)
54+
{
55+
authHostSelected = authHost;
56+
if(referer.hostname.substr(referer.hostname.indexOf('.')) === authHostSelected || referer.protocol + "//" + referer.host === authHostSelected || referer.host === authHostSelected)
57+
{
58+
authHostSelected = referer.protocol+"//"+referer.host;
59+
break;
60+
}
61+
}
62+
63+
return authHostSelected;
5364
}
5465

5566
/**

0 commit comments

Comments
 (0)