@@ -253,15 +253,52 @@ associated with the account that created it. This allows the API to transparentl
253253serve only account-created documents on all kind of requests: read, edit, delete
254254and of course create.
255255
256- The only thing that we need to do is configure the name of the field that will
257- be used to store the owner of the document. It can be done at a global level
258- (all endpoints will use the same field) and/or at endpoint level (see feature
259- documentation for details). Let's just set the global field name in our
260- settings file:
256+ There are only two things that we need to do in order to activate this feature:
257+
258+ 1. configure the name of the field that will be used to store the owner of the
259+ document
260+ 2. set the document owner on each incoming POST request.
261+
262+
263+ Since we want to enable this feature for all of our API endpoints we'll just
264+ update our ``settings.py `` file by setting a proper ``AUTH_FIELD `` value:
265+
266+ ::
267+
268+ # Name of the field used to store the owner of each document
269+ AUTH_FIELD = 'user_id'
270+
271+
272+ Then, we want to update our authentication class to properly update the field's
273+ value:
261274
262275.. code-block :: python
276+ :emphasize- lines: 15 - 17
277+
263278
264- AUTH_USERNAME_FIELD : ' username'
279+ from eve import Eve
280+ from eve.auth import BasicAuth
281+ from werkzeug.security import check_password_hash
282+
283+
284+ class RolesAuth (BasicAuth ):
285+ def check_auth (self , username , password , allowed_roles , resource ):
286+ # use Eve's own db driver; no additional connections/resources are used
287+ accounts = app.data.driver.db[' accounts' ]
288+ lookup = {' username' : username}
289+ if allowed_roles:
290+ # only retrieve a user if his roles match ``allowed_roles``
291+ lookup[' roles' ] = {' $in' : allowed_roles}
292+ account = accounts.find_one(lookup)
293+ # set 'AUTH_FIELD' value to the account's ObjectId
294+ # (instead of _Id, you might want to use ID_FIELD)
295+ self .user_id = account[' _id' ]
296+ return account and check_password_hash(account[' password' ], password)
297+
298+
299+ if __name__ == ' __main__' :
300+ app = Eve(auth = RolesAuth)
301+ app.run()
265302
266303 This is all we need to do. Now, when a user hits the, say, ``/invoices/ ``
267304endpoint with a GET request, he will only be served with the invoices created
@@ -483,19 +520,9 @@ methods. See :ref:`auth` for more details.
4835206. Only allowing access to account resources
484521~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
485522This is achieved with the :ref: `user-restricted ` feature, as seen in
486- :ref: `accounts_basic `. Update the settings file with the following global
487- setting (or use the local ``auth_username_field `` if you only want to enable
488- the feature on selected endpoints):
489-
490- .. code-block :: python
491-
492- AUTH_USERNAME_FIELD : ' token'
493-
494- Stored documents will be associated with their account token. When a user hits
495- the, say, ``/invoices/ `` endpoint with a GET request, he will only be served
496- with the invoices created by his own token. The same will happen with DELETE
497- and PATCH, making it impossible for an authenticated user to accidentally
498- retrieve, edit or delete other people data.
523+ :ref: `accounts_basic `. You might want to store the user token as your
524+ ``AUTH_FIELD `` value, but if you want user tokens to be easily revocable, then
525+ your best option is to use the account unique id for this.
499526
500527Basic vs Token: Final Considerations
501528------------------------------------
@@ -505,10 +532,4 @@ stored on the client and being sent over the wire with every request. If
505532you're sending your tokens out-of-band, and you're on SSL/TLS, that's quite
506533a lot of additional security.
507534
508- If you are using the :ref: `user-restricted ` feature then a second and not
509- irrelevant advantage is that, since you are just storing tokens with documents,
510- when the user will eventually change his/her username no maintenance will be
511- needed, as the token itself won't change. With Basic Authentication, since we
512- would be storing usernames with documents, we'd be forced to update them all.
513-
514535.. _SSL/TLS : http://en.wikipedia.org/wiki/Transport_Layer_Security
0 commit comments