Skip to content

Commit 9303562

Browse files
auth: add socket authorization functionality.
- add `createSocketAuthorizationString` function - add `createAuthorizationQuery` function
1 parent 013d77d commit 9303562

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

lib/auth.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ const url = require('url')
1010

1111
const AuthUtil = require('./util/auth')
1212

13+
const { SOCKET_ORIGINAL_URL } = require('./config')
14+
1315
/**
14-
* Set Authorization.
15-
* @param {Object} options
16-
* @param {string} signKey
17-
* @param {number} clientId
16+
* Set Authorization for api requests.
1817
*/
1918
const setAuthorization = (options, signKey, clientId) => {
2019
const { path, query } = url.parse(options.uri)
@@ -35,6 +34,28 @@ const setAuthorization = (options, signKey, clientId) => {
3534
options.headers = Object.assign({}, options.headers, headers)
3635
}
3736

37+
/**
38+
* Builds authorization string for socket connection.
39+
* @return {String}
40+
*/
41+
const createSocketAuthorizationString = (clientId, signKey, timestamp) => AuthUtil.createSocketAuthHash({
42+
clientId,
43+
timestamp,
44+
originalUrl: SOCKET_ORIGINAL_URL
45+
}, signKey)
46+
47+
/**
48+
* Creates authorization query based on auth, clientId and timestamp.
49+
* @return {String}
50+
*/
51+
const createAuthorizationQuery = (clientId, signKey) => {
52+
const timestamp = Date.now()
53+
const auth = createSocketAuthorizationString(clientId, signKey, timestamp)
54+
55+
return `authorization=${auth}&user_id=${clientId}&timestamp=${timestamp}`
56+
}
57+
3858
module.exports = {
59+
createAuthorizationQuery,
3960
setAuthorization
4061
}

0 commit comments

Comments
 (0)