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
ce0624cf
Commit
ce0624cf
authored
Jun 28, 2018
by
Chavee Issariyapat
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auth with auth service
parent
241d16c4
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
83 additions
and
86 deletions
+83
-86
authhook/auth_on_publish.js
+19
-26
authhook/auth_on_register.js
+31
-33
authhook/auth_on_subscribe.js
+19
-26
authhook/config/custom-environment-variables.json
+4
-0
authhook/config/development.json
+4
-0
authhook/index.js
+3
-0
authhook/package.json
+3
-1
No files found.
authhook/auth_on_publish.js
View file @
ce0624cf
var
config
=
require
(
'config'
);
// https://github.com/isaacs/node-lru-cache
// https://github.com/isaacs/node-lru-cache
var
LRU
=
require
(
"lru-cache"
)
,
var
LRU
=
require
(
"lru-cache"
)
,
cache
=
LRU
({
cache
=
LRU
({
...
@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
...
@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
});
});
// for testing
// for testing
function
authPublish
(
client_id
,
username
,
topic
)
{
function
authCheck
(
client_id
,
username
,
topic
,
callback
)
{
if
(
!
client_id
||
!
username
||
!
topic
)
return
false
;
callback
(
true
);
else
return
true
;
}
}
module
.
exports
=
function
(
options
=
{})
{
module
.
exports
=
function
(
options
=
{})
{
...
@@ -24,36 +24,29 @@ module.exports = function(options={}) {
...
@@ -24,36 +24,29 @@ module.exports = function(options={}) {
var
topic
=
req
.
body
.
topic
;
var
topic
=
req
.
body
.
topic
;
var
cachekey
=
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
cachekey
=
'pub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
// cache missed
if
(
authPublish
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
))
{
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
authstatus
=
true
;
cache
.
set
(
cachekey
,
result
);
cache
.
set
(
cachekey
,
authstatus
);
res
.
send
({
}
"result"
:
result
?
"ok"
:
"no"
else
{
});
authstatus
=
false
;
});
cache
.
set
(
cachekey
,
authstatus
);
}
}
else
{
// cache hit
}
if
(
authstatus
)
{
out
=
{
"result"
:
"ok"
}
}
}
else
{
else
{
out
=
{
res
.
send
(
{
"result"
:
"no"
"result"
:
authstatus
?
"ok"
:
"no"
}
}
);
}
}
res
.
send
(
out
);
next
();
next
();
}
}
else
{
res
.
send
({
"result"
:
"no"
});
}
}
}
}
}
authhook/auth_on_register.js
View file @
ce0624cf
var
config
=
require
(
'config'
);
// https://github.com/isaacs/node-lru-cache
// https://github.com/isaacs/node-lru-cache
var
LRU
=
require
(
"lru-cache"
)
,
var
LRU
=
require
(
"lru-cache"
)
,
cache
=
LRU
({
cache
=
LRU
({
...
@@ -5,51 +6,48 @@ var LRU = require("lru-cache") ,
...
@@ -5,51 +6,48 @@ var LRU = require("lru-cache") ,
maxAge
:
1000
*
60
*
5
maxAge
:
1000
*
60
*
5
});
});
var
authclient
=
require
(
'seneca'
)({
log
:
'silent'
})
.
client
({
port
:
config
.
get
(
'authserv_port'
),
host
:
config
.
get
(
'authserv_host'
)});
// for testing
// for testing
function
authByUserPasswd
(
client_id
,
username
,
password
)
{
function
authCheck
(
client_id
,
token
,
password
,
callback
)
{
if
(
!
client_id
||
!
username
||
!
password
)
return
false
;
authclient
.
act
({
role
:
'auth'
,
cmd
:
'token'
,
action
:
'info'
,
token
:
token
},
function
(
err
,
res
)
{
else
return
true
;
console
.
log
(
res
);
if
(
res
&&
res
.
data
)
{
callback
(
true
);
}
else
{
callback
(
false
);
}
});
}
}
module
.
exports
=
function
(
options
=
{})
{
module
.
exports
=
function
(
options
=
{})
{
return
function
(
req
,
res
,
next
)
{
return
function
(
req
,
res
,
next
)
{
if
(
req
.
header
(
'vernemq-hook'
)
==
'auth_on_register'
)
{
if
(
req
.
header
(
'vernemq-hook'
)
==
'auth_on_register'
&&
req
&&
req
.
body
)
{
if
(
options
.
debug
)
{
var
cachekey
=
'auth:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
req
.
body
.
password
;
console
.
log
(
req
.
body
);
var
authstatus
=
cache
.
get
(
cachekey
);
}
var
out
,
authstatus
;
var
cachekey
=
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
req
.
body
.
password
;
authstatus
=
cache
.
get
(
cachekey
);
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
// cache missed
if
(
authByUserPasswd
(
req
.
body
.
client_id
,
req
.
body
.
username
,
req
.
body
.
password
))
{
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
req
.
body
.
password
,
function
(
result
)
{
authstatus
=
true
;
cache
.
set
(
cachekey
,
result
);
cache
.
set
(
cachekey
,
authstatus
);
res
.
send
({
}
"result"
:
result
?
"ok"
:
"no"
else
{
});
authstatus
=
false
;
});
cache
.
set
(
cachekey
,
authstatus
);
}
}
else
{
// cache hit
}
if
(
authstatus
)
{
out
=
{
"result"
:
"ok"
}
}
}
else
{
else
{
out
=
{
res
.
send
(
{
"result"
:
"no"
"result"
:
authstatus
?
"ok"
:
"no"
}
}
);
}
}
res
.
send
(
out
);
next
();
next
();
}
}
else
{
res
.
send
({
"result"
:
"no"
});
}
}
}
}
}
authhook/auth_on_subscribe.js
View file @
ce0624cf
var
config
=
require
(
'config'
);
// https://github.com/isaacs/node-lru-cache
// https://github.com/isaacs/node-lru-cache
var
LRU
=
require
(
"lru-cache"
)
,
var
LRU
=
require
(
"lru-cache"
)
,
cache
=
LRU
({
cache
=
LRU
({
...
@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
...
@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
});
});
// for testing
// for testing
function
authSubscribe
(
client_id
,
username
,
topic
)
{
function
authCheck
(
client_id
,
username
,
topic
,
callback
)
{
if
(
!
client_id
||
!
username
||
!
topic
)
return
false
;
callback
(
true
);
else
return
true
;
}
}
module
.
exports
=
function
(
options
=
{})
{
module
.
exports
=
function
(
options
=
{})
{
...
@@ -24,36 +24,29 @@ module.exports = function(options={}) {
...
@@ -24,36 +24,29 @@ module.exports = function(options={}) {
var
topic
=
req
.
body
.
topic
;
var
topic
=
req
.
body
.
topic
;
var
cachekey
=
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
cachekey
=
'sub:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
topic
;
var
authstatus
=
cache
.
get
(
cachekey
);
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
// cache missed
if
(
authSubscribe
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
))
{
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
authstatus
=
true
;
cache
.
set
(
cachekey
,
result
);
cache
.
set
(
cachekey
,
authstatus
);
res
.
send
({
}
"result"
:
result
?
"ok"
:
"no"
else
{
});
authstatus
=
false
;
});
cache
.
set
(
cachekey
,
authstatus
);
}
}
else
{
// cache hit
}
if
(
authstatus
)
{
out
=
{
"result"
:
"ok"
}
}
}
else
{
else
{
out
=
{
res
.
send
(
{
"result"
:
"no"
"result"
:
authstatus
?
"ok"
:
"no"
}
}
);
}
}
res
.
send
(
out
);
next
();
next
();
}
}
else
{
res
.
send
({
"result"
:
"no"
});
}
}
}
}
}
authhook/config/custom-environment-variables.json
0 → 100644
View file @
ce0624cf
{
"authserv_host"
:
"AUTH_SERVICE_HOST"
,
"authserv_port"
:
"AUTH_SERVICE_PORT"
}
authhook/config/development.json
0 → 100644
View file @
ce0624cf
{
"authserv_host"
:
"203.154.135.231"
,
"authserv_port"
:
31088
}
authhook/index.js
View file @
ce0624cf
process
.
env
[
"NODE_ENV"
]
=
"development"
;
process
.
env
[
"NODE_CONFIG_DIR"
]
=
__dirname
+
"/config/"
;
var
restify
=
require
(
'restify'
);
var
restify
=
require
(
'restify'
);
var
auth_on_register
=
require
(
'./auth_on_register'
)({
debug
:
true
});
var
auth_on_register
=
require
(
'./auth_on_register'
)({
debug
:
true
});
var
auth_on_publish
=
require
(
'./auth_on_publish'
)({
debug
:
true
});
var
auth_on_publish
=
require
(
'./auth_on_publish'
)({
debug
:
true
});
...
...
authhook/package.json
View file @
ce0624cf
...
@@ -9,7 +9,9 @@
...
@@ -9,7 +9,9 @@
"author"
:
""
,
"author"
:
""
,
"license"
:
"
ISC
"
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"dependencies"
:
{
"
config
"
:
"^1.30.0"
,
"
lru-cache
"
:
"^4.1.3"
,
"
lru-cache
"
:
"^4.1.3"
,
"
restify
"
:
"^7.1.1"
"
restify
"
:
"^7.1.1"
,
"
seneca
"
:
"^3.6.0"
}
}
}
}
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