Commit e2691fd0 authored by David Haynes's avatar David Haynes 🙆
Browse files

More sane handling of getting session information

- /auth/status to get all the juicy deets
parent 04796779
Pipeline #3456 passed with stage
in 1 minute and 10 seconds
...@@ -7,6 +7,7 @@ name = "pypi" ...@@ -7,6 +7,7 @@ name = "pypi"
pylint = "*" pylint = "*"
pylint-django = "*" pylint-django = "*"
coverage = "*" coverage = "*"
black = "*"
[packages] [packages]
django = "<2.1,>=2.0" django = "<2.1,>=2.0"
......
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "bdea5532bb3583afe0508086a4acd7186b5c21fad2de7f9bcd73dd0381ee51e7" "sha256": "ff4400988dabd456ef6af790269f309a1e60eb9801a14defa18ae693e724e346"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
"default": { "default": {
"certifi": { "certifi": {
"hashes": [ "hashes": [
"sha256:339dc09518b07e2fa7eda5450740925974815557727d6bd35d319c1524a04a4c", "sha256:47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7",
"sha256:6d58c986d22b038c8c0df30d639f23a3e6d172a05c3583e766f4c0b785c0986a" "sha256:993f830721089fef441cdfeb4b2c8c9df86f0c63239f06bd025a76a7daddb033"
], ],
"version": "==2018.10.15" "version": "==2018.11.29"
}, },
"chardet": { "chardet": {
"hashes": [ "hashes": [
...@@ -93,85 +93,114 @@ ...@@ -93,85 +93,114 @@
}, },
"mysqlclient": { "mysqlclient": {
"hashes": [ "hashes": [
"sha256:ff8ee1be84215e6c30a746b728c41eb0701a46ca76e343af445b35ce6250644f" "sha256:062d78953acb23066c0387a8f3bd0ecf946626f599145bb7fd201460e8f773e1",
"sha256:3981ae9ce545901a36a8b7aed76ed02960a429f75dc53b7ad77fb2f9ab7cd56b",
"sha256:b3591a00c0366de71d65108627899710d9cfb00e575c4d211aa8de59b1f130c9"
], ],
"index": "pypi", "index": "pypi",
"version": "==1.3.13" "version": "==1.3.14"
}, },
"pytz": { "pytz": {
"hashes": [ "hashes": [
"sha256:a061aa0a9e06881eb8b3b2b43f05b9439d6583c206d0a6c340ff72a7b6669053", "sha256:31cb35c89bd7d333cd32c5f278fca91b523b0834369e757f4c5641ea252236ca",
"sha256:ffb9ef1de172603304d9d2819af6f5ece76f2e85ec10692a524dd876e72bf277" "sha256:8e0f8568c118d3077b46be7d654cc8167fa916092e28320cde048e54bfc9f1e6"
], ],
"version": "==2018.5" "version": "==2018.7"
}, },
"redis": { "redis": {
"hashes": [ "hashes": [
"sha256:8a1900a9f2a0a44ecf6e8b5eb3e967a9909dfed219ad66df094f27f7d6f330fb", "sha256:2100750629beff143b6a200a2ea8e719fcf26420adabb81402895e144c5083cf",
"sha256:a22ca993cea2962dbb588f9f30d0015ac4afcc45bee27d3978c0dbe9e97c6c0f" "sha256:8e0bdd2de02e829b6225b25646f9fb9daffea99a252610d040409a6738541f0a"
], ],
"version": "==2.10.6" "version": "==3.0.1"
}, },
"requests": { "requests": {
"hashes": [ "hashes": [
"sha256:99dcfdaaeb17caf6e526f32b6a7b780461512ab3f1d992187801694cba42770c", "sha256:65b3a120e4329e33c9889db89c80976c5272f56ea92d3e74da8a463992e3ff54",
"sha256:a84b8c9ab6239b578f22d1c21d51b696dcfe004032bb80ea832398d6909d7279" "sha256:ea881206e59f41dbd0bd445437d792e43906703fff75ca8ff43ccdb11f33f263"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.20.0" "version": "==2.20.1"
}, },
"urllib3": { "urllib3": {
"hashes": [ "hashes": [
"sha256:41c3db2fc01e5b907288010dec72f9d0a74e37d6994e6eb56849f59fea2265ae", "sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39",
"sha256:8819bba37a02d143296a4d032373c4dd4aca11f6d4c9973335ca75f9c8475f59" "sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"
], ],
"version": "==1.24" "version": "==1.24.1"
} }
}, },
"develop": { "develop": {
"appdirs": {
"hashes": [
"sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92",
"sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e"
],
"version": "==1.4.3"
},
"astroid": { "astroid": {
"hashes": [ "hashes": [
"sha256:292fa429e69d60e4161e7612cb7cc8fa3609e2e309f80c224d93a76d5e7b58be", "sha256:35b032003d6a863f5dcd7ec11abd5cd5893428beaa31ab164982403bcb311f22",
"sha256:c7013d119ec95eb626f7a2011f0b63d0c9a095df9ad06d8507b37084eada1a8d" "sha256:6a5d668d7dc69110de01cdf7aeec69a679ef486862a0850cc0fd5571505b6b7e"
], ],
"version": "==2.0.4" "version": "==2.1.0"
},
"attrs": {
"hashes": [
"sha256:10cbf6e27dbce8c30807caf056c8eb50917e0eaafe86347671b57254006c3e69",
"sha256:ca4be454458f9dec299268d472aaa5a11f67a4ff70093396e1ceae9c76cf4bbb"
],
"version": "==18.2.0"
},
"black": {
"hashes": [
"sha256:817243426042db1d36617910df579a54f1afd659adb96fc5032fcf4b36209739",
"sha256:e030a9a28f542debc08acceb273f228ac422798e5215ba2a791a6ddeaaca22a5"
],
"index": "pypi",
"version": "==18.9b0"
},
"click": {
"hashes": [
"sha256:2335065e6395b9e67ca716de5f7526736bfa6ceead690adf616d925bdc622b13",
"sha256:5b94b49521f6456670fdb30cd82a4eca9412788a93fa6dd6df72c94d5a8ff2d7"
],
"version": "==7.0"
}, },
"coverage": { "coverage": {
"hashes": [ "hashes": [
"sha256:03481e81d558d30d230bc12999e3edffe392d244349a90f4ef9b88425fac74ba", "sha256:029c69deaeeeae1b15bc6c59f0ffa28aa8473721c614a23f2c2976dec245cd12",
"sha256:0b136648de27201056c1869a6c0d4e23f464750fd9a9ba9750b8336a244429ed", "sha256:02abbbebc6e9d5abe13cd28b5e963dedb6ffb51c146c916d17b18f141acd9947",
"sha256:10a46017fef60e16694a30627319f38a2b9b52e90182dddb6e37dcdab0f4bf95", "sha256:1bbfe5b82a3921d285e999c6d256c1e16b31c554c29da62d326f86c173d30337",
"sha256:198626739a79b09fa0a2f06e083ffd12eb55449b5f8bfdbeed1df4910b2ca640", "sha256:210c02f923df33a8d0e461c86fdcbbb17228ff4f6d92609fc06370a98d283c2d",
"sha256:23d341cdd4a0371820eb2b0bd6b88f5003a7438bbedb33688cd33b8eae59affd", "sha256:2d0807ba935f540d20b49d5bf1c0237b90ce81e133402feda906e540003f2f7a",
"sha256:28b2191e7283f4f3568962e373b47ef7f0392993bb6660d079c62bd50fe9d162", "sha256:35d7a013874a7c927ce997350d314144ffc5465faf787bb4e46e6c4f381ef562",
"sha256:2a5b73210bad5279ddb558d9a2bfedc7f4bf6ad7f3c988641d83c40293deaec1", "sha256:3636f9d0dcb01aed4180ef2e57a4e34bb4cac3ecd203c2a23db8526d86ab2fb4",
"sha256:2eb564bbf7816a9d68dd3369a510be3327f1c618d2357fa6b1216994c2e3d508", "sha256:42f4be770af2455a75e4640f033a82c62f3fb0d7a074123266e143269d7010ef",
"sha256:337ded681dd2ef9ca04ef5d93cfc87e52e09db2594c296b4a0a3662cb1b41249", "sha256:48440b25ba6cda72d4c638f3a9efa827b5b87b489c96ab5f4ff597d976413156",
"sha256:3a2184c6d797a125dca8367878d3b9a178b6fdd05fdc2d35d758c3006a1cd694", "sha256:4dac8dfd1acf6a3ac657475dfdc66c621f291b1b7422a939cc33c13ac5356473",
"sha256:3c79a6f7b95751cdebcd9037e4d06f8d5a9b60e4ed0cd231342aa8ad7124882a", "sha256:4e8474771c69c2991d5eab65764289a7dd450bbea050bc0ebb42b678d8222b42",
"sha256:3d72c20bd105022d29b14a7d628462ebdc61de2f303322c0212a054352f3b287", "sha256:551f10ddfeff56a1325e5a34eff304c5892aa981fd810babb98bfee77ee2fb17",
"sha256:3eb42bf89a6be7deb64116dd1cc4b08171734d721e7a7e57ad64cc4ef29ed2f1", "sha256:5b104982f1809c1577912519eb249f17d9d7e66304ad026666cb60a5ef73309c",
"sha256:4635a184d0bbe537aa185a34193898eee409332a8ccb27eea36f262566585000", "sha256:5c62aef73dfc87bfcca32cee149a1a7a602bc74bac72223236b0023543511c88",
"sha256:56e448f051a201c5ebbaa86a5efd0ca90d327204d8b059ab25ad0f35fbfd79f1", "sha256:633151f8d1ad9467b9f7e90854a7f46ed8f2919e8bc7d98d737833e8938fc081",
"sha256:5a13ea7911ff5e1796b6d5e4fbbf6952381a611209b736d48e675c2756f3f74e", "sha256:772207b9e2d5bf3f9d283b88915723e4e92d9a62c83f44ec92b9bd0cd685541b",
"sha256:69bf008a06b76619d3c3f3b1983f5145c75a305a0fea513aca094cae5c40a8f5", "sha256:7d5e02f647cd727afc2659ec14d4d1cc0508c47e6cfb07aea33d7aa9ca94d288",
"sha256:6bc583dc18d5979dc0f6cec26a8603129de0304d5ae1f17e57a12834e7235062", "sha256:a9798a4111abb0f94584000ba2a2c74841f2cfe5f9254709756367aabbae0541",
"sha256:701cd6093d63e6b8ad7009d8a92425428bc4d6e7ab8d75efbb665c806c1d79ba", "sha256:b38ea741ab9e35bfa7015c93c93bbd6a1623428f97a67083fc8ebd366238b91f",
"sha256:7608a3dd5d73cb06c531b8925e0ef8d3de31fed2544a7de6c63960a1e73ea4bc", "sha256:b6a5478c904236543c0347db8a05fac6fc0bd574c870e7970faa88e1d9890044",
"sha256:76ecd006d1d8f739430ec50cc872889af1f9c1b6b8f48e29941814b09b0fd3cc", "sha256:c6248bfc1de36a3844685a2e10ba17c18119ba6252547f921062a323fb31bff1",
"sha256:7aa36d2b844a3e4a4b356708d79fd2c260281a7390d678a10b91ca595ddc9e99", "sha256:c705ab445936457359b1424ef25ccc0098b0491b26064677c39f1d14a539f056",
"sha256:7d3f553904b0c5c016d1dad058a7554c7ac4c91a789fca496e7d8347ad040653", "sha256:d95a363d663ceee647291131dbd213af258df24f41350246842481ec3709bd33",
"sha256:7e1fe19bd6dce69d9fd159d8e4a80a8f52101380d5d3a4d374b6d3eae0e5de9c", "sha256:e27265eb80cdc5dab55a40ef6f890e04ecc618649ad3da5265f128b141f93f78",
"sha256:8c3cb8c35ec4d9506979b4cf90ee9918bc2e49f84189d9bf5c36c0c1119c6558", "sha256:ebc276c9cb5d917bd2ae959f84ffc279acafa9c9b50b0fa436ebb70bbe2166ea",
"sha256:9d6dd10d49e01571bf6e147d3b505141ffc093a06756c60b053a859cb2128b1f", "sha256:f4d229866d030863d0fe3bf297d6d11e6133ca15bbb41ed2534a8b9a3d6bd061",
"sha256:be6cfcd8053d13f5f5eeb284aa8a814220c3da1b0078fa859011c7fffd86dab9", "sha256:f95675bd88b51474d4fe5165f3266f419ce754ffadfb97f10323931fa9ac95e5",
"sha256:c1bb572fab8208c400adaf06a8133ac0712179a334c09224fb11393e920abcdd", "sha256:f95bc54fb6d61b9f9ff09c4ae8ff6a3f5edc937cda3ca36fc937302a7c152bf1",
"sha256:de4418dadaa1c01d497e539210cb6baa015965526ff5afc078c57ca69160108d", "sha256:fd0f6be53de40683584e5331c341e65a679dbe5ec489a0697cec7c2ef1a48cda"
"sha256:e05cb4d9aad6233d67e0541caa7e511fa4047ed7750ec2510d466e806e0255d6",
"sha256:f3f501f345f24383c0000395b26b726e46758b71393267aeae0bd36f8b3ade80"
], ],
"index": "pypi", "index": "pypi",
"version": "==4.5.1" "version": "==5.0a4"
}, },
"isort": { "isort": {
"hashes": [ "hashes": [
...@@ -224,19 +253,18 @@ ...@@ -224,19 +253,18 @@
}, },
"pylint": { "pylint": {
"hashes": [ "hashes": [
"sha256:1d6d3622c94b4887115fe5204982eee66fdd8a951cf98635ee5caee6ec98c3ec", "sha256:689de29ae747642ab230c6d37be2b969bf75663176658851f456619aacf27492",
"sha256:31142f764d2a7cd41df5196f9933b12b7ee55e73ef12204b648ad7e556c119fb" "sha256:771467c434d0d9f081741fec1d64dfb011ed26e65e12a28fe06ca2f61c4d556c"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.1.1" "version": "==2.2.2"
}, },
"pylint-django": { "pylint-django": {
"hashes": [ "hashes": [
"sha256:5dc5f85caef2c5f9e61622b9cbd89d94edd3dcf546939b2974d18de4fa90d676", "sha256:f2899a0f33c9bede0c9912ade9a57bbfc4b400b4912c59bea055353f9d2ff56f"
"sha256:bf313f10b68ed915a34f0f475cc9ff8c7f574a95302beb48b79c5993f7efd84c"
], ],
"index": "pypi", "index": "pypi",
"version": "==2.0.2" "version": "==2.0.4"
}, },
"pylint-plugin-utils": { "pylint-plugin-utils": {
"hashes": [ "hashes": [
...@@ -246,10 +274,17 @@ ...@@ -246,10 +274,17 @@
}, },
"six": { "six": {
"hashes": [ "hashes": [
"sha256:70e8a77beed4562e7f14fe23a786b54f6296e34344c23bc42f07b15018ff98e9", "sha256:3350809f0555b11f552448330d0b52d5f24c91a322ea4a15ef22629740f3761c",
"sha256:832dc0e10feb1aa2c68dcc57dbb658f1c7e65b9b61af69048abc87a2db00a0eb" "sha256:d16a0141ec1a18405cd4ce8b4613101da75da0e9a7aec5bdd4fa804d0e0eba73"
],
"version": "==1.12.0"
},
"toml": {
"hashes": [
"sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c",
"sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e"
], ],
"version": "==1.11.0" "version": "==0.10.0"
}, },
"wrapt": { "wrapt": {
"hashes": [ "hashes": [
......
...@@ -4,29 +4,45 @@ import { Button } from "reactstrap"; ...@@ -4,29 +4,45 @@ import { Button } from "reactstrap";
class AuthButton extends React.Component { class AuthButton extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { is_auth: false }; this.state = { error: null, is_auth: false };
} }
componentDidMount() { componentDidMount() {
this.setState(() => { fetch("/auth/status")
return { is_auth: window.django.user.is_authenticated == "True" }; .then(res => res.json())
}); .then(
result => {
this.setState({
is_auth: result.is_authenticated
});
},
error => {
this.setState({
error
});
}
);
} }
render() { render() {
return ( const { is_auth, error } = this.state;
<div> if (error) {
{this.state.is_auth ? ( return <div>Error: {error.message}</div>;
<Button color="info" href="/auth/logout"> } else {
Logout return (
</Button> <div>
) : ( {is_auth ? (
<Button color="info" href="/auth/login"> <Button color="info" href="/auth/logout">
Login Logout
</Button> </Button>
)} ) : (
</div> <Button color="info" href="/auth/login">
); Login
</Button>
)}
</div>
);
}
} }
} }
......
{% load static %} {% load static %}
<html> <html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#006633" />
<meta name="description" content="University-branded URL shortening" />
<title>Welcome &bull; SRCT Go</title>
</head>
<head> <body>
<meta charset="utf-8"> <!-- React injects itself here -->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <div id="root"></div>
<meta name="theme-color" content="#006633" /> </body>
<meta name="description" content="University-branded URL shortening" />
<title>Welcome &bull; SRCT Go</title>
</head>
<body> <script src="static/main.js"></script>
<!-- React injects itself here --> </html>
<div id="root"></div>
</body>
<script>
window.django = {
logout: "{% url "cas_logout" %}",
user: {
username: "{{ request.user.username }}",
full_name: "{{ request.user.get_full_name }}",
last_login: "{{ request.user.last_login }}",
is_authenticated: "{{ request.user.is_authenticated }}"
}
};
</script>
<script src="static/main.js"></script>
</html>
\ No newline at end of file
...@@ -4,17 +4,15 @@ settings/urls.py ...@@ -4,17 +4,15 @@ settings/urls.py
The URLs of the project and their associated view that requests are routed to. The URLs of the project and their associated view that requests are routed to.
""" """
# Django Imports # Django Imports
from django.urls import path, re_path, include from django.urls import path, include
from django.contrib import admin from django.contrib import admin
from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView
# App Imports
from . import views
from cas import views as cas_views
# Third Party # Third Party
from rest_framework import routers from rest_framework import routers
from cas import views as cas_views
# App Imports
from . import views
router = routers.DefaultRouter() router = routers.DefaultRouter()
router.register(r"golinks", views.URLViewSet, base_name="golinks") router.register(r"golinks", views.URLViewSet, base_name="golinks")
...@@ -32,7 +30,8 @@ urlpatterns = [ ...@@ -32,7 +30,8 @@ urlpatterns = [
# /admin - Administrator interface. # /admin - Administrator interface.
path("admin/", admin.site.urls, name="go_admin"), path("admin/", admin.site.urls, name="go_admin"),
path("auth/", include("rest_framework.urls")), path("auth/", include("rest_framework.urls")),
path("auth/token/", views.CustomAuthToken.as_view()) path("auth/token/", views.CustomAuthToken.as_view()),
path("auth/status/", views.GetSessionInfo.as_view())
# # /view/<short> - View URL data. Cached for 15 minutes # # /view/<short> - View URL data. Cached for 15 minutes
# re_path(r'^view/(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$', # re_path(r'^view/(?P<short>([\U00010000-\U0010ffff][\U0000200D]?)+)$',
# cache_page(60 * 15)(go.views.view), name='view'), # cache_page(60 * 15)(go.views.view), name='view'),
......
...@@ -4,15 +4,16 @@ go/views.py ...@@ -4,15 +4,16 @@ go/views.py
The functions that handle a request to a given URL. Get some data, manipulate The functions that handle a request to a given URL. Get some data, manipulate
it, and return a rendered template. it, and return a rendered template.
""" """
from rest_framework import viewsets from rest_framework import viewsets, permissions
from rest_framework import permissions
from rest_framework.authentication import TokenAuthentication, SessionAuthentication from rest_framework.authentication import TokenAuthentication, SessionAuthentication
from .serializers import URLSerializer
from .models import URL
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.authtoken.models import Token from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from rest_framework.authtoken.views import ObtainAuthToken
from .serializers import URLSerializer
from .models import URL
class URLPermission(permissions.BasePermission): class URLPermission(permissions.BasePermission):
...@@ -46,11 +47,6 @@ class URLViewSet(viewsets.ModelViewSet): ...@@ -46,11 +47,6 @@ class URLViewSet(viewsets.ModelViewSet):
serializer.save(owner=self.request.user.registereduser) serializer.save(owner=self.request.user.registereduser)
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
class CustomAuthToken(ObtainAuthToken): class CustomAuthToken(ObtainAuthToken):
authentication_classes = (SessionAuthentication,) authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,) permission_classes = (IsAuthenticated,)
...@@ -59,3 +55,20 @@ class CustomAuthToken(ObtainAuthToken): ...@@ -59,3 +55,20 @@ class CustomAuthToken(ObtainAuthToken):
token, created = Token.objects.get_or_create(user=request.user) token, created = Token.objects.get_or_create(user=request.user)
return Response({"token": token.key}) return Response({"token": token.key})
class GetSessionInfo(APIView):
"""Handy endpoint to return current user session status & information to the frontend."""
authentication_classes = (SessionAuthentication,)
permission_classes = (IsAuthenticated,)
def get(self, request, *args, **kwargs):
token, created = Token.objects.get_or_create(user=request.user)
session_info = {
"username": request.user.username,
# "full_name": f"{request.user.get_full_name}",
"last_login": request.user.last_login,
"is_authenticated": request.user.is_authenticated,
"token": token.key,
}
return Response(session_info)
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment