@@ -10,11 +10,10 @@ const url = require('url')
1010
1111const 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 */
1918const 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 } ×tamp=${ timestamp } `
56+ }
57+
3858module . exports = {
59+ createAuthorizationQuery,
3960 setAuthorization
4061}
0 commit comments