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
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
77 additions
and
80 deletions
+77
-80
authhook/auth_on_publish.js
+17
-24
authhook/auth_on_register.js
+29
-31
authhook/auth_on_subscribe.js
+17
-24
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
var
LRU
=
require
(
"lru-cache"
)
,
cache
=
LRU
({
...
...
@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
});
// for testing
function
authPublish
(
client_id
,
username
,
topic
)
{
if
(
!
client_id
||
!
username
||
!
topic
)
return
false
;
else
return
true
;
function
authCheck
(
client_id
,
username
,
topic
,
callback
)
{
callback
(
true
);
}
module
.
exports
=
function
(
options
=
{})
{
...
...
@@ -24,36 +24,29 @@ module.exports = function(options={}) {
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'
)
{
// cache missed
if
(
authPublish
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
))
{
authstatus
=
true
;
cache
.
set
(
cachekey
,
authstatus
);
}
else
{
authstatus
=
false
;
cache
.
set
(
cachekey
,
authstatus
);
}
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
({
"result"
:
result
?
"ok"
:
"no"
});
});
}
else
{
// cache hit
}
if
(
authstatus
)
{
out
=
{
"result"
:
"ok"
res
.
send
({
"result"
:
authstatus
?
"ok"
:
"no"
});
}
next
();
}
else
{
out
=
{
res
.
send
(
{
"result"
:
"no"
}
}
res
.
send
(
out
);
next
();
});
}
}
}
authhook/auth_on_register.js
View file @
ce0624cf
var
config
=
require
(
'config'
);
// https://github.com/isaacs/node-lru-cache
var
LRU
=
require
(
"lru-cache"
)
,
cache
=
LRU
({
...
...
@@ -5,51 +6,48 @@ var LRU = require("lru-cache") ,
maxAge
:
1000
*
60
*
5
});
var
authclient
=
require
(
'seneca'
)({
log
:
'silent'
})
.
client
({
port
:
config
.
get
(
'authserv_port'
),
host
:
config
.
get
(
'authserv_host'
)});
// for testing
function
authByUserPasswd
(
client_id
,
username
,
password
)
{
if
(
!
client_id
||
!
username
||
!
password
)
return
false
;
else
return
true
;
function
authCheck
(
client_id
,
token
,
password
,
callback
)
{
authclient
.
act
({
role
:
'auth'
,
cmd
:
'token'
,
action
:
'info'
,
token
:
token
},
function
(
err
,
res
)
{
console
.
log
(
res
);
if
(
res
&&
res
.
data
)
{
callback
(
true
);
}
else
{
callback
(
false
);
}
});
}
module
.
exports
=
function
(
options
=
{})
{
return
function
(
req
,
res
,
next
)
{
if
(
req
.
header
(
'vernemq-hook'
)
==
'auth_on_register'
)
{
if
(
options
.
debug
)
{
console
.
log
(
req
.
body
);
}
var
out
,
authstatus
;
var
cachekey
=
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
req
.
body
.
password
;
authstatus
=
cache
.
get
(
cachekey
);
if
(
req
.
header
(
'vernemq-hook'
)
==
'auth_on_register'
&&
req
&&
req
.
body
)
{
var
cachekey
=
'auth:'
+
req
.
body
.
client_id
+
':'
+
req
.
body
.
username
+
':'
+
req
.
body
.
password
;
var
authstatus
=
cache
.
get
(
cachekey
);
if
(
typeof
(
authstatus
)
==
'undefined'
)
{
// cache missed
if
(
authByUserPasswd
(
req
.
body
.
client_id
,
req
.
body
.
username
,
req
.
body
.
password
))
{
authstatus
=
true
;
cache
.
set
(
cachekey
,
authstatus
);
}
else
{
authstatus
=
false
;
cache
.
set
(
cachekey
,
authstatus
);
}
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
req
.
body
.
password
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
({
"result"
:
result
?
"ok"
:
"no"
});
});
}
else
{
// cache hit
}
if
(
authstatus
)
{
out
=
{
"result"
:
"ok"
res
.
send
({
"result"
:
authstatus
?
"ok"
:
"no"
});
}
next
();
}
else
{
out
=
{
res
.
send
(
{
"result"
:
"no"
}
}
res
.
send
(
out
);
next
();
});
}
}
}
authhook/auth_on_subscribe.js
View file @
ce0624cf
var
config
=
require
(
'config'
);
// https://github.com/isaacs/node-lru-cache
var
LRU
=
require
(
"lru-cache"
)
,
cache
=
LRU
({
...
...
@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
});
// for testing
function
authSubscribe
(
client_id
,
username
,
topic
)
{
if
(
!
client_id
||
!
username
||
!
topic
)
return
false
;
else
return
true
;
function
authCheck
(
client_id
,
username
,
topic
,
callback
)
{
callback
(
true
);
}
module
.
exports
=
function
(
options
=
{})
{
...
...
@@ -24,36 +24,29 @@ module.exports = function(options={}) {
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'
)
{
// cache missed
if
(
authSubscribe
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
))
{
authstatus
=
true
;
cache
.
set
(
cachekey
,
authstatus
);
}
else
{
authstatus
=
false
;
cache
.
set
(
cachekey
,
authstatus
);
}
authCheck
(
req
.
body
.
client_id
,
req
.
body
.
username
,
topic
,
function
(
result
)
{
cache
.
set
(
cachekey
,
result
);
res
.
send
({
"result"
:
result
?
"ok"
:
"no"
});
});
}
else
{
// cache hit
}
if
(
authstatus
)
{
out
=
{
"result"
:
"ok"
res
.
send
({
"result"
:
authstatus
?
"ok"
:
"no"
});
}
next
();
}
else
{
out
=
{
res
.
send
(
{
"result"
:
"no"
}
}
res
.
send
(
out
);
next
();
});
}
}
}
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
auth_on_register
=
require
(
'./auth_on_register'
)({
debug
:
true
});
var
auth_on_publish
=
require
(
'./auth_on_publish'
)({
debug
:
true
});
...
...
authhook/package.json
View file @
ce0624cf
...
...
@@ -9,7 +9,9 @@
"author"
:
""
,
"license"
:
"
ISC
"
,
"dependencies"
:
{
"
config
"
:
"^1.30.0"
,
"
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