-
Notifications
You must be signed in to change notification settings - Fork 372
/
channel_authorizer.ts
64 lines (54 loc) · 1.5 KB
/
channel_authorizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import {
AuthRequestType,
InternalAuthOptions,
ChannelAuthorizationHandler,
ChannelAuthorizationRequestParams,
ChannelAuthorizationCallback
} from './options';
import Runtime from 'runtime';
const composeChannelQuery = (
params: ChannelAuthorizationRequestParams,
authOptions: InternalAuthOptions
) => {
var query = 'socket_id=' + encodeURIComponent(params.socketId);
query += '&channel_name=' + encodeURIComponent(params.channelName);
for (var i in authOptions.params) {
query +=
'&' +
encodeURIComponent(i) +
'=' +
encodeURIComponent(authOptions.params[i]);
}
if (authOptions.paramProvider != null) {
let dynamicParams = authOptions.paramProvider();
for (var i in dynamicParams) {
query +=
'&' +
encodeURIComponent(i) +
'=' +
encodeURIComponent(dynamicParams[i]);
}
}
return query;
};
const ChannelAuthorizer = (
authOptions: InternalAuthOptions
): ChannelAuthorizationHandler => {
if (typeof Runtime.getAuthorizers()[authOptions.transport] === 'undefined') {
throw `'${authOptions.transport}' is not a recognized auth transport`;
}
return (
params: ChannelAuthorizationRequestParams,
callback: ChannelAuthorizationCallback
) => {
const query = composeChannelQuery(params, authOptions);
Runtime.getAuthorizers()[authOptions.transport](
Runtime,
query,
authOptions,
AuthRequestType.ChannelAuthorization,
callback
);
};
};
export default ChannelAuthorizer;