Skip to content

Commit 89c457d

Browse files
Merge pull request #1 from 4success/master
adding custom authorizer
2 parents 15d46dc + a21d477 commit 89c457d

File tree

2 files changed

+5299
-5363
lines changed

2 files changed

+5299
-5363
lines changed

js-src/Websocket.ts

Lines changed: 98 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
import {Channel} from "./Channel";
22
import {AxiosResponse} from "axios";
33

4-
export type Options = { authEndpoint: string, host: string };
4+
export type ChannelAuthorizationCallback = (
5+
error: Error | null,
6+
authData: any | null
7+
) => void;
8+
9+
export interface DeprecatedChannelAuthorizer {
10+
authorize(socketId: string, channelName: string, callback: ChannelAuthorizationCallback): void;
11+
}
12+
13+
export interface ChannelAuthorizerGenerator {
14+
(
15+
channel: Channel,
16+
options: DeprecatedAuthorizerOptions
17+
): DeprecatedChannelAuthorizer;
18+
}
19+
20+
export interface DeprecatedAuthOptions {
21+
params?: any;
22+
headers?: any;
23+
}
24+
25+
export interface DeprecatedAuthorizerOptions {
26+
authTransport: 'ajax' | 'jsonp';
27+
authEndpoint: string;
28+
auth?: DeprecatedAuthOptions;
29+
}
30+
31+
export type Options = { authEndpoint: string, host: string, authorizer?: DeprecatedChannelAuthorizer };
32+
533
export type MessageBody = { event: string, channel?: string, data: object };
634

735
export class Websocket {
@@ -58,7 +86,7 @@ export class Websocket {
5886
}
5987
}
6088

61-
this.on('whoami', ({ socket_id: socketId }) => {
89+
this.on('whoami', ({socket_id: socketId}) => {
6290
this.socketId = socketId
6391

6492
console.log(`just set socketId to ${socketId}`)
@@ -88,24 +116,10 @@ export class Websocket {
88116
return this
89117
}
90118

91-
protected parseMessage(body: string): MessageBody {
92-
try {
93-
return JSON.parse(body)
94-
} catch (error) {
95-
console.error(error)
96-
97-
return undefined
98-
}
99-
}
100-
101119
getSocketId(): string {
102120
return this.socketId
103121
}
104122

105-
private socketIsReady(): boolean {
106-
return this.websocket.readyState === this.websocket.OPEN
107-
}
108-
109123
send(message: object): void {
110124
if (this.socketIsReady()) {
111125
this.websocket.send(JSON.stringify(message))
@@ -115,7 +129,7 @@ export class Websocket {
115129
this.buffer.push(message)
116130
}
117131

118-
close (): void {
132+
close(): void {
119133
this.internalListeners = {}
120134

121135
clearInterval(this.pingInterval)
@@ -132,39 +146,6 @@ export class Websocket {
132146
}
133147
}
134148

135-
private actuallySubscribe(channel: Channel): void {
136-
if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
137-
console.log(`Sending auth request for channel ${channel.name}`)
138-
139-
axios.post(this.options.authEndpoint, {
140-
socket_id: this.getSocketId(),
141-
channel_name: channel.name,
142-
}).then((response: AxiosResponse) => {
143-
console.log(`Subscribing to channels ${channel.name}`)
144-
145-
this.send({
146-
event: 'subscribe',
147-
data: {
148-
channel: channel.name,
149-
... response.data
150-
},
151-
})
152-
}).catch((error) => {
153-
console.log(`Auth request for channel ${channel.name} failed`)
154-
console.error(error)
155-
})
156-
} else {
157-
console.log(`Subscribing to channels ${channel.name}`)
158-
159-
this.send({
160-
event: 'subscribe',
161-
data: {
162-
channel: channel.name,
163-
},
164-
})
165-
}
166-
}
167-
168149
unsubscribe(channel: Channel): void {
169150
this.send({
170151
event: 'unsubscribe',
@@ -195,4 +176,71 @@ export class Websocket {
195176
delete this.internalListeners[event]
196177
}
197178
}
179+
180+
protected parseMessage(body: string): MessageBody {
181+
try {
182+
return JSON.parse(body)
183+
} catch (error) {
184+
console.error(error)
185+
186+
return undefined
187+
}
188+
}
189+
190+
private socketIsReady(): boolean {
191+
return this.websocket.readyState === this.websocket.OPEN
192+
}
193+
194+
private actuallySubscribe(channel: Channel): void {
195+
if (channel.name.startsWith('private-') || channel.name.startsWith('presence-')) {
196+
console.log(`Sending auth request for channel ${channel.name}`)
197+
198+
if (this.options.authorizer) {
199+
this.options.authorizer.authorize(this.getSocketId(), channel.name, (error, authData) => {
200+
if (!error) {
201+
console.log(`Subscribing to channels ${channel.name}`)
202+
203+
this.send({
204+
event: 'subscribe',
205+
data: {
206+
channel: channel.name,
207+
...authData
208+
},
209+
})
210+
} else {
211+
console.log(`Auth request for channel ${channel.name} failed`)
212+
console.error(error)
213+
}
214+
})
215+
} else {
216+
axios.post(this.options.authEndpoint, {
217+
socket_id: this.getSocketId(),
218+
channel_name: channel.name,
219+
}).then((response: AxiosResponse) => {
220+
console.log(`Subscribing to channels ${channel.name}`)
221+
222+
this.send({
223+
event: 'subscribe',
224+
data: {
225+
channel: channel.name,
226+
...response.data
227+
},
228+
})
229+
}).catch((error) => {
230+
console.log(`Auth request for channel ${channel.name} failed`)
231+
console.error(error)
232+
})
233+
}
234+
235+
} else {
236+
console.log(`Subscribing to channels ${channel.name}`)
237+
238+
this.send({
239+
event: 'subscribe',
240+
data: {
241+
channel: channel.name,
242+
},
243+
})
244+
}
245+
}
198246
}

0 commit comments

Comments
 (0)