Session management
Customizing the session duration
Depending on the business domain of an application, there might be different requirements for how long users should remain signed in. Criteria to base this decision upon typically revolve around user activity on the application and how long it has been since the user first signed in.
Ultimately, picking the ideal session lifetime is a trade-off between security and user experience. Longer sessions are generally better for UX but worse for security; and vice-versa.
Fortunately, with Nopwd you have to ability to fully control the lifetime of your users’ sessions. There are two settings for doing so and you can specify the authentication components attributes:
lifetime
: to set the maximum session lifetime (up to a year);idletimeout
: to optionaly set the inactivity timeout.
Getting a fresh session token
A session is initiated when a user is authenticated with <np-login>
authentication component.
To retrieve the user’s session token just call get
method:
Transmitting the session token
Your frontend is reponsible for sending the token
to your server. A typical way to do this is to transmit the token
in an Authorization
header like this:
Verifying the session token
Once transmitted to your server, you must verify that the token is valid (generated for your application and not expired). You can achieve that by:
- Using our API endpoint (simplest way);
- Using Third-Party JWT Libraries (recommanded);
- On your own.
Using our API endpoint
If you don’t have a backend server, for example, if you’re using Webflow, we have provided an API endpoint to validate the access token from nopwd.io.
See the API specs here to learn more.
Using Third-Party JWT Libraries
To avoid mistakes in this critical flow, you can use third party libraries to verify JWT tokens. Check the list of third party libraries here. Make sure you check for the ES256
algorithm that NoPwd uses.
Here is an example using the battle-tested jose library.
On your own
The token
is a JWT Token, and it’s signed using Asymmetric Signing Algorithm ES256. This means, unlike symmetric JWT tokens that are signed and verified using the same secret key, this asymmetric JWT Token is signed using a secret key that only Nopwd knows, but can be verified using a public key that you can find here.
Checklist:
- check 1: Check that the JWT is well-formed
- check 2: Make sure that the token is issued by nopwd:
iss = nopwd.io
- check 3: Make sure that the audience matches your own domain’s name:
aud = yourwebsite.com
- check 4: Make sure that the token is not expired:
now < exp
- check 5: Check the signature
Terminating a session
A session is typically terminated when the user logs out with the <np-logout>
button
component or after a period of inactivity. In that case, this component emit a np:logout
event.
You can also terminate a session programmaticaly using the revoke method:
A word about Security
To strengthen security, we use asymmetric cryptography (also known as public-key cryptographic) to maintain and verify the validity of a session. The private key is generated and stored (only) on the end-user device and can’t be extracted even by your web application. A compromission of a session token don’t compromize the entire session.