Skip to content

Commit 6fcb8b5

Browse files
author
Jens Timmerman
committed
added an IsAuthenticatedOrTokenHasScope Permission
1 parent dbc4702 commit 6fcb8b5

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

oauth2_provider/ext/rest_framework/permissions.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.core.exceptions import ImproperlyConfigured
44

5-
from rest_framework.permissions import BasePermission
5+
from rest_framework.permissions import BasePermission, IsAuthenticated
66

77
from ...settings import oauth2_settings
88

@@ -29,7 +29,7 @@ def has_permission(self, request, view):
2929

3030
return token.is_valid(required_scopes)
3131

32-
assert False, ('TokenHasScope requires either the'
32+
assert False, ('TokenHasScope requires the'
3333
'`oauth2_provider.rest_framework.OAuth2Authentication` authentication '
3434
'class to be used.')
3535

@@ -84,3 +84,16 @@ def get_scopes(self, request, view):
8484
]
8585

8686
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

Comments
 (0)