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
80fff711
Commit
80fff711
authored
Sep 20, 2018
by
zCaesar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refractor code 1st time
parent
61bbce57
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
128 additions
and
329 deletions
+128
-329
authhook/auth_on_publish.js
+17
-82
authhook/auth_on_register.js
+9
-24
authhook/auth_on_subscribe.js
+13
-76
authhook/checkClientRole.js
+17
-58
authhook/on_deliver.js
+0
-15
authhook/on_unsubscribe.js
+19
-22
authhook/package-lock.json
+0
-0
authhook/utils/getDates.js
+42
-0
authhook/utils/getGroupID.js
+11
-20
authhook/utils/getTopic.js
+0
-32
No files found.
authhook/auth_on_publish.js
View file @
80fff711
...
...
@@ -7,9 +7,9 @@ var LRU = require("lru-cache"),
});
// for testing
function
authCheck
(
client_id
,
username
,
topic
,
callback
)
{
callback
(
true
);
}
//
function authCheck(client_id, username, topic, callback) {
//
callback(true);
//
}
module
.
exports
=
function
(
options
=
{})
{
return
function
(
req
,
res
,
next
)
{
...
...
@@ -25,57 +25,24 @@ module.exports = function (options = {}) {
var
cachekey
=
'pub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
// checkRole
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
;
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
if
(
err
)
{
// Set Response
var
isShadow
=
require
(
'./utils/getTopic'
).
isShadow
if
(
isShadow
(
topic
))
{
var
output
=
{};
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
,
output
);
// get topic where concat with groupID
var
response
=
{
'result'
:
'ok'
,
'modifiers'
:
{
'topic'
:
_ftopic
,
'qos'
:
0
,
'retain'
:
false
}
if
(
typeof
(
authstatus
)
===
'undefined'
)
{
cache
.
set
(
cachekey
,
true
);
// cache missed
}
if
(
output
.
verb
==
'get'
||
output
.
verb
==
'read'
)
{
response
.
modifiers
.
payload
=
Buffer
.
from
(
req
.
body
.
client_id
).
toString
(
'base64'
);
}
console
.
log
(
response
);
var
response
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
var
decoded
=
require
(
'./checkClientRole'
).
checkRealDB
(
req
.
body
.
username
)
if
(
decoded
)
{
if
(
decoded
.
role
===
'realtimedb'
)
response
=
{
'result'
:
'ok'
}
else
response
=
{
'result'
:
'no'
}
res
.
send
(
response
);
});
}
else
{
res
.
send
(
response
);
}
next
();
})
}
else
{
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
// console.log(_id)
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'pub'
,
_id
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
response
=
{
var
output
=
{};
var
_id
=
require
(
'./utils/getGroupID'
).
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
)
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'pub'
,
_id
,
req
.
body
.
client_id
,
output
);
// get topic where concat with groupID
response
=
{
'result'
:
'ok'
,
'modifiers'
:
{
'topic'
:
_ftopic
,
...
...
@@ -83,47 +50,15 @@ module.exports = function (options = {}) {
'retain'
:
false
}
}
console
.
log
(
response
)
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
(
response
);
});
}
else
{
res
.
send
(
response
);
}
next
();
})
}
}
else
{
console
.
log
(
decoded
)
if
(
decoded
.
role
===
'realtimedb'
)
{
var
response
=
{
'result'
:
'ok'
}
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
(
response
);
});
if
(
output
.
verb
==
'get'
||
output
.
verb
==
'read'
)
{
response
.
modifiers
.
payload
=
Buffer
.
from
(
req
.
body
.
client_id
).
toString
(
'base64'
);
}
else
{
res
.
send
(
response
);
}
next
();
}
}
})
}
else
{
res
.
send
({
"result"
:
"no"
});
res
.
send
({
"result"
:
"no"
});
next
();
}
}
...
...
authhook/auth_on_register.js
View file @
80fff711
var
config
=
require
(
'config'
);
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
var
getRole
=
require
(
'./checkClientRole'
).
getRole
// https://github.com/isaacs/node-lru-cache
var
LRU
=
require
(
"lru-cache"
),
...
...
@@ -10,23 +8,18 @@ var LRU = require("lru-cache"),
});
var
debug
=
false
;
var
authclient
=
require
(
'seneca'
)({
log
:
'silent'
})
.
client
({
port
:
config
.
get
(
'authserv_port'
),
host
:
config
.
get
(
'authserv_host'
)
});
var
authclient
=
require
(
'seneca'
)({
log
:
'silent'
})
.
client
({
port
:
config
.
get
(
'authserv_port'
),
host
:
config
.
get
(
'authserv_host'
)
});
function
authCheck
(
client_id
,
token
,
password
,
callback
)
{
checkRealDB
(
token
,
(
err
,
decoded
)
=>
{
/*
* decode.scope
* decode.iat
* decode.exp
*/
if
(
err
)
{
getRole
(
token
,
(
role
)
=>
{
if
(
role
===
false
)
{
authclient
.
act
({
role
:
'auth'
,
cmd
:
'token'
,
action
:
'info'
,
token
:
token
},
function
(
err
,
res
)
{
if
(
require
(
'./checkClientRole'
).
checkRealDB
(
token
))
{
// auth realtimedb by token
callback
(
true
)
}
else
{
if
(
require
(
'./checkClientRole'
).
getRole
(
token
))
callback
(
true
)
// auth client device by token
else
{
authclient
.
act
({
role
:
'auth'
,
cmd
:
'token'
,
action
:
'info'
,
token
:
token
},
function
(
err
,
res
)
{
// auth client device by query from db
if
(
debug
)
{
console
.
log
(
"res ------>
\
n"
);
console
.
log
(
res
);
...
...
@@ -48,16 +41,8 @@ function authCheck(client_id, token, password, callback) {
callback
(
false
);
}
});
}
else
callback
(
true
)
})
}
else
{
callback
(
true
)
}
})
}
module
.
exports
=
function
(
options
=
{})
{
...
...
authhook/auth_on_subscribe.js
View file @
80fff711
...
...
@@ -9,9 +9,9 @@ var LRU = require("lru-cache"),
var
debug
=
false
;
// for testing
function
authCheck
(
client_id
,
token
,
topic
,
callback
)
{
callback
(
true
);
}
//
function authCheck(client_id, token, topic, callback) {
//
callback(true);
//
}
module
.
exports
=
function
(
options
=
{})
{
...
...
@@ -30,99 +30,36 @@ module.exports = function (options = {}) {
var
cachekey
=
'sub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
// checkRole
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
if
(
err
)
{
// Set Response
var
isShadow
=
require
(
'./utils/getTopic'
).
isShadow
if
(
isShadow
(
topic
))
{
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
'topic'
:
_ftopic
,
'qos'
:
0
}]
var
response
=
{
'result'
:
'ok'
,
'topics'
:
_topic
}
console
.
log
(
response
)
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
(
response
);
});
cache
.
set
(
cachekey
,
true
);
// cache missed
}
else
{
var
response
var
decoded
=
require
(
'./checkClientRole'
).
checkRealDB
(
req
.
body
.
username
)
if
(
decoded
)
{
if
(
decoded
.
role
===
'realtimedb'
)
response
=
{
'result'
:
'ok'
}
else
response
=
{
'result'
:
'no'
}
res
.
send
(
response
);
}
next
();
})
}
else
{
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
// console.log(_id)
var
_id
=
require
(
'./utils/getGroupID'
).
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
)
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'sub'
,
_id
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
_topic
=
[{
// setTopic for response
'topic'
:
_ftopic
,
'qos'
:
0
}]
var
response
=
{
response
=
{
'result'
:
'ok'
,
'topics'
:
_topic
}
console
.
log
(
response
)
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
(
response
);
});
}
else
{
res
.
send
(
response
);
}
next
();
})
}
}
else
{
console
.
log
(
decoded
)
if
(
decoded
.
role
===
'realtimedb'
)
{
var
response
=
{
'result'
:
'ok'
}
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
(
response
);
});
}
else
{
res
.
send
(
response
);
}
next
();
}
}
})
}
else
{
res
.
send
({
"result"
:
"no"
});
res
.
send
({
"result"
:
"no"
});
next
();
}
}
}
authhook/checkClientRole.js
View file @
80fff711
var
jwt
=
require
(
'jsonwebtoken'
)
var
config
=
require
(
'config'
)
var
getExp
=
require
(
'./utils/getDates'
).
getExp
function
checkRealDB
(
token
,
cb
)
{
module
.
exports
.
checkRealDB
=
checkRealDB
module
.
exports
.
getRole
=
getRole
module
.
exports
.
signRole
=
signRole
function
checkRealDB
(
token
)
{
const
verifyOptions
=
{
algorithms
:
[
'RS256'
]
};
jwt
.
verify
(
token
,
config
.
get
(
'pubca'
),
verifyOptions
,
(
err
,
decoded
)
=>
{
cb
(
err
,
decoded
)
})
try
{
return
jwt
.
verify
(
token
,
config
.
get
(
'pubca'
),
verifyOptions
)
}
catch
(
e
)
{
return
false
}
}
module
.
exports
.
checkRealDB
=
checkRealDB
function
getRole
(
token
,
cb
)
{
function
getRole
(
token
)
{
var
secret
=
'nexpie'
jwt
.
verify
(
token
,
secret
,
(
err
,
decoded
)
=>
{
if
(
err
)
{
// console.log(err)
cb
(
false
)
try
{
return
jwt
.
verify
(
token
,
secret
)
}
else
{
// console.log(decoded)
cb
(
decoded
)
catch
(
e
)
{
return
false
}
})
}
module
.
exports
.
getRole
=
getRole
function
signRole
(
req
,
res
)
{
var
secret
=
'nexpie'
...
...
@@ -40,44 +42,3 @@ function signRole(req, res) {
res
.
send
(
'role not complete'
)
}
}
\ No newline at end of file
module
.
exports
.
signRole
=
signRole
function
getExp
(
expires
)
{
var
exp
if
(
expires
.
endsWith
(
'y'
))
{
exp
=
getYears
(
expires
.
split
(
'y'
)[
0
])
}
else
if
(
expires
.
endsWith
(
'm'
))
{
exp
=
getMonths
(
expires
.
split
(
'm'
)[
0
])
}
else
if
(
expires
.
endsWith
(
'd'
))
{
exp
=
expires
.
split
(
'd'
)[
0
]
+
'd'
}
else
if
(
expires
.
endsWith
(
'h'
))
{
exp
=
expires
.
split
(
'h'
)[
0
]
+
'h'
}
else
if
(
expires
.
endsWith
(
'mi'
))
{
exp
=
getMinutes
(
expires
.
split
(
'mi'
)[
0
])
}
else
if
(
expires
.
endsWith
(
's'
))
{
exp
=
getSeconds
(
expires
.
split
(
's'
)[
0
])
}
else
exp
=
getYears
(
10
)
return
exp
}
function
getMonths
(
d
)
{
return
(
d
*
30
)
+
'd'
}
function
getYears
(
m
)
{
return
(
m
*
30
*
12
)
+
'd'
}
function
getMinutes
(
mi
)
{
return
(
mi
*
1000
*
60
)
+
'ms'
}
function
getSeconds
(
ms
)
{
return
(
ms
*
1000
)
+
'ms'
}
\ No newline at end of file
authhook/on_deliver.js
View file @
80fff711
...
...
@@ -16,21 +16,6 @@ function on_deliver(req, res, next) {
}
module
.
exports
.
on_deliver
=
on_deliver
function
hasGroup
(
topics
)
{
if
(
topics
.
indexOf
(
'!'
)
>
-
1
)
{
return
true
}
else
return
false
}
module
.
exports
.
hasGroup
=
hasGroup
// function beDesireTopic(topics) {
// if (hasGroup(topics)) {
// return joinTopic(topics)
// }
// else return topics
// }
function
joinTopic
(
topics
)
{
var
topic
=
topics
.
split
(
'/!'
)[
1
]
return
topics
.
split
(
'/!'
)[
0
]
+
topic
.
substring
(
topic
.
indexOf
(
'/'
),
topic
.
length
)
...
...
authhook/on_unsubscribe.js
View file @
80fff711
...
...
@@ -2,33 +2,31 @@ function on_unsubscribe(req, res, next) {
console
.
log
(
'------on unsubscribe-------'
)
console
.
log
(
req
.
body
)
var
topic
=
req
.
body
.
topics
[
0
];
// checkRole
var
checkRealDB
=
require
(
'./checkClientRole'
).
checkRealDB
checkRealDB
(
req
.
body
.
username
,
(
err
,
decoded
)
=>
{
if
(
err
)
{
// Set Response
var
getGroupID
=
require
(
'./utils/getGroupID'
).
getGroupID
getGroupID
(
req
.
body
.
username
,
req
.
body
.
client_id
,
(
_id
)
=>
{
// get groupID
// console.log(_id)
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'unsub'
,
_id
,
req
.
body
.
client_id
)
// get topic where concat with groupID
var
response
=
{
"result"
:
"ok"
,
"topics"
:
[
_ftopic
]
var
token
=
req
.
body
.
username
var
client_id
=
req
.
body
.
client_id
var
response
var
decoded
=
require
(
'./checkClientRole'
).
checkRealDB
(
token
)
if
(
decoded
)
{
if
(
decoded
.
role
===
'realtimedb'
)
{
response
=
{
'result'
:
'ok'
}
}
console
.
log
(
response
)
else
response
=
{
'result'
:
'no'
}
res
.
send
(
response
);
}
)
next
(
)
}
else
{
console
.
log
(
decoded
)
if
(
decoded
.
role
===
'realtimedb'
)
{
var
response
=
{
'result'
:
'ok'
var
_id
=
require
(
'./utils/getGroupID'
).
getGroupID
(
token
,
client_id
)
if
(
_id
)
{
var
_ftopic
=
require
(
'./utils/getTopic'
).
rewriteTopic
(
topic
,
'unsub'
,
_id
,
client_id
)
response
=
{
"result"
:
"ok"
,
"topics"
:
[
_ftopic
]
}
console
.
log
(
response
)
res
.
send
(
response
);
}
else
response
=
{
'result'
:
'no'
}
res
.
send
(
response
);
next
()
}
})
}
module
.
exports
.
on_unsubscribe
=
on_unsubscribe
\ No newline at end of file
authhook/package-lock.json
View file @
80fff711
This diff is collapsed.
Click to expand it.
authhook/utils/getDates.js
0 → 100644
View file @
80fff711
module
.
exports
.
getExp
=
getExp
function
getExp
(
expires
)
{
var
exp
if
(
expires
.
endsWith
(
'y'
))
{
exp
=
getYears
(
expires
.
split
(
'y'
)[
0
])
}
else
if
(
expires
.
endsWith
(
'm'
))
{
exp
=
getMonths
(
expires
.
split
(
'm'
)[
0
])
}
else
if
(
expires
.
endsWith
(
'd'
))
{
exp
=
expires
.
split
(
'd'
)[
0
]
+
'd'
}
else
if
(
expires
.
endsWith
(
'h'
))
{
exp
=
expires
.
split
(
'h'
)[
0
]
+
'h'
}
else
if
(
expires
.
endsWith
(
'mi'
))
{
exp
=
getMinutes
(
expires
.
split
(
'mi'
)[
0
])
}
else
if
(
expires
.
endsWith
(
's'
))
{
exp
=
getSeconds
(
expires
.
split
(
's'
)[
0
])
}
else
exp
=
getYears
(
10
)
return
exp
}
function
getMonths
(
d
)
{
return
(
d
*
30
)
+
'd'
}
function
getYears
(
m
)
{
return
(
m
*
30
*
12
)
+
'd'
}
function
getMinutes
(
mi
)
{
return
(
mi
*
1000
*
60
)
+
'ms'
}
function
getSeconds
(
ms
)
{
return
(
ms
*
1000
)
+
'ms'
}
\ No newline at end of file
authhook/utils/getGroupID.js
View file @
80fff711
var
getRole
=
require
(
'../checkClientRole'
).
getRole
function
getGroupID
(
token
,
id
,
cb
)
{
checkID
(
token
,
id
,
(
groupId
)
=>
{
// console.log(groupId)
cb
(
groupId
)
})
}
module
.
exports
.
getGroupID
=
getGroupID
function
checkID
(
token
,
id
,
cb
)
{
// console.log(token)
getRole
(
token
,
(
role
)
=>
{
// console.log(role)
function
getGroupID
(
token
,
client_id
)
{
var
role
=
getRole
(
token
)
if
(
role
)
{
if
(
role
.
hasOwnProperty
(
'groupId'
))
{
cb
(
role
.
groupId
)
return
role
.
groupId
}
else
{
doDB
(
id
,
(
groupId
)
=>
{
// console.log(groupId)
cb
(
groupId
)
})
return
doDB
(
client_id
)
}
})
}
else
return
role
}
module
.
exports
.
getGroupID
=
getGroupID
function
doDB
(
id
,
cb
)
{
function
doDB
(
client_id
)
{
var
groupId
=
'test'
cb
(
groupId
)
return
groupId
}
\ No newline at end of file
authhook/utils/getTopic.js
View file @
80fff711
// function getTopic(topic, id) {
// //var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1]
// var _topic = topicPrefix(topic) + id + topic.split(topicPrefix(topic))[1]
// return _topic
// }
// module.exports.getTopic = getTopic
// function topicPrefix(topic) {
// if (topic.indexOf('@msg') !== -1) {
// return '@msg/!'
// } else if (topic.indexOf('@shadow/updated') !== -1) {
// return '@shadow/updated/!'
// } else if (topic.indexOf('@shadow/update') !== -1) {
// return '@shadow/update/'
// } else if (topic.indexOf('@shadow/get') !== -1) {
// return '@shadow/get/!'
// } else if (topic.indexOf('@client') !== -1) {
// return '@client/'
// }
// }
var
modify
=
require
(
'./modify'
);
function
rewriteTopic
(
topic
,
op
,
groupid
,
clientid
,
output
)
{
...
...
@@ -64,12 +43,3 @@ function rewriteTopic(topic, op, groupid, clientid, output) {
}
module
.
exports
.
rewriteTopic
=
rewriteTopic
\ No newline at end of file
function
isShadow
(
topic
)
{
if
(
topic
.
indexOf
(
'@shadow'
)
!==
-
1
)
{
return
true
}
else
return
false
}
module
.
exports
.
isShadow
=
isShadow
\ No newline at end of file
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