Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
vernemq
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
stack
vernemq
Commits
8b9e2a85
Commit
8b9e2a85
authored
Sep 19, 2018
by
Chavee Issariyapat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rewrite some functions
parent
f78abcd2
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
132 additions
and
38 deletions
+132
-38
authhook/auth_on_publish.js
+9
-4
authhook/auth_on_subscribe.js
+10
-4
authhook/index.js
+3
-3
authhook/on_deliver.js
+11
-9
authhook/on_unsubscribe.js
+1
-1
authhook/utils/getTopic.js
+61
-17
authhook/utils/modify.js
+37
-0
No files found.
authhook/auth_on_publish.js
View file @
8b9e2a85
...
@@ -26,8 +26,6 @@ module.exports = function (options = {}) {
...
@@ -26,8 +26,6 @@ module.exports = function (options = {}) {
var
cachekey
=
'pub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
cachekey
=
'pub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
var
authstatus
=
cache
.
get
(
cachekey
);
console
.
log
(
req
.
body
)
// checkRole
// checkRole
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
...
@@ -35,7 +33,9 @@ module.exports = function (options = {}) {
...
@@ -35,7 +33,9 @@ module.exports = function (options = {}) {
// Set Response
// Set Response
var
isShadow
=
require
(
'./utils/getTopic'
).
isShadow
var
isShadow
=
require
(
'./utils/getTopic'
).
isShadow
if
(
isShadow
(
topic
))
{
if
(
isShadow
(
topic
))
{
var
_ftopic
=
require
(
'./utils/getTopic'
).
getTopic
(
topic
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'pub'
,
_id
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
response
=
{
var
response
=
{
'result'
:
'ok'
,
'result'
:
'ok'
,
'modifiers'
:
{
'modifiers'
:
{
...
@@ -57,12 +57,17 @@ module.exports = function (options = {}) {
...
@@ -57,12 +57,17 @@ module.exports = function (options = {}) {
res
.
send
(
response
);
res
.
send
(
response
);
}
}
next
();
next
();
})
}
}
else
{
else
{
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
// console.log(_id)
// console.log(_id)
var
_ftopic
=
require
(
'./utils/getTopic'
).
getTopic
(
topic
,
_id
)
// get topic where concat with groupID
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'pub'
,
_id
,
req
.
body
.
client
_id
)
// get topic where concat with groupID
var
response
=
{
var
response
=
{
'result'
:
'ok'
,
'result'
:
'ok'
,
'modifiers'
:
{
'modifiers'
:
{
...
...
authhook/auth_on_subscribe.js
View file @
8b9e2a85
...
@@ -31,8 +31,6 @@ module.exports = function (options = {}) {
...
@@ -31,8 +31,6 @@ module.exports = function (options = {}) {
var
cachekey
=
'sub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
cachekey
=
'sub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
var
authstatus
=
cache
.
get
(
cachekey
);
console
.
log
(
req
.
body
)
// checkRole
// checkRole
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
...
@@ -40,7 +38,11 @@ module.exports = function (options = {}) {
...
@@ -40,7 +38,11 @@ module.exports = function (options = {}) {
// Set Response
// Set Response
var
isShadow
=
require
(
'./utils/getTopic'
).
isShadow
var
isShadow
=
require
(
'./utils/getTopic'
).
isShadow
if
(
isShadow
(
topic
))
{
if
(
isShadow
(
topic
))
{
var
_ftopic
=
require
(
'./utils/getTopic'
).
getTopic
(
topic
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'sub'
,
_id
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
_topic
=
[{
// setTopic for response
var
_topic
=
[{
// setTopic for response
'topic'
:
_ftopic
,
'topic'
:
_ftopic
,
'qos'
:
0
'qos'
:
0
...
@@ -62,12 +64,14 @@ module.exports = function (options = {}) {
...
@@ -62,12 +64,14 @@ module.exports = function (options = {}) {
res
.
send
(
response
);
res
.
send
(
response
);
}
}
next
();
next
();
})
}
}
else
{
else
{
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
// console.log(_id)
// console.log(_id)
var
_ftopic
=
require
(
'./utils/getTopic'
).
getTopic
(
topic
,
_id
)
// get topic where concat with groupID
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'sub'
,
_id
,
req
.
body
.
client
_id
)
// get topic where concat with groupID
var
_topic
=
[{
// setTopic for response
var
_topic
=
[{
// setTopic for response
'topic'
:
_ftopic
,
'topic'
:
_ftopic
,
'qos'
:
0
'qos'
:
0
...
@@ -93,7 +97,9 @@ module.exports = function (options = {}) {
...
@@ -93,7 +97,9 @@ module.exports = function (options = {}) {
}
}
}
}
else
{
else
{
console
.
log
(
'>>>>>>>>>>>>>>>>>>>'
)
console
.
log
(
decoded
)
console
.
log
(
decoded
)
console
.
log
(
'>>>>>>>>>>>>>>>>>>>'
)
if
(
decoded
.
role
===
'realtimedb'
)
{
if
(
decoded
.
role
===
'realtimedb'
)
{
var
response
=
{
var
response
=
{
'result'
:
'ok'
'result'
:
'ok'
...
...
authhook/index.js
View file @
8b9e2a85
...
@@ -2,9 +2,9 @@ process.env["NODE_ENV"] = "development";
...
@@ -2,9 +2,9 @@ process.env["NODE_ENV"] = "development";
process
.
env
[
"NODE_CONFIG_DIR"
]
=
__dirname
+
"/config/"
;
process
.
env
[
"NODE_CONFIG_DIR"
]
=
__dirname
+
"/config/"
;
var
auth_on_register_debug
=
process
.
env
[
"AUTH_ON_REGISTER_DEBUG"
]
||
false
;
var
auth_on_register_debug
=
process
.
env
[
"AUTH_ON_REGISTER_DEBUG"
]
||
false
;
var
auth_on_publish_debug
=
process
.
env
[
"AUTH_ON_PUBLISH_DEBUG"
]
||
fals
e
;
var
auth_on_publish_debug
=
process
.
env
[
"AUTH_ON_PUBLISH_DEBUG"
]
||
tru
e
;
var
auth_on_subscribe_debug
=
process
.
env
[
"AUTH_ON_SUBSCRIBE_DEBUG"
]
||
fals
e
;
var
auth_on_subscribe_debug
=
process
.
env
[
"AUTH_ON_SUBSCRIBE_DEBUG"
]
||
tru
e
;
var
on_publish_debug
=
process
.
env
[
"ON_PUBLISH_DEBUG"
]
||
fals
e
;
var
on_publish_debug
=
process
.
env
[
"ON_PUBLISH_DEBUG"
]
||
tru
e
;
var
restify
=
require
(
'restify'
);
var
restify
=
require
(
'restify'
);
var
auth_on_register
=
require
(
'./auth_on_register'
)({
debug
:
auth_on_register_debug
});
var
auth_on_register
=
require
(
'./auth_on_register'
)({
debug
:
auth_on_register_debug
});
...
...
authhook/on_deliver.js
View file @
8b9e2a85
var
modify
=
require
(
'./utils/modify'
);
function
on_deliver
(
req
,
res
,
next
)
{
function
on_deliver
(
req
,
res
,
next
)
{
var
topics
=
beDesireTopic
(
req
.
body
.
topic
)
console
.
log
(
'----on_deliver-----'
)
console
.
log
(
'----on_deliver-----'
)
var
topics
=
modify
.
removeTempWordInTopic
(
req
.
body
.
topic
)
console
.
log
(
req
.
body
)
console
.
log
(
req
.
body
)
console
.
log
(
topics
)
//
console.log(topics)
var
response
=
{
var
response
=
{
'result'
:
'ok'
,
'result'
:
'ok'
,
'modifiers'
:
{
'modifiers'
:
{
"topic"
:
topics
"topic"
:
topics
}
}
}
}
console
.
log
(
response
);
res
.
send
(
response
)
res
.
send
(
response
)
}
}
module
.
exports
.
on_deliver
=
on_deliver
module
.
exports
.
on_deliver
=
on_deliver
...
@@ -21,13 +24,12 @@ function hasGroup(topics) {
...
@@ -21,13 +24,12 @@ function hasGroup(topics) {
}
}
module
.
exports
.
hasGroup
=
hasGroup
module
.
exports
.
hasGroup
=
hasGroup
function
beDesireTopic
(
topics
)
{
// function beDesireTopic(topics) {
if
(
hasGroup
(
topics
))
{
// if (hasGroup(topics)) {
return
joinTopic
(
topics
)
// return joinTopic(topics)
}
// }
else
return
topics
// else return topics
}
// }
module
.
exports
.
beDesireTopic
=
beDesireTopic
function
joinTopic
(
topics
)
{
function
joinTopic
(
topics
)
{
var
topic
=
topics
.
split
(
'/!'
)[
1
]
var
topic
=
topics
.
split
(
'/!'
)[
1
]
...
...
authhook/on_unsubscribe.js
View file @
8b9e2a85
...
@@ -10,7 +10,7 @@ function on_unsubscribe(req, res, next) {
...
@@ -10,7 +10,7 @@ function on_unsubscribe(req, res, next) {
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
// console.log(_id)
// console.log(_id)
var
_ftopic
=
require
(
'./utils/getTopic'
).
getTopic
(
topic
,
_id
)
// get topic where concat with groupID
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'unsub'
,
_id
,
req
.
body
.
client
_id
)
// get topic where concat with groupID
var
response
=
{
var
response
=
{
"result"
:
"ok"
,
"result"
:
"ok"
,
"topics"
:
[
_ftopic
]
"topics"
:
[
_ftopic
]
...
...
authhook/utils/getTopic.js
View file @
8b9e2a85
function
getTopic
(
topic
,
id
)
{
// function getTopic(topic, id) {
var
_topic
=
classifiedTopic
(
topic
)
+
'/!'
+
id
+
topic
.
split
(
classifiedTopic
(
topic
))[
1
]
// //var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1]
return
_topic
// var _topic = topicPrefix(topic) + id + topic.split(topicPrefix(topic))[1]
}
// return _topic
module
.
exports
.
getTopic
=
getTopic
// }
// module.exports.getTopic = getTopic
function
classifiedTopic
(
topic
)
{
if
(
topic
.
indexOf
(
'@msg'
)
!==
-
1
)
{
// function topicPrefix(topic) {
return
'@msg'
// if (topic.indexOf('@msg') !== -1) {
}
else
if
(
topic
.
indexOf
(
'@shadow/updated'
)
!==
-
1
)
{
// return '@msg/!'
return
'@shadow/updated'
// } else if (topic.indexOf('@shadow/updated') !== -1) {
}
else
if
(
topic
.
indexOf
(
'@shadow/update'
)
!==
-
1
)
{
// return '@shadow/updated/!'
return
'@shadow/update'
// } else if (topic.indexOf('@shadow/update') !== -1) {
}
else
if
(
topic
.
indexOf
(
'@shadow/get'
)
!==
-
1
)
{
// return '@shadow/update/'
return
'@shadow/get'
// } else if (topic.indexOf('@shadow/get') !== -1) {
}
else
if
(
topic
.
indexOf
(
'@client'
)
!==
-
1
)
{
// return '@shadow/get/!'
return
'@client'
// } else if (topic.indexOf('@client') !== -1) {
// return '@client/'
// }
// }
var
modify
=
require
(
'./modify'
);
function
rewriteTopic
(
topic
,
op
,
groupid
,
clientid
)
{
var
chunk
=
topic
.
split
(
'/'
,
2
);
switch
(
chunk
[
0
])
{
case
'@msg'
:
return
modify
.
insertWordIntoTopic
(
topic
,
'!'
+
clientid
,
1
);
case
'@shadow'
:
if
(
chunk
[
1
]
==
'write'
)
return
modify
.
insertWordIntoTopic
(
topic
,
''
+
clientid
,
2
);
else
if
(
chunk
[
1
]
==
'set'
)
return
modify
.
insertWordIntoTopic
(
topic
,
''
+
groupid
,
2
);
else
if
(
chunk
[
1
]
==
'changed'
)
{
if
(
op
==
'pub'
)
{
if
(
chunk
.
length
>
2
)
return
modify
.
insertWordIntoTopic
(
topic
,
'!'
+
groupid
,
2
);
else
if
(
chunk
.
length
==
2
)
return
modify
.
insertWordIntoTopic
(
topic
,
'!'
+
clientid
,
2
);
else
return
""
;
}
else
{
if
(
chunk
.
length
>
2
)
return
modify
.
insertWordIntoTopic
(
topic
,
'!'
+
groupid
,
2
);
else
if
(
chunk
.
length
==
2
)
return
modify
.
insertWordIntoTopic
(
topic
,
'!'
+
clientid
,
2
);
else
return
""
;
}
}
else
return
""
;
case
'@private'
:
return
modify
.
insertWordIntoTopic
(
topic
,
'!'
+
clientid
,
1
);
}
}
}
}
module
.
exports
.
rewriteTopic
=
rewriteTopic
function
isShadow
(
topic
)
{
function
isShadow
(
topic
)
{
if
(
topic
.
indexOf
(
'@shadow'
)
!==
-
1
)
{
if
(
topic
.
indexOf
(
'@shadow'
)
!==
-
1
)
{
return
true
return
true
...
...
authhook/utils/modify.js
0 → 100644
View file @
8b9e2a85
module
.
exports
.
insertWordIntoTopic
=
insertWordIntoTopic
module
.
exports
.
removeTempWordInTopic
=
removeTempWordInTopic
/*
Ex: insertWordIntoTopic('@shadow/home/temp', '!GROUP', 1)
expected return : "@shadow/!GROUP/home/temp"
*/
function
insertWordIntoTopic
(
topic
,
word
,
pos
)
{
var
a
=
topic
.
split
(
'/'
);
a
.
splice
(
pos
,
0
,
word
);
// console.log('>>> insertWordIntoTopic('+topic +','+ word +','+ pos +');');
// console.log(a.join('/'));
return
a
.
join
(
'/'
);
}
/*
Ex : removeTempWordInTopic('@shadow/!GROUP/home/temp')
expected return : "@shadowhome/temp"
*/
function
removeTempWordInTopic
(
topic
)
{
var
a
=
topic
.
split
(
'/'
);
var
k
=
0
,
l
=
a
.
length
;
while
(
k
<
l
)
{
if
(
a
[
k
].
substr
(
0
,
1
)
==
'!'
)
{
a
.
splice
(
k
,
1
);
l
--
;
}
else
k
++
;
}
return
a
.
join
(
'/'
);
}
// Sample
// var str = insertWordIntoTopic('@shadow/home/temp', '!GROUP', 0);
// console.log(str);
// console.log(removeTempWordInTopic(str));
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment