| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import requests |
| 4 |
from hashlib import md5 |
| 5 |
|
| 6 |
from django.http import HttpRequest, HttpResponseRedirect |
| 7 |
from django.urls import reverse |
| 8 |
from django.utils.http import urlencode |
| 9 |
from django.views.decorators.csrf import csrf_exempt |
| 10 |
|
| 11 |
from allauth.socialaccount.adapter import get_adapter |
| 12 |
from allauth.socialaccount.helpers import ( |
| 13 |
complete_social_login, |
| 14 |
render_authentication_error, |
| 15 |
) |
| 16 |
from allauth.socialaccount.models import SocialLogin, SocialToken |
| 17 |
from allauth.socialaccount.providers.draugiem.provider import DraugiemProvider |
| 18 |
|
| 19 |
from ..base import AuthError |
| 20 |
|
| 21 |
|
| 22 |
class DraugiemApiError(Exception): |
| 23 |
pass |
| 24 |
|
| 25 |
|
| 26 |
ACCESS_TOKEN_URL = "https://api.draugiem.lv/json" # nosec |
| 27 |
AUTHORIZE_URL = "https://api.draugiem.lv/authorize" |
| 28 |
|
| 29 |
|
| 30 |
def login(request: HttpRequest): |
| 31 |
app = get_adapter().get_app(request, DraugiemProvider.id) |
| 32 |
redirect_url = request.build_absolute_uri(reverse(callback)) |
| 33 |
# Draugiem mandates a weak hashing algorithm. |
| 34 |
redirect_url_hash = md5(f"{app.secret}{redirect_url}".encode()).hexdigest() # nosec |
| 35 |
params = { |
| 36 |
"app": app.client_id, |
| 37 |
"hash": redirect_url_hash, |
| 38 |
"redirect": redirect_url, |
| 39 |
} |
| 40 |
SocialLogin.stash_state(request) |
| 41 |
return HttpResponseRedirect(f"{AUTHORIZE_URL}?{urlencode(params)}") |
| 42 |
|
| 43 |
|
| 44 |
@csrf_exempt |
| 45 |
def callback(request: HttpRequest): |
| 46 |
adapter = get_adapter() |
| 47 |
provider = adapter.get_provider(request, DraugiemProvider.id) |
| 48 |
|
| 49 |
if "dr_auth_status" not in request.GET: |
| 50 |
return render_authentication_error(request, provider, error=AuthError.UNKNOWN) |
| 51 |
|
| 52 |
if request.GET["dr_auth_status"] != "ok": |
| 53 |
return render_authentication_error(request, provider, error=AuthError.DENIED) |
| 54 |
|
| 55 |
if "dr_auth_code" not in request.GET: |
| 56 |
return render_authentication_error(request, provider, error=AuthError.UNKNOWN) |
| 57 |
|
| 58 |
ret = None |
| 59 |
auth_exception = None |
| 60 |
try: |
| 61 |
app = provider.app |
| 62 |
login = draugiem_complete_login(request, app, request.GET["dr_auth_code"]) |
| 63 |
login.state = SocialLogin.unstash_state(request) |
| 64 |
|
| 65 |
ret = complete_social_login(request, login) |
| 66 |
except (requests.RequestException, DraugiemApiError) as e: |
| 67 |
auth_exception = e |
| 68 |
|
| 69 |
if not ret: |
| 70 |
ret = render_authentication_error(request, provider, exception=auth_exception) |
| 71 |
|
| 72 |
return ret |
| 73 |
|
| 74 |
|
| 75 |
def draugiem_complete_login(request: HttpRequest, app, code): |
| 76 |
provider = get_adapter().get_provider(request, DraugiemProvider.id) |
| 77 |
with get_adapter().get_requests_session() as sess: |
| 78 |
params = {"action": "authorize", "app": app.secret, "code": code} |
| 79 |
response = sess.get(ACCESS_TOKEN_URL, params=params) |
| 80 |
response.raise_for_status() |
| 81 |
response_json = response.json() |
| 82 |
|
| 83 |
if "error" in response_json: |
| 84 |
raise DraugiemApiError(response_json["error"]) |
| 85 |
|
| 86 |
token = SocialToken(app=app, token=response_json["apikey"]) |
| 87 |
|
| 88 |
login = provider.sociallogin_from_response(request, response_json) |
| 89 |
login.token = token |
| 90 |
return login |