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

Add docs to refresh token periodically #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
85 changes: 85 additions & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,88 @@ agServer.setMiddleware(agServer.MIDDLEWARE_INBOUND, async (middlewareStream) =>
```

!! Note that in this case, the token contains all the information that we need to authorize this publish action, but we didn't really need to store the `channels` list inside the JWT - An alternative approach would have been to fetch the user account details from the database using the username from the JWT (but it would require an extra database lookup; bad for performance). See the section on [middleware and authorization](middleware-and-authorization.md) for more info about middleware in SocketCluster.

### Refreshing auth tokens periodically

In the following code example there's a comprehensive way to refresh JWT token periodically.

```js
(async () => {
for await (let { socket } of agServer.listener('connection')) {
let renewAuthTokenInterval = null;
let jwtId = 0;

const clearRenewAuthTokenInterval = () => {
clearInterval(renewAuthTokenInterval);
renewAuthTokenInterval = null;
};

const renewAuthToken = (socket) => {
// 30 Minutes interval
const renewalIntervalMs = 30 * 60 * 1000;

const expirationInMinutes = 30;

// Renew the auth token periodically
renewAuthTokenInterval = setInterval(() => {
const oldToken = socket.authToken; // This is the token, translated to an object
if (oldToken) {
delete oldToken.iat;
delete oldToken.exp;
delete oldToken.nbf;
delete oldToken.jti;

socket.setAuthToken(oldToken, {
expiresIn: `${expirationInMinutes}m`, // See https://github.com/auth0/node-jsonwebtoken
jwtid: `${jwtId++}`,
});
}
}, renewalIntervalMs);
};

(async () => {
for await (const request of socket.procedure('login')) {
socket.setAuthToken({
hello: 'world',
});

renewAuthToken(socket);
}
})();

(async () => {
for await (const request of socket.procedure('logout')) {
socket.deauthenticate();
clearRenewAuthTokenInterval();
}
})();

(async () => {
for await (const request of socket.listener('disconnect')) {
clearRenewAuthTokenInterval();
}
})();

(async () => {
for await (const request of socket.listener('deauthenticate')) {
clearRenewAuthTokenInterval();
}
})();

// socketcluster-client automatically saves the auth token to LocalStorage as `socketcluster.authToken`
// That token is then provided by the #handshake event. In that event the server verifies if the token is valid.
// If it is it fires the authStateChange event
// Opposed checking via the connect event on the socket, that event is fired before the agServer connection event as per documentation
(async () => {
for await (const request of socket.listener('authStateChange')) {
if (
socket.authState === socket.AUTHENTICATED &&
!renewAuthTokenInterval
) {
renewAuthToken();
}
}
})();
}
})();
```