Project

General

Profile

Authz2023 » History » Version 15

Shuvam Misra, 27/12/2023 12:20 PM

1 1 Shuvam Misra
# Authorization architecture, design and implementation
2
3
It's 2023. Trump may become President of God's Own Country next year. We have moved from bespoke authentication and authorization design and implementation to Keycloak and IDshield. With all this comes a new view about the architecture of authorization data, and its implementation.
4
5
## Authorisation information
6
7
This information specifies what a user can and cannot do. It has four dimensions:
8 12 Shuvam Misra
* **Raw capability**: this specifies if the user can perform a specific operation. In implementation, it maps on to a web service call (WSC). Can user X call WSC Y?
9
* **Visibility constraints**: this specifies whether the user can see all data in a specific table or for a specific call (*e.g.* all sales data) and if not, then which subsets can he see? This is equivalent to doing a `SELECT` on a table and using the visibility constraints for a `WHERE` clause. For instance, user X can see all sales data and user Y can see only North Zone data. In other words, the visibility constraint defines the scope of the access right.
10 1 Shuvam Misra
* **Attribute constraints**: this specifies whether the user can see/edit all the attributes (or columns, in DB parlance) of a class of entities, or can only see a subset. For instance, some privileged users can see the full employee list with all attributes, but most users are not allowed to see the salary data. So, there are restrictions on certain restricted attributes. We can then divide the list of attributes into a general-access set and a privileged set.
11
* **Value constraints**: this specifies whether a user's access is restricted to certain value limits of certain quantitative fields. For instance, a junior manager is permitted to approve an invoice with a total value less than a million dollars, whereas a vice president can approve invoices of up to ten million.
12
13
Combining all these, we can make a sample statement like this: Mister Joe Pesci can do voucher edits (he has the `voucheredit` **capability**) for vouchers of only retail sales (**visibility constraint** based on voucher type) whose value is less than $20,000 (**value constraint**). And while he does so, he is not permitted to change the date of the voucher (**attribute constraint**).
14
15
Expressing this in a tight notation, we can specify this tuple:
16
```
17 5 Shuvam Misra
joe.pesci:   (voucheredit, vouchertype=retailsales, val=(amt,le,20000), attr=!date)
18 1 Shuvam Misra
```
19
20 12 Shuvam Misra
We call these tuples **qualified capabilities**, which are the result of applying constraints to raw capabilities. Here, the `(amt, le, 20000)` indicates that the amount of the voucher needs to be less than or equal to (hence `le`) the limit given.
21 3 Shuvam Misra
22 12 Shuvam Misra
Mr Pesci may have multiple such qualified capabilities, for various combinations of these four elements.
23 1 Shuvam Misra
```
24 5 Shuvam Misra
joe.pesci:   (voucherview, vouchertype=ALL, val={}, attr={})
25
joe.pesci:   (voucheredit, vouchertype=retailsales, val=(amt,le,20000), attr=!date)
26
joe.pesci:   (vouchernew,  vouchertype=retailsales, val=(amt,le,20000), attr={})
27 1 Shuvam Misra
```
28
With this set of records, Mr Pesci can see all vouchers, create vouchers only in retail sales of value less than $20,000 and enter all details (no attribute constraints) but when editing these vouchers, he is not allowed to edit the date.
29
30 12 Shuvam Misra
We can collapse the semantics of the fourth term (attribute constraints) if we expand the set of values for the first term. So, instead of having a generic `voucheredit` raw capability, we define two raw capabilities: `vouchereditfull` and `vouchereditnodate`. With this refinement, we can express the previous set of qualified capabilities in the following way:
31 1 Shuvam Misra
```
32 5 Shuvam Misra
joe.pesci:   (voucherview,        vouchertype=ALL,         val={})
33 11 Shuvam Misra
joe.pesci:   (vouchereditnodate,  vouchertype=retailsales, val=(amt,le,20000))
34 5 Shuvam Misra
joe.pesci:   (vouchernewfull,     vouchertype=retailsales, val=(amt,le,20000))
35 1 Shuvam Misra
```
36
Thus, the fourth member of the tuple can be eliminated everywhere by applying this trick of defining a more fine-grained set of capabilities.
37 11 Shuvam Misra
38
We make one more pragmatic simplification: we assume that quantitative limits (*i.e.* `val=`) will always be upper caps. It is very unlikely that there will be a lower cap to authorisation constraints. In that case, we may assume that the operator will always be `le`, therefore `val=(amt, le, 20000)` now becomes simplified to `limit=(amt, 20000)`. Therefore we now have:
39
```
40 1 Shuvam Misra
joe.pesci:   (voucherview,        vouchertype=ALL,         limit={})
41
joe.pesci:   (vouchereditnodate,  vouchertype=retailsales, limit=(amt,20000))
42
joe.pesci:   (vouchernewfull,     vouchertype=retailsales, limit=(amt,20000))
43
```
44
45 12 Shuvam Misra
We can now move from the particular to the general. If we generalise the example of Mr Pesci's capabilities. We can now say that a qualified capability has
46
* one raw capability
47
* zero or more scope constraints
48
* zero or more upper-limit constraints
49
50
Switching to JSON, we get
51
``` json
52
"usercaps": {
53
    "user": "joe.pesci",
54
    "caplist": [{
55
        "cap": "voucherview",
56
        "scope": [
57
            {"vouchertype": "ALL"}
58
        ],
59
        "limit": []
60
    },{
61
        "cap": "vouchereditnodate",
62
        "scope": [
63
            {"vouchertype": "retailsales"}
64
        ],
65
        "limit": [
66
            {"amt": 20000}
67
        ]
68
    },{
69
        "cap": "vouchernewfull",
70
        "scope": [
71
            {"vouchertype": "retailsales"}
72
        ],
73
        "limit": [
74
            {"amt": 20000}
75
        ]
76
    }]
77
}
78
```
79
There will be one such block for each user in the system, and the `caplist` can have dozens or hundreds of elements in its array. In this representation, one qualified capability is represented by:
80
``` json
81
    {
82
        "cap": "vouchernewfull",
83
        "scope": [
84
            {"vouchertype": "retailsales"}
85
        ],
86
        "limit": [
87
            {"amt": 20000}
88
        ]
89
    }
90
```
91 13 Shuvam Misra
Both `scope` and `limit` are arrays, so it's possible to have additional entries in them. For example:
92
``` json
93
    {
94
        "cap": "vouchernewfull",
95
        "scope": [
96
            {"vouchertype": "retailsales"},
97
            {"region": "N"}
98
        ],
99
        "limit": [
100
            {"amt": 20000},
101
            {"voucherage": 30}
102
        ]
103
    }
104
```
105
This may mean that the user has the `vouchernewfull` raw capability, which allows the user to create new vouchers, but
106
* only for retail sales transactions
107
* only for the North region, not for any other part of the business
108
* only for voucher values less than or equal to 20,000 in whatever is the currency
109
* only for vouchers younger than or equal to 30 days, which means there is a cap on how far back-dated the new vouchers may be
110 4 Shuvam Misra
111
## Using the authorisation information
112
113 14 Shuvam Misra
A matching function will go through a user's `usercaps` data structure and decide whether there is any qualified capability in her `caplist` which matches the access being attempted.
114 1 Shuvam Misra
115 14 Shuvam Misra
For a qualified capability to match:
116
* all the `scope` terms in the capability must match the corresponding terms given in access being attempted
117
* all the `limit` terms in the capability must equal or exceed the figures in the access being attempted
118
* if there is a term missing in the `scope` or `limit` of the access being attempted, then it is deemed that it has matched the corresponding term in the capability. So, for instance, if there is no `region` specified when Mister Pesci attempts to view vouchers, and the capability says `"region": "N"`, it is deemed that Mister Pesci can be permitted to perform the operation. (The `"region": "N"` becomes a constraint which will be applied by the business logic, outside the authorisation checking function.)
119
120
The matching operation will return the list of matching elements from the `caplist`. More than one entry may match; all matching entries will be returned in an array.
121
122
The business logic which called the authorisation check function will now scan the entries returned, and will apply any constraints indicated by them. For instance, two users may attempt a `voucherview` operation, but one user may have a qualified capability with `"scope": [{"region": "N"}]` and the other user may not have any `"region"` constraint. In that case, the business logic will receive the matching capabilities list from the matching function, and in the first user's case, will see the `"region"` constraint, and will pull out only the matching subset of vouchers to show the user. In the second user's case, the business logic must query the database and pull out all vouchers to show. This is to be done *by the business logic*, not by the authorisation module.
123 4 Shuvam Misra
124
## The `authz_check()` function
125
126 15 Shuvam Misra
This function may be designed to take two parameters: *(i)* all details of an access attempt, and *(ii)* the user's `caplist`.
127 4 Shuvam Misra
128 15 Shuvam Misra
Before the application code calls `authz_check()`, it needs to know the list of attributes based on which authorization decisions are made. Basing our example on the tuples shown earlier, the code may go through the following questions and put together a set of attributes and values:
129 4 Shuvam Misra
* who is the user? `joe.pesci`
130 15 Shuvam Misra
* what operation is being attempted? Voucher edit. So we determine the `cap` the operation needs:
131 4 Shuvam Misra
  * Is the date too being updated? If yes, then `vouchereditfull`
132 15 Shuvam Misra
  * Else, `vouchereditnodate` or `vouchereditfull`
133
* What's the type of the voucher being accessed? Pull out the voucher record from the database and find out its type. Let us say it turns out to be `bulksales`
134
* What is the amount of the voucher being accessed? This is `15520.50`
135 4 Shuvam Misra
136
It is not necessary that all details of the operation being attempted are contained in the request parameters of the WSC. Some authorisation determining parameters may be environmental, like
137 1 Shuvam Misra
* what is the country from which the access is being attempted (use geo-IP)
138
* what is the time of day? (It's conceivable that access rules do not permit editing of records outside office hours, only viewing.)
139
140 15 Shuvam Misra
So, first, the application code must put together all details of the operation being attempted:
141 4 Shuvam Misra
``` json
142
{
143
    "user": "joe.pesci",
144 15 Shuvam Misra
    "capneeded": ["vouchereditfull", "vouchereditnodate"]
145
    "scope": [
146
        {"vouchertype": "bulksales"},
147
    ],
148
    "limit": [
149
        {"voucheramt": "15520.50"}
150
    ]
151 4 Shuvam Misra
}
152
```
153 8 Shuvam Misra
Having put together this packet of attributes and values, the authorization-check function may be called. The function will first peep inside this structure, pull out the username (`joe.pesci`), load the user's authorization rights tuples from backing store, and perform a matching operation. If the full set of authorization tuples are part of the JWT payload, then no database access is needed.
154 4 Shuvam Misra
155 1 Shuvam Misra
Given the sample tuples listed above, `joe.pesci` will not be granted permission to perform the operation, since he has no rights to operate on `bulksales` vouchers.
156 8 Shuvam Misra
157
## Variety of constraint variables
158
159
The examples above have dealt with voucher operations including viewing, editing, and creation. For those operations, the list of constraint variables were
160
* voucher type
161
* voucher amount
162
163
For an entirely different operation, say viewing of MIS reports of sales data, the constraints may be
164
* which zone the user belongs to (he will see only his own region's data)
165 9 Shuvam Misra
* which department he belongs to (he will see only his product category's data, and dept maps to product category)
166 8 Shuvam Misra
* what his rank is (this decides whether he can see only the last month's data, or the last year's, or all historical data)
167
168
So, for voucher-related capabilities, one set of constraint variables are applied, and for sales report viewing, a totally different set of variables apply. These can be keyed to the capability:
169
* for `voucherview`, `vouchereditfull`, `vouchereditnondate`, `vouchernew`, the voucher type is a constraint variable
170
* for `salesreport`, the user's department ID is a constraint variable