This is the follow-up part to part 1 of Auth With JWT story. Highly recommended to go over that first if you haven’t already.
Challenges:
JWT Payload Size:
As you might have read in the first part, the business challenges we were trying to solve involved supporting different roles per context(e.g course). This means a user can be a student in one course and be a teacher in another course, be TA in another course etc. So, we needed to store information about a user and all their courses with the corresponding role in it.
This use case stresses our ability to bake all these data inside the JWT token in terms of size. As this token would be used in every request as an authorization context for the user, we needed to make sure that it doesn’t have any adverse effect on the request latency.
The mitigation strategy for this was:
Limit the authorization information for the user. So, here in this specific case, the user would have a limitation on how many courses they can enroll in(e.g 500). It works out fine as a user would usually need only so many courses for their lifetime of study.
Let’s say, for some reason; a user needs more courses than the set limitation, the mitigation at this level was to inject authorization info for the latest N set of authorization info for the user instead of everything based on the activity of that user in those contexts. Going with the student/course example, the user’s client would receive a token with the latest N courses the user access. In this way, the size limit of the token overhead would never go out of hand.
JWT Refresh Interval:
Another challenge was the refresh interval associated with JWT architecture. To keep the JWT shortlived, it needs to be refreshed at regular intervals. There is an inherent challenge with having a token valid for some time(here, it was specifically for a few minutes or so). If the authorization information changes during that interval(student enrolls in a new course etc), it’s not going to be reflected in the token. This will cause an inconsistent authorization/permission state in those contexts for the user.
This challenge of auth with JWT was mitigated by identifying those certain use cases where course role changes happen. And business logic behind those use cases would request and force a refresh of the token. Certain approaches were:
Whenever an event occurs that would result in a change of authorization, the response will include specific metadata that would indicate a need for a token refresh.
If the event occurs on the client’s end, the client will invalidate the token and make the “refresh token” API call immediately.
Other lessons learned:
Client-side support overhead:
While the team was already aware of the required support on the client side (web/mobile), their overheads of them were slightly underestimated, especially around the force refresh need, which ended up in several use cases, along with the nature that triggers request spike.
Doesn’t scale for permission-based auth:
This has been a known challenge right from the very beginning. Suppose we were to make the authorization process more granular and with individual permission; this would significantly increase the payload that would become virtually unusable. This risk was calculated, especially due to alignment across stakeholders to ensure we don’t have such a use case in the foreseeable future. Any permission-based authorization need could trigger re-thinking this architecture altogether.
Interesting findings(?):
Security- No context-based check:
In some areas, the code was checking a specific role without checking context-based authorization checks. Example:
if(role == "teacher") {
//do stuff assuming this is a teacher//no check for specific context(course etc)
} else {
//do stuff assuming this is a student role
}Code language:JavaScript(javascript)
As you can see, logic was written with the assumption of two roles only, as well as no explicit check was done to verify those users’ authorization for specific context requested (e.g course).
IDOR:
IDOR vulnerability was another issue we discovered in the codebase in different places due to a lack of security practices in the early days of the product.
Conclusion:
Despite the challenges explained, this was a suitable choice for us to build a good enough solution at a faster pace and with minimal overhead(e.g no server-side role-check etc). However, though we found auth with JWT to be a good fit for our platform use cases, it doesn’t necessarily mean JWT might be the right technology choice for you as well. You should do your due diligence. A core fundamental aspect of JWT is probably good to remember: JWT doesn’t care about the encryption of data. It simply cares about validation through signature verification.
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
Leave a Reply