|
1 | 1 | Class-based Views
|
2 | 2 | =================
|
3 | 3 |
|
| 4 | +Django OAuth Toolkit provides generic classes useful to implement OAuth2 protected endpoints |
| 5 | +using the *Class Based View* approach. |
| 6 | + |
| 7 | + |
| 8 | +.. class:: ProtectedResourceView(ProtectedResourceMixin, View): |
| 9 | + |
| 10 | + A view that provides OAuth2 authentication out of the box. To implement a protected |
| 11 | + endpoint, just define your CBV as:: |
| 12 | + |
| 13 | + class MyEndpoint(ProtectedResourceView): |
| 14 | + """ |
| 15 | + A GET endpoint that needs OAuth2 authentication |
| 16 | + """ |
| 17 | + def get(self, request, *args, **kwargs): |
| 18 | + return HttpResponse('Hello, World!') |
| 19 | + |
| 20 | + **Please notice**: ``OPTION`` method is not OAuth2 protected to allow preflight requests. |
| 21 | + |
| 22 | +.. class:: ScopedProtectedResourceView(ScopedResourceMixin, ProtectedResourceView): |
| 23 | + |
| 24 | + A view that provides OAuth2 authentication and scopes handling out of the box. To implement |
| 25 | + a protected endpoint, just define your CBV specifying the ``required_scopes`` field:: |
| 26 | + |
| 27 | + class MyScopedEndpoint(ScopedProtectedResourceView): |
| 28 | + required_scopes = ['can_make_it can_break_it'] |
| 29 | + |
| 30 | + """ |
| 31 | + A GET endpoint that needs OAuth2 authentication |
| 32 | + and a set of scopes: 'can_make_it' and 'can_break_it' |
| 33 | + """ |
| 34 | + def get(self, request, *args, **kwargs): |
| 35 | + return HttpResponse('Hello, World!') |
| 36 | + |
| 37 | + |
| 38 | +.. class:: ReadWriteScopedResourceView(ReadWriteScopedResourceMixin, ProtectedResourceView): |
| 39 | + |
| 40 | + A view that provides OAuth2 authentication and read/write default scopes. |
| 41 | + ``GET``, ``HEAD``, ``OPTIONS`` http methods require ``read`` scope, others methods |
| 42 | + need the ``write`` scope. If you need, you can always specify an additional list of |
| 43 | + scopes in the ``required_scopes`` field:: |
| 44 | + |
| 45 | + class MyRWEndpoint(ReadWriteScopedResourceView): |
| 46 | + required_scopes = ['has_additional_powers'] # optional |
| 47 | + |
| 48 | + """ |
| 49 | + A GET endpoint that needs OAuth2 authentication |
| 50 | + and the 'read' scope. If required_scopes was specified, |
| 51 | + clients also need those scopes. |
| 52 | + """ |
| 53 | + def get(self, request, *args, **kwargs): |
| 54 | + return HttpResponse('Hello, World!') |
| 55 | + |
| 56 | + |
| 57 | +Generic views in DOT are obtained composing a set of mixins you can find in the :doc:`views.mixins <mixins>` |
| 58 | +module: feel free to use those mixins directly if you want to provide your own class based views. |
0 commit comments