julianlam / nodebb-plugin-session-sharing

Allows login sessions from your app to persist in NodeBB
MIT License
88 stars 65 forks source link

Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value #45

Closed anemol closed 6 years ago

anemol commented 6 years ago

I updated nodebb to the v1.7.4 and now the plugin doesn't work.

2018-02-06T20:08:34.483Z [2652] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T20:08:34.486Z [2652] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
2018-02-06T20:08:38.768Z [2652] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T20:08:38.769Z [2652] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
2018-02-06T20:08:39.990Z [2652] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
julianlam commented 6 years ago

Hm... looks like you're on redis. Can you run this for me?

type appId:uid (this is assuming the token id is "appId", which is the default if it is not set in the plugin settings.

zscore schemaLog session_sharing_hash_to_zset

and let me know what the output is?

anemol commented 6 years ago

Yes, i'm using redis database.

type appId:uid
hash

zscore schemaLog session_sharing_hash_to_zset
"1517857135427"

Until now, the plugin worked fine and users were able to auto-connect to my forum from my website (nodejs).

I upgraded nodebb to the last version (v1.7.4) yesterday because a problem with the top menu+ theme persona. Today, an user said to me he couldn't go anymore on forum after refreshing his cache and logout. I did the same operation (logout) and yes, impossible to auto-login from my website.

I added some fields to my "payload base", especially "groups".

Still have errors:

2018-02-06T20:53:34.754Z [14643] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T20:53:34.760Z [14643] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value

My payload base is:

    var data = {
                        "id": req.user.id,
                        "username": req.user.username,
                        //"lastName": '',
                        //"fisrName": '',
                        "fullname":'',
                        'website':'',
                        'birthday':'',
                        'aboutme':'',
                        "picture" : '',
                        'location':'',
                        'signature':'',
                        'groupTitle':'',
                        'email': req.user.email||'',
                        'groups':[],
                    };
julianlam commented 6 years ago

It should be fine to remove groups. Whether you leave it in or not is of no consequence.

Can you run this in redis-cli for me?

zrem schemaLog session_sharing_hash_to_zset

This should remove the logged upgrade for the session sharing plugin. Not sure why it did not upgrade properly for you.

Then ./nodebb upgrade -s, and let me know the output. One of the lines should read:

Convert remote-to-local user ID from hash to sorted set

anemol commented 6 years ago
zrem schemaLog session_sharing_hash_to_zset
(integer) 1

./nodebb upgrade -s

Updating NodeBB...

1. Updating NodeBB data store schema...
Parsing upgrade scripts... 
OK | 1 script(s) found, 46 skipped
  → [2017/10/19] Convert remote-to-local user ID from hash to sorted set... OK
Schema update complete!
2018-02-06T21:11:57.784Z [21263] - info: [session-sharing] Settings OK
2018-02-06T21:11:57.786Z [21263] - info: Routes added
2018-02-06T21:11:57.788Z [21263] - info: NodeBB Ready
2018-02-06T21:11:57.790Z [21263] - info: Enabling 'trust proxy'
2018-02-06T21:11:57.794Z [21263] - info: NodeBB is now listening on: 0.0.0.0:4567
2018-02-06T21:12:05.249Z [21263] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
2018-02-06T21:12:23.165Z [21263] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T21:12:23.166Z [21263] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
2018-02-06T21:12:47.325Z [21263] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T21:12:47.326Z [21263] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
2018-02-06T21:12:53.474Z [21263] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T21:12:53.475Z [21263] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value

Still not working.

julianlam commented 6 years ago

Which version of the session-sharing plugin do you have? Latest is v4.2.2

anemol commented 6 years ago

Installed 4.2.2 | Latest 4.2.2

julianlam commented 6 years ago

Thanks. I just published v4.2.3.

Can you update to that version, then zrem schemaLog session_sharing_hash_to_zset, and then ./nodebb upgrade -s?

anemol commented 6 years ago

Installed 4.2.3 | Latest 4.2.3

zrem schemaLog session_sharing_hash_to_zset
(integer) 1
./nodebb upgrade -s

Updating NodeBB...

1. Updating NodeBB data store schema...
Parsing upgrade scripts... 
OK | 1 script(s) found, 46 skipped
  → [2017/10/19] Convert remote-to-local user ID from hash to sorted set... OK
Schema update complete!

Unfortunately, still not working and the same errors:

2018-02-06T21:38:55.668Z [29027] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T21:38:55.672Z [29027] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
2018-02-06T21:38:59.248Z [29027] - warn: [session-sharing] Array expected for `groups` in JWT payload. Ignoring.
2018-02-06T21:38:59.259Z [29027] - warn: [session-sharing] Error encountered while parsing token: WRONGTYPE Operation against a key holding the wrong kind of value
anemol commented 6 years ago

Maybe the problem comes from my payload? (but it worked before, so, i don't understand)

From the website side, i coded as that:

var jwt = require('jsonwebtoken');
var data = {
                        "id": req.user.id,
                        "username": req.user.username,
                        //"lastName": '',
                        //"fisrName": '',
                        "fullname":'',
                        'website':'',
                        'birthday':'',
                        'aboutme':'',
                        "picture" : '',
                        'location':'',
                        'signature':'',
                        'groupTitle':'',
                        'email': req.user.email||'',
                        //'groups':[],
                    };

var token = jwt.sign(data, secret);
res.cookie('forum', token, { domain:'.littleworldofsatoshi.com',httpOnly: true,maxAge: 7*24*3600 * 1000 });
julianlam commented 6 years ago

Any chance you can send me your redis database rdb file for testing?

anemol commented 6 years ago

I don't prefer no. Sorry.

I had to reset the plugin because i lost the admin page and impossible to login.

./nodebb reset --plugin nodebb-plugin-session-sharing

When re-enable pluging and go into admin page, impossible to go on the plugin page (already had this type of error before)

2018-02-06T22:15:52.508Z [8414] - error: /assets/templates/admin/plugins/session-sharing.js
 Error: ENOENT: no such file or directory, open '/home/fofo/NodeBB/build/public/templates/admin/plugins/session-sharing.tpl'
    at Error (native)

I had to build and then ok. Maybe a normal error.

For now, i'm using my primo account to admin the forum because my normal admin account uses the sharing sessions and is not more usable.

julianlam commented 6 years ago

@anemol In that case, install v3.2.1 of the session-sharing plugin. That might get you going again.

anemol commented 6 years ago

How to find and install an older version? Your github doesn't publish a release list.

anemol commented 6 years ago

I tried to change the payload with the minimal field list but still the same error in the log.

data = {
"id": req.user.id,
"username": req.user.username,
'email': req.user.email||null,
};

id and username are unique and email is not require.

If i remove the email field, i get this error:

2018-02-07T12:02:29.786Z [3322] - info: [session-sharing] Payload valid, but local account not found. Assuming guest.

If i check the database, (appId #uid) i have the right value (id from website = id from forum)

Field | Value
-- | --
171 | 2
173 | 29
174 | 3
176 | 4
178 | 22
...

If i print to console the err ( plugin.findOrCreateUser method), i get this:

{ ReplyError: WRONGTYPE Operation against a key holding the wrong kind of value
    at new Command (/home/fofo/NodeBB/node_modules/redis/lib/command.js:12:22)
    at RedisClient.zscore (/home/fofo/NodeBB/node_modules/redis/lib/commands.js:58:47)
    at module.sortedSetScore (/home/fofo/NodeBB/src/database/redis/sorted.js:131:15)
    at /home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:1204:19
    at apply (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:20:25)
    at /home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:56:12
    at /home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:3686:9
    at replenish (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:882:17)
    at /home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:886:9
    at eachOfLimit (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:913:22)
    at /home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:918:16
    at eachOf (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:991:5)
    at _parallel (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:3685:5)
    at Object.parallelLimit [as parallel] (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:3765:3)
    at plugin.findOrCreateUser (/home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/library.js:210:10)
    at /home/fofo/NodeBB/node_modules/nodebb-plugin-session-sharing/node_modules/async/dist/async.js:1204:19
  command: 'ZSCORE',
  args: [ 'appId:uid', 171 ],
  code: 'WRONGTYPE' }

For information, i'm using Redis Version 3.0.6

julianlam commented 6 years ago

Yes, that's precisely the problem -- your appId:uid is a hash, when it should be a sorted set. I made the change with a corresponding upgrade script but for whatever reason your NodeBB isn't actually performing the upgrade. That's what I requested a database dump to debug but otherwise I can't reproduce to fix the issue, whatever it is...

julianlam commented 6 years ago

I think I got it. Please download the latest version (v4.2.4) and run the following again to trigger the upgrade script:

zrem schemaLog session_sharing_hash_to_zset

./nodebb upgrade -s

anemol commented 6 years ago

I updated to the last version and apply the commands.
It works now :) Thanks!

julianlam commented 6 years ago

Glad to hear it, and happy to know you're back on your way :+1: