2
2
3
3
from django .core .exceptions import ImproperlyConfigured
4
4
5
- from rest_framework .permissions import BasePermission
5
+ from rest_framework .permissions import BasePermission , IsAuthenticated
6
6
7
7
from ...settings import oauth2_settings
8
8
@@ -29,7 +29,7 @@ def has_permission(self, request, view):
29
29
30
30
return token .is_valid (required_scopes )
31
31
32
- assert False , ('TokenHasScope requires either the'
32
+ assert False , ('TokenHasScope requires the'
33
33
'`oauth2_provider.rest_framework.OAuth2Authentication` authentication '
34
34
'class to be used.' )
35
35
@@ -84,3 +84,16 @@ def get_scopes(self, request, view):
84
84
]
85
85
86
86
return required_scopes
87
+
88
+
89
+ class IsAuthenticatedOrTokenHasScope (BasePermission ):
90
+ """
91
+ The user is authenticated using some backend or the token has the right scope
92
+ This is usefull when combined with the DjangoModelPermissions to allow people browse the browsable api's
93
+ if they log in using the a non token bassed middleware,
94
+ and let them access the api's using a rest client with a token
95
+ """
96
+ def has_permission (self , request , view ):
97
+ is_authenticated = IsAuthenticated ()
98
+ token_has_scope = TokenHasScope ()
99
+ return is_authenticated .has_permission (request , view ) or token_has_scope .has_permission (request , view )
0 commit comments