servant
servant copied to clipboard
Add JWT token expiration at JWTSettings level - NominalDiffTime
Introduction
The ability to set expiration to the JWT Token in servant-auth-server library, rests on the CookieSettings data type configuration and in particular in the field cookieExpires as we can appreciate it here.
Discussion
The problems regarding using this field for setting JWT Token expiration time are the following:
-
CookieSettingsare usually created at application startup time and it keeps with the same values during the whole application life cycle. SincecookieExpiresis an absolute and deterministic point in time, futuresJWT Tokenswill contain precisely the same expiration time leading to an undesired behavior and expiring the token upon creation. -
CookieSettingsis a particular Data Type for all the cookies andJWT Tokenshould not be coupled to the rest of the cookies. - With the current setup and using the automatic authentication schema like the one described here, it is not possible to configure the application to create
JWT Tokenswith specificDiffTimeexpirations, like for example configure the authentication context to create a JWT that expires in 2 hours, even usingCookieSettings.cookieExpires. - The only possible way to do this is using the
acceptLoginfunction and the creation of theCookieSettingsvalue every time the entity authenticates successfully, but this authentication setup is manual and cannot be done withBasicAuthenticationcombinator.
Proposal
The proposal is implemented in this PR and includes the following changes:
- Add
expiresIn :: Maybe NominalDiffTimeinJWTSettings - Remove
Maybe UTCTimeparameter frommakeJWTfunction. - Calculate expiration on
makeJWTfunction usinggetCurrentTime + expiresInif it is present.
Solution
- The implemented solution will allow to create once
JWTSettingsandCookieSettingsbut allow the user to set an optionalNominalDiffTimeto calculate the expiration of theJWT Tokenupon token creation if the value is present. - This removes the need of calling explicitly
acceptLoginand allowingBasicAuthenticationcontext to handle the creation of the token by itself.