Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add client reserved userdata property #942

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [client.unsubscribe (unsubscriptions, [callback])](#clientunsubscribe-unsubscriptions-callback)
- [client.close ([callback])](#clientclose-callback)
- [client.emptyOutgoingQueue ([callback])](#clientemptyoutgoingqueue-callback)
- [client.userdata](#clientuserdata)

## new Client(aedes, stream, request)

Expand Down Expand Up @@ -154,3 +155,21 @@ Clear all outgoing messages (QoS > 1) related to this client from persistence

[websocket-stream]: https://www.npmjs.com/websocket-stream
[websocket-stream-doc-on-the-server]: https://github.com/maxogden/websocket-stream/blob/master/readme.md#on-the-server

## client.userdata

Aedes will not create or use client.userdata - so this is safe to create if you need to store your own data against client.

Examples of possible use: authenticating by JWT (store the decoded JWT data here), capture the IP address of the client, store additional statistics.

As the client is available in most callbacks, this may be useful. Beware client can be null in some callbacks for internal publish ($SYS).

```
client.userdata = {
ip: clientip,
data: {
write:[ 'path/allowed/publish' ],
read:[ 'path/allowed/subscribe' ]
}
}
```
46 changes: 46 additions & 0 deletions docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,49 @@ In order to use Aedes in clusters you have to choose a persistence and an mqemit
[mqemitter-redis]: https://www.npmjs.com/mqemitter-redis
[mqemitter-mongodb]: https://www.npmjs.com/mqemitter-mongodb
[mqemitter-child-process]: https://www.npmjs.com/mqemitter-child-process

## Client userdata

The tag `userdata` on the client structure is free for use by users to store whatever they want.

Comment on lines +113 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would name this section JWT Authentication and then explain that we will store JWT auth and client ip in client.userdata

<details>
<summary>Example snippet which uses JWT authentication, and captures the client ip address:</summary>

```
broker.authenticate = (client, username, password, callback)=>{
username = (username || '');
password = (password || '').toString();
let allow = false;
client.userdata = {};
if ((username === 'jwt')){
try {
var token = jwt.verify( password, secret );
// store token away for future use
client.userdata.token = token;
allow = true;
} catch(e) {
console.log('invalid jwt');
}
}
if (allow){
if (client.conn && client.conn.remoteAddress){
client.userdata.ip = client.conn.remoteAddress;
}
if (client.req){
if (client.req.socket){
client.userdata.ip = client.req.socket.remoteAddress;
}
if (client.req.rawHeaders){
for (let i = 0; i < client.req.rawHeaders.length; i++){
if (client.req.rawHeaders[i] === 'x-forwarded-for'){
client.userdata.ip = client.req.rawHeaders[i+1];
}
}
}
}
}
callback(null, allow);
}
```

</details>