A microservices case study: Auth
When writing your APIs for a microservice, special care should be taken to properly plan out auth and permissions since parts of your user system will be handled by other services. Previously, we introduced Yeti Threads as a case study for writing microservices in Node.js. This time, we’ll be diving into the Auth strategy in this case study.
“Auth” is really short for two things, authentication and authorization. Microservices don’t provide their own authentication, but they’re in charge of at least enforcing their own authorization, if not tracking the permissions as well. In this case, OpenID Connect gives us a JWT token encoded with a user identity and scopes of authorization (perhaps from Auth0, or one that we at &yet helped you customize and deploy). The microservice can trust these values because they’re signed by a private key that only the services themselves know, and can validate the signature. The scopes are a list of tags, typically indicating which sets of actions and endpoints are allowed.
In Yeti Threads, you might notice that we don’t have a users table in the database. Since we trust the identity and scopes in the signed JWT, we can simply treat the user id as an opaque string; it doesn’t matter what it is, as long as it uniquely identifies a user. I also decided that I didn’t need a scope for most actions, except for modifying permissions, in which case we need them to have 'forum_admin' in their scopes. Look at the access.js controller and you’ll see that the handlers specify an auth.scope
(which is a handy hapi handler feature).