2020-07-05 23:40:39 +08:00
|
|
|
# The core application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/7/5
|
|
|
|
|
|
|
|
# Copyright (c) 2020 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-07-08 07:24:36 +08:00
|
|
|
"""The HTTP digest authentication utilities of the Mia core
|
|
|
|
application.
|
2020-07-05 23:40:39 +08:00
|
|
|
|
|
|
|
"""
|
2020-08-10 02:39:41 +08:00
|
|
|
import ipaddress
|
|
|
|
import socket
|
2020-07-06 22:26:29 +08:00
|
|
|
from functools import wraps
|
2020-08-13 08:05:35 +08:00
|
|
|
from typing import Optional
|
2020-07-06 22:26:29 +08:00
|
|
|
|
2020-08-11 11:27:46 +08:00
|
|
|
from django.conf import settings
|
2020-08-10 02:39:41 +08:00
|
|
|
from django.db.models import F
|
|
|
|
from django.db.models.functions import Now
|
|
|
|
from django.http import HttpResponse, HttpRequest
|
|
|
|
from geoip import geolite2
|
2020-07-06 22:26:29 +08:00
|
|
|
|
2020-08-10 02:39:41 +08:00
|
|
|
from .models import User, Country
|
2020-07-04 08:17:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AccountBackend:
|
2020-07-05 23:40:39 +08:00
|
|
|
"""The account backend for the django-digest module."""
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def get_partial_digest(self, username: str) -> Optional[str]:
|
2020-07-05 23:40:39 +08:00
|
|
|
"""Returns the HTTP digest authentication password digest hash
|
|
|
|
of a user.
|
|
|
|
|
|
|
|
Args:
|
2020-08-13 08:05:35 +08:00
|
|
|
username: The log in user name.
|
2020-07-05 23:40:39 +08:00
|
|
|
|
|
|
|
Return:
|
2020-08-13 08:05:35 +08:00
|
|
|
The HTTP digest authentication password hash of the user, or None
|
|
|
|
if the user does not exist.
|
2020-07-05 23:40:39 +08:00
|
|
|
"""
|
|
|
|
user = User.objects.filter(login_id=username).first()
|
|
|
|
if user is None:
|
2020-07-04 08:17:08 +08:00
|
|
|
return None
|
2020-07-05 23:40:39 +08:00
|
|
|
return user.password
|
2020-07-04 08:17:08 +08:00
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def get_user(self, username: str) -> Optional[User]:
|
2020-07-05 23:40:39 +08:00
|
|
|
"""Returns the user by her log in user name.
|
|
|
|
|
|
|
|
Args:
|
2020-08-13 10:17:52 +08:00
|
|
|
username: The log in user name.
|
2020-07-05 23:40:39 +08:00
|
|
|
|
|
|
|
Return:
|
2020-08-13 10:17:52 +08:00
|
|
|
The user, or None if the user does not exist.
|
2020-07-05 23:40:39 +08:00
|
|
|
"""
|
|
|
|
return User.objects.filter(login_id=username).first()
|
2020-07-06 22:26:29 +08:00
|
|
|
|
|
|
|
|
2020-07-30 00:12:13 +08:00
|
|
|
def login_required(function=None):
|
2020-07-06 22:26:29 +08:00
|
|
|
"""The decorator to check if the user has logged in, and send
|
|
|
|
HTTP 401 if the user has not logged in.
|
|
|
|
"""
|
|
|
|
def decorator(view_func):
|
|
|
|
@wraps(view_func)
|
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if request.user.is_anonymous:
|
|
|
|
return HttpResponse(status=401)
|
2020-07-06 23:04:14 +08:00
|
|
|
if "logout" in request.session:
|
|
|
|
del request.session["logout"]
|
2020-08-10 02:39:41 +08:00
|
|
|
if "visit_logged" in request.session:
|
|
|
|
del request.session["visit_logged"]
|
2020-07-06 23:04:14 +08:00
|
|
|
return HttpResponse(status=401)
|
2020-08-11 11:27:46 +08:00
|
|
|
if not settings.DEBUG:
|
|
|
|
_log_visit(request)
|
2020-07-06 22:26:29 +08:00
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
return _wrapped_view
|
|
|
|
if function:
|
|
|
|
return decorator(function)
|
|
|
|
return decorator
|
2020-08-10 02:39:41 +08:00
|
|
|
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def _log_visit(request: HttpRequest) -> None:
|
2020-08-10 02:39:41 +08:00
|
|
|
"""Logs the visit information for the logged-in user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
|
|
|
"""
|
|
|
|
if "visit_logged" in request.session:
|
|
|
|
return
|
|
|
|
user = request.user
|
2020-08-11 11:25:04 +08:00
|
|
|
ip = _get_remote_ip(request)
|
2020-08-10 02:39:41 +08:00
|
|
|
User.objects.filter(pk=user.pk).update(
|
|
|
|
visits=F("visits") + 1,
|
|
|
|
visited_at=Now(),
|
|
|
|
visited_ip=ip,
|
|
|
|
visited_host=_get_host(ip),
|
|
|
|
visited_country=_get_country(ip),
|
|
|
|
)
|
|
|
|
request.session["visit_logged"] = True
|
|
|
|
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def _get_remote_ip(request: HttpRequest) -> str:
|
|
|
|
"""Returns the IP of the remote client.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request: The request.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The IP of the remote client.
|
|
|
|
"""
|
2020-08-11 11:25:04 +08:00
|
|
|
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
|
|
|
if x_forwarded_for:
|
|
|
|
return x_forwarded_for.split(",")[0]
|
|
|
|
return request.META.get('REMOTE_ADDR')
|
|
|
|
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def _get_host(ip: str) -> Optional[str]:
|
2020-08-10 02:39:41 +08:00
|
|
|
"""Look-up the host name by its IP.
|
|
|
|
|
|
|
|
Args:
|
2020-08-13 08:05:35 +08:00
|
|
|
ip: The IP
|
2020-08-10 02:39:41 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-13 08:05:35 +08:00
|
|
|
The host name, or None if the look-up fails.
|
2020-08-10 02:39:41 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return socket.gethostbyaddr(ip)[0]
|
2020-08-13 10:17:52 +08:00
|
|
|
except socket.herror:
|
|
|
|
return None
|
|
|
|
except socket.gaierror:
|
2020-08-10 02:39:41 +08:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def _get_country(ip: str) -> Optional[Country]:
|
2020-08-10 02:39:41 +08:00
|
|
|
"""Look-up the country by its IP.
|
|
|
|
|
|
|
|
Args:
|
2020-08-13 08:05:35 +08:00
|
|
|
ip: The IP
|
2020-08-10 02:39:41 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-13 08:05:35 +08:00
|
|
|
The country.
|
2020-08-10 02:39:41 +08:00
|
|
|
"""
|
|
|
|
code = _get_country_code(ip)
|
|
|
|
try:
|
|
|
|
return Country.objects.get(code=code)
|
|
|
|
except Country.DoesNotExist:
|
2020-08-13 08:05:35 +08:00
|
|
|
return None
|
2020-08-10 02:39:41 +08:00
|
|
|
|
|
|
|
|
2020-08-13 08:05:35 +08:00
|
|
|
def _get_country_code(ip: str) -> Optional[str]:
|
2020-08-10 02:39:41 +08:00
|
|
|
"""Look-up the country code by its IP.
|
|
|
|
|
|
|
|
Args:
|
2020-08-13 08:05:35 +08:00
|
|
|
ip: The IP
|
2020-08-10 02:39:41 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-13 08:05:35 +08:00
|
|
|
The country code, or None if the look-up fails.
|
2020-08-10 02:39:41 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return geolite2.lookup(ip).country
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
ipaddr = ipaddress.ip_address(ip)
|
|
|
|
if ipaddr.is_private:
|
|
|
|
return "AA"
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return None
|