In the previous article, we did a short overview of GraphQL technology and component for the SQL databases under .Net. There you may find a couple of examples on how to set up metaDbSchema, use pagination or define filters, so, if you haven’t read it yet — click here.
Press enter or click to view image in full size
It’s all good for fetching the data from DB, but what if you need more control over the resultset as well as the ability to quickly and easily set role-based permissions at the field level? In this case, we’ll need to implement access to the system, and permissions that allow a particular user getting data. It means that we need authentication/authorization facts and JSON Web Token (JWT), an open standard (RFC 7519) that defines a way for transmitting those facts.
Although both terms authorization and authentication are related to security and getting access to the data or part of the system’s functionality, they are often confused. So let’s shortly recall what do authentication, authorization, and JSON Web Token mean.
Authentication
When it comes to validating your credentials such as Username and Password (to verify your identity), the system checks whether you are using your own credentials. Essentially, authentication is the process of ascertaining that somebody really is the person who they claim to be.
Authorization
In contrast, authorization is the instrument to determine what level of access a user has. For example, is user Harald has the rights to see this data? Or he can only see data that is related to him?
Press enter or click to view image in full size
What is JSON Web Token (JWT)?
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed.
Authorization is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token.
The purpose of using JWT is not to hide data but to ensure the authenticity of the data. JWT is signed and encoded, not encrypted.
Setting up permissions for GraphQL API application
Get Dmytro Fedorchenko’s stories in your inbox
Join Medium for free to get updates from this writer.
We’ve already done all the magic and you can download it as an example, but let’s focus on a few crucial steps.
- First of all, let’s set authentication options in Startup.cs file
2. We also need web api method that generates JWT access token:
3. Now we can use Check and Apply permissions and here we have two options to do this. The first one is to apply user permissions on generating GraphQL schema. So, we can omit schemas or schema fields and those won’t appear and can’t be used in further actions:
In our example, we omit “Customer” schema if a logged user is in the “User” role. For example, it may be useful if we have to be sure that some sensitive entities are hidden from logged user, even if something wrong and data returned from DB. Let’s see how it works:
Press enter or click to view image in full size
The second way is quite interesting — prefiltered data is based on the SQL-query level’s role. Let’s inherit from DbCommandBuilder (we’re using NReco.Data as lighweight-ORM) and apply permissions before SQL-query is run in Startup.cs:
and set up permissions by using Relex query — if logged person has “admin” will fetch all customer’s oders (relex-where expression will be “true”) or if we logged as usual client we will get only orders related to that customer:
Now we can fetch only user’s orders based on our access permissions:
Press enter or click to view image in full size
For example, if we generate admin’s toke and try to fetch the same data — we’ll get all orders:
Press enter or click to view image in full size
Relex is a strong and very flexible instrument that allows us not only compose where-clause filter condition, but decide which fields will be chosen, compose a complex subqueries to check permission, ect.
In this topic, we played a bit with setting up access permissions for the NReco GraphQL API under .Net, showing how we can define and apply permissions on different levels (even on the field level). Depending on business needs, technical consultants may easily add or modify permission rules and it won’t take so much time. At the same time, it’s clear to understand what we show for particular users.
Thanks for reading and it would be cool to hear your thoughts in the comments.