This commit is contained in:
parent
c40e6c19ea
commit
cf927114c2
@ -46,7 +46,7 @@ use to authorize the following requests. it also sets a cookie to make
|
|||||||
requesting from the client more simple. (cookie parser is needed to make
|
requesting from the client more simple. (cookie parser is needed to make
|
||||||
authentication with cookies possible)
|
authentication with cookies possible)
|
||||||
|
|
||||||
the id of the logged in user will be available in `res.connection.user_id` in
|
the id of the logged in user will be available in `req.connection.user_id` in
|
||||||
all of the following request handlers.
|
all of the following request handlers.
|
||||||
|
|
||||||
### Excluding routes
|
### Excluding routes
|
||||||
|
14
index.js
14
index.js
@ -39,9 +39,10 @@ function init (get_user, ignore_paths = []) {
|
|||||||
*
|
*
|
||||||
* @param {string} user name or email of the given user
|
* @param {string} user name or email of the given user
|
||||||
* @param {string} password hashed password
|
* @param {string} password hashed password
|
||||||
|
* @param {any} req request object
|
||||||
* @returns {Promise<string>} session key if successful
|
* @returns {Promise<string>} session key if successful
|
||||||
*/
|
*/
|
||||||
async function authenticate (user, password, response) {
|
async function authenticate (user, password, req) {
|
||||||
const user_entry
|
const user_entry
|
||||||
= await new Promise ((res) => res (me.get_user (user)));
|
= await new Promise ((res) => res (me.get_user (user)));
|
||||||
|
|
||||||
@ -51,7 +52,7 @@ async function authenticate (user, password, response) {
|
|||||||
if (!await password_helper.verify (user_entry.password, password))
|
if (!await password_helper.verify (user_entry.password, password))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
response.connection.user_id = user_entry.id;
|
req.connection.user_id = user_entry.id;
|
||||||
|
|
||||||
const session_key = crypto.sign_object (
|
const session_key = crypto.sign_object (
|
||||||
{ id: user_entry.id },
|
{ id: user_entry.id },
|
||||||
@ -98,11 +99,13 @@ function request_handler_block (session, user, res) {
|
|||||||
* @param {string} session session key
|
* @param {string} session session key
|
||||||
* @param {string} user user name
|
* @param {string} user user name
|
||||||
* @param {string} key user hash
|
* @param {string} key user hash
|
||||||
|
* @param {any} req request object
|
||||||
* @param {any} res response object
|
* @param {any} res response object
|
||||||
* @param {any} next next handler
|
* @param {any} next next handler
|
||||||
* @returns {Promise<boolean>} true if handler authenticated
|
* @returns {Promise<boolean>} true if handler authenticated
|
||||||
*/
|
*/
|
||||||
async function request_handler_authenticate (session, user, key, res, next) {
|
// eslint-disable-next-line max-len, max-params
|
||||||
|
async function request_handler_authenticate (session, user, key, req, res, next) {
|
||||||
if (typeof session === 'undefined' && typeof user !== 'undefined') {
|
if (typeof session === 'undefined' && typeof user !== 'undefined') {
|
||||||
if (typeof key === 'undefined') {
|
if (typeof key === 'undefined') {
|
||||||
const user_salt = await salt (user);
|
const user_salt = await salt (user);
|
||||||
@ -115,7 +118,7 @@ async function request_handler_authenticate (session, user, key, res, next) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const session_key = await authenticate (user, key);
|
const session_key = await authenticate (user, key, req);
|
||||||
|
|
||||||
res.status (
|
res.status (
|
||||||
session_key === null
|
session_key === null
|
||||||
@ -137,6 +140,7 @@ async function request_handler_authenticate (session, user, key, res, next) {
|
|||||||
{ id: jwt.id },
|
{ id: jwt.id },
|
||||||
me.jwt_secret
|
me.jwt_secret
|
||||||
);
|
);
|
||||||
|
req.connection.user_id = jwt.id;
|
||||||
res.cookie (
|
res.cookie (
|
||||||
me.app_id,
|
me.app_id,
|
||||||
new_user_token,
|
new_user_token,
|
||||||
@ -192,7 +196,7 @@ async function request_handler (req, res, next) {
|
|||||||
|
|
||||||
if (request_handler_block (session, user, res))
|
if (request_handler_block (session, user, res))
|
||||||
return;
|
return;
|
||||||
if (await request_handler_authenticate (session, user, key, res, next))
|
if (await request_handler_authenticate (session, user, key, req, res, next))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
res.status (consts.http.status_forbidden);
|
res.status (consts.http.status_forbidden);
|
||||||
|
@ -42,7 +42,7 @@ async function start_server () {
|
|||||||
|
|
||||||
app.use ((req, res) => {
|
app.use ((req, res) => {
|
||||||
res.status (consts.http.status_ok)
|
res.status (consts.http.status_ok)
|
||||||
.end (`foo:${res.connection.user_id}`);
|
.end (`foo:${req.connection.user_id}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
return new Promise ((res) => {
|
return new Promise ((res) => {
|
||||||
|
@ -20,15 +20,12 @@ test.before (async () => {
|
|||||||
port = await mock_server.start_server ();
|
port = await mock_server.start_server ();
|
||||||
});
|
});
|
||||||
|
|
||||||
test.only ('login', async (t) => {
|
test ('login', async (t) => {
|
||||||
console.log ('logging in');
|
|
||||||
console.log ('port:', port);
|
|
||||||
const session = await client.login (
|
const session = await client.login (
|
||||||
'testuser',
|
'testuser',
|
||||||
'foo',
|
'foo',
|
||||||
`http://localhost:${port}`
|
`http://localhost:${port}`
|
||||||
);
|
);
|
||||||
console.log ('server respond');
|
|
||||||
t.is (typeof session, 'string');
|
t.is (typeof session, 'string');
|
||||||
|
|
||||||
const resp = await fetch (
|
const resp = await fetch (
|
||||||
@ -38,7 +35,6 @@ test.only ('login', async (t) => {
|
|||||||
|
|
||||||
t.is (resp.status, consts.http.status_ok);
|
t.is (resp.status, consts.http.status_ok);
|
||||||
t.is (await resp.text (), 'foo:69');
|
t.is (await resp.text (), 'foo:69');
|
||||||
console.log ('done test');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test ('allow access to excluded paths', async (t) => {
|
test ('allow access to excluded paths', async (t) => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user