Skip to content

Commit f9df58c

Browse files
committed
add authentication token_expire
1 parent 5c2c805 commit f9df58c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,27 @@ set `API_PERMISSION_CONF` in your settings.py as a dict.
4343

4444
```python
4545
API_PERMISSION_CONF = {
46-
'API_PREFIX': ['api/topic/'], # default is /api/
46+
'API_PREFIX': ['api/topic/'], # default is /
4747
'PERMISSION_DENIED_CODE': 1, # default is 1
4848
'AUTHORIZATION_HEADER': 'HTTP_AUTHORIZATION', # default is HTTP_AUTHORIZATION
49-
'ADMIN_SITE_PATH': '/admin/' # default is /admin/
49+
'ADMIN_SITE_PATH': '/admin/', # default is /admin/
50+
'TOKEN_EXPIRE': 15, # unit is days, default is None, which won't check token expire.
5051
}
5152
```
5253

5354
You can custom `API_PREFIX` as a str like `'/'` or list like `['api/account', 'api/topic']`.
5455

56+
** When you set `TOKEN_EXPIRE`, you need add below in your `REST_FRAMEWORK` settings. **
57+
58+
```python
59+
REST_FRAMEWORK = {
60+
'DEFAULT_AUTHENTICATION_CLASSES': (
61+
...
62+
'api_permission.authentication.ExpireTokenAuthentication',
63+
),
64+
}
65+
```
66+
5567
## 3. Demo
5668

5769
### 3.1 list

api_permission/api_settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
API_PERMISSION_CONF = getattr(settings, "API_PERMISSION_CONF", {})
44

5-
API_PREFIX = API_PERMISSION_CONF.get('API_PREFIX', ['/api/'])
5+
API_PREFIX = API_PERMISSION_CONF.get('API_PREFIX', ['/'])
66
PERMISSION_DENIED_CODE = API_PERMISSION_CONF.get('PERMISSION_DENIED_CODE', 1)
77
AUTHORIZATION_HEADER = API_PERMISSION_CONF.get('AUTHORIZATION_HEADER', 'HTTP_AUTHORIZATION')
88
ADMIN_SITE_PATH = API_PERMISSION_CONF.get('ADMIN_SITE_PATH', '/admin/')
9+
TOKEN_EXPIRE = API_PERMISSION_CONF.get('TOKEN_EXPIRE', None)

api_permission/authentication.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import datetime
2+
3+
from django.conf import settings
4+
from django.utils import timezone
5+
from django.utils.translation import ugettext_lazy as _
6+
from rest_framework import exceptions
7+
from rest_framework.authentication import TokenAuthentication
8+
from .api_settings import TOKEN_EXPIRE
9+
10+
11+
class ExpireTokenAuthentication(TokenAuthentication):
12+
def authenticate_credentials(self, key):
13+
model = self.get_model()
14+
try:
15+
token = model.objects.select_related('user').get(key=key)
16+
except model.DoesNotExist:
17+
raise exceptions.AuthenticationFailed(_('Token not exists.'))
18+
19+
if not token.user.is_active:
20+
raise exceptions.AuthenticationFailed(_('User not active or deleted.'))
21+
22+
if TOKEN_EXPIRE:
23+
assert type(TOKEN_EXPIRE) == int, "TOKEN_EXPIRE type must be Int"
24+
if timezone.now() > token.created + datetime.timedelta(days=int(TOKEN_EXPIRE)):
25+
raise exceptions.AuthenticationFailed(_('Token has expired'))
26+
27+
return (token.user, token)

0 commit comments

Comments
 (0)