Many examples of RESTful web services do not address the problems that many applications today are multi-tenant.
Imagine a multi-user backend that provides a RESTful API . The backend data architecture uses a common database and a common schema. Each table will contain a link to tenant_id :
+-------------+----+-----------------+ | tenant_name| id | shared_secret | +-------------+----+-----------------+ | bob | 1 | 2737sm45sx543 | +-------------+----+-----------------+ | alice | 2 | 2190sl39sa8da | +-------------+----+-----------------+ +-------------+----+-------+-----------+ | pet_name | id | type | tenant_id | +-------------+----+-------+-----------+ | fuffy | 1 | dog | 1 | +-------------+----+-------+-----------+ | kerry | 2 | cat | 2 | +-------------+----+-------+-----------+
Question 1 : With three or more client applications (e.g. Android, iOS, and a web application) that interact with the RESTful backend , how would you authenticate against the backend?
RESTful backend, API, HTTP-Verbs, shared database and schema | | +---- Web Application (Client 1) | | | + Alice | | | + Bob | +---- Android Application (Client 2) | | | + Alice | | | + Bob | +---- iOS Application (Client 3) | | | + Alice | | | + Bob |
Each client must allow Alice and Bob to manage their pets. Each client is a graphical interface, and it will use (internally, making HTTP requests) a backend. Question: how can each client authenticate to the backend?
Suppose HMAC (it is excellent RESTful, no sessions): this method involves signing the payload with a shared secret (never sent over the wire). Should each client have its own copy of the tenant table (which contains the shared_secret field)?
Android App -> Client Sign -> Signed Request -> Backend -> Result Web App -> Client Sign -> Signed Request -> Backend -> Result
Question 2 : what does the resource URI look like?
Here are two possibilities for the ways to GET Bob:
Opportunity # 1: The Authorization header gives you the tenant's name (unique):
GET /pets HTTP/1.1 Host: www.example.org Authorization: bob:c29kYW9kYSBhb2lzYWRoIGYgZDUzNDUz
Opportunity number 2. tenant_id sent as a request parameter:
GET /pets/tenant_id=1 HTTP/1.1 Host: www.example.org Authorization: bob:c29kYW9kYSBhb2lzYWRoIGYgZDUzNDUz