Browse Source

Adiciona autenticação via Token

pull/3151/head
eribeiro 6 years ago
parent
commit
9b1764ec6e
  1. 17
      docs/token-auth.rst
  2. 4
      sapl/api/urls.py
  3. 18
      sapl/api/views.py
  4. 18
      sapl/settings.py

17
docs/token-auth.rst

@ -0,0 +1,17 @@
1. Realizar o migrate
./manage.py migrate
2. Criar um API Token para usuário e anotar a API Key gerada.
python3 manage.py drf_create_token admin
3. Testar endpoint
curl http://localhost:8000/api/version -H 'Authorization: Token <API Key>'
4. Exemplo de POST
curl -d '{"nome_completo”:”Gozer The Gozerian“, "nome_parlamentar": “Gozer”, "sexo":"M"}' -X POST http://localhost:8000/api/parlamentares/parlamentar/ -H 'Authorization: Token <API Key>' -H 'Content-Type: application/json'
Note: If you use TokenAuthentication in production you must ensure that your API is only available over https.
References: https://www.django-rest-framework.org/api-guide/authentication/#tokenauthentication

4
sapl/api/urls.py

@ -6,7 +6,7 @@ from rest_framework.routers import DefaultRouter
from sapl.api.deprecated import MateriaLegislativaViewSet, SessaoPlenariaViewSet,\ from sapl.api.deprecated import MateriaLegislativaViewSet, SessaoPlenariaViewSet,\
AutoresProvaveisListView, AutoresPossiveisListView, AutorListView,\ AutoresProvaveisListView, AutoresPossiveisListView, AutorListView,\
ModelChoiceView ModelChoiceView
from sapl.api.views import SaplApiViewSetConstrutor from sapl.api.views import SaplApiViewSetConstrutor, AppVersionView
from .apps import AppConfig from .apps import AppConfig
@ -70,7 +70,7 @@ urlpatterns = [
url(r'^api/', include(deprecated_urlpatterns_api)), url(r'^api/', include(deprecated_urlpatterns_api)),
url(r'^api/', include(urlpatterns_api_doc)), url(r'^api/', include(urlpatterns_api_doc)),
url(r'^api/', include(urlpatterns_router)), url(r'^api/', include(urlpatterns_router)),
url(r'^api/version', AppVersionView.as_view()),
# implementar caminho para autenticação # implementar caminho para autenticação
# https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/ # https://www.django-rest-framework.org/tutorial/4-authentication-and-permissions/

18
sapl/api/views.py

@ -20,6 +20,9 @@ from rest_framework.decorators import action
from rest_framework.fields import SerializerMethodField from rest_framework.fields import SerializerMethodField
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from sapl.api.forms import SaplFilterSetMixin from sapl.api.forms import SaplFilterSetMixin
from sapl.api.permissions import SaplModelPermissions from sapl.api.permissions import SaplModelPermissions
@ -587,3 +590,18 @@ class _NormaJuridicaViewset:
def destaques(self, request, *args, **kwargs): def destaques(self, request, *args, **kwargs):
self.queryset = self.get_queryset().filter(norma_de_destaque=True) self.queryset = self.get_queryset().filter(norma_de_destaque=True)
return self.list(request, *args, **kwargs) return self.list(request, *args, **kwargs)
class AppVersionView(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request):
content = {
'name': 'SAPL',
'description': 'Sistema de Apoio ao Processo Legislativo',
'version': settings.SAPL_VERSION,
'user': request.user.username,
'is_authenticated': request.user.is_authenticated(),
}
return Response(content)

18
sapl/settings.py

@ -86,6 +86,7 @@ INSTALLED_APPS = (
'drf_yasg', 'drf_yasg',
#'rest_framework_swagger', #'rest_framework_swagger',
'rest_framework', 'rest_framework',
'rest_framework.authtoken',
'django_filters', 'django_filters',
'easy_thumbnails', 'easy_thumbnails',
@ -147,14 +148,6 @@ if DEBUG:
SITE_URL = config('SITE_URL', cast=str, default='') SITE_URL = config('SITE_URL', cast=str, default='')
CACHES = {
'default': {
'BACKEND': 'speedinfo.backends.proxy_cache',
'CACHE_BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}
REST_FRAMEWORK = { REST_FRAMEWORK = {
"UNICODE_JSON": False, "UNICODE_JSON": False,
"DEFAULT_PARSER_CLASSES": ( "DEFAULT_PARSER_CLASSES": (
@ -167,6 +160,7 @@ REST_FRAMEWORK = {
"sapl.api.permissions.SaplModelPermissions", "sapl.api.permissions.SaplModelPermissions",
), ),
"DEFAULT_AUTHENTICATION_CLASSES": ( "DEFAULT_AUTHENTICATION_CLASSES": (
'rest_framework.authentication.TokenAuthentication',
"rest_framework.authentication.SessionAuthentication", "rest_framework.authentication.SessionAuthentication",
), ),
"DEFAULT_PAGINATION_CLASS": "sapl.api.pagination.StandardPagination", "DEFAULT_PAGINATION_CLASS": "sapl.api.pagination.StandardPagination",
@ -175,6 +169,14 @@ REST_FRAMEWORK = {
'django_filters.rest_framework.DjangoFilterBackend', 'django_filters.rest_framework.DjangoFilterBackend',
), ),
} }
CACHES = {
'default': {
'BACKEND': 'speedinfo.backends.proxy_cache',
'CACHE_BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}
ROOT_URLCONF = 'sapl.urls' ROOT_URLCONF = 'sapl.urls'

Loading…
Cancel
Save