| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.conf import settings |
| 4 |
from django.core.exceptions import ImproperlyConfigured |
| 5 |
from django.http import HttpRequest, HttpResponseRedirect |
| 6 |
from django.urls import reverse |
| 7 |
from django.utils.http import urlencode |
| 8 |
|
| 9 |
from openid.consumer.discover import DiscoveryFailure |
| 10 |
|
| 11 |
from allauth.socialaccount.adapter import get_adapter |
| 12 |
from allauth.socialaccount.helpers import render_authentication_error |
| 13 |
from allauth.socialaccount.providers.openid.provider import ( |
| 14 |
OpenIDAccount, |
| 15 |
OpenIDProvider, |
| 16 |
) |
| 17 |
from allauth.socialaccount.providers.openid.views import _openid_consumer |
| 18 |
|
| 19 |
|
| 20 |
if "allauth.socialaccount.providers.openid" not in settings.INSTALLED_APPS: |
| 21 |
raise ImproperlyConfigured( |
| 22 |
"The steam provider requires 'allauth.socialaccount.providers.openid' to be installed" |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
STEAM_OPENID_URL = "https://steamcommunity.com/openid" |
| 27 |
|
| 28 |
|
| 29 |
class SteamAccount(OpenIDAccount): |
| 30 |
def to_str(self): |
| 31 |
dflt = super().to_str() |
| 32 |
return self.account.extra_data.get("personaname", dflt) |
| 33 |
|
| 34 |
def get_profile_url(self): |
| 35 |
return self.account.extra_data.get("profileurl") |
| 36 |
|
| 37 |
def get_avatar_url(self): |
| 38 |
return ( |
| 39 |
self.account.extra_data.get("avatarfull") |
| 40 |
or self.account.extra_data.get("avatarmedium") |
| 41 |
or self.account.extra_data.get("avatar") |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
def extract_steam_id(url): |
| 46 |
prefix = "https://steamcommunity.com/openid/id/" |
| 47 |
if not url.startswith(prefix): |
| 48 |
raise ValueError(url) |
| 49 |
return url[len(prefix) :] |
| 50 |
|
| 51 |
|
| 52 |
def request_steam_account_summary(api_key, steam_id): |
| 53 |
api_base = "https://api.steampowered.com/" |
| 54 |
method = "ISteamUser/GetPlayerSummaries/v0002/" |
| 55 |
params = {"key": api_key, "steamids": steam_id} |
| 56 |
|
| 57 |
with get_adapter().get_requests_session() as sess: |
| 58 |
resp = sess.get(api_base + method, params=params) |
| 59 |
resp.raise_for_status() |
| 60 |
data = resp.json() |
| 61 |
|
| 62 |
playerlist = data.get("response", {}).get("players", []) |
| 63 |
return playerlist[0] if playerlist else {"steamid": steam_id} |
| 64 |
|
| 65 |
|
| 66 |
class SteamOpenIDProvider(OpenIDProvider): |
| 67 |
id = "steam" |
| 68 |
name = "Steam" |
| 69 |
account_class = SteamAccount |
| 70 |
uses_apps = True |
| 71 |
supports_redirect = True |
| 72 |
|
| 73 |
def __init__(self, request: HttpRequest, app=None) -> None: |
| 74 |
if app is None: |
| 75 |
app = get_adapter().get_app(request, self.id) |
| 76 |
super().__init__(request, app=app) |
| 77 |
|
| 78 |
def get_login_url(self, request: HttpRequest, **kwargs): |
| 79 |
url = reverse("steam_login") |
| 80 |
if kwargs: |
| 81 |
url += f"?{urlencode(kwargs)}" |
| 82 |
return url |
| 83 |
|
| 84 |
def sociallogin_from_response(self, request: HttpRequest, response): |
| 85 |
steam_id = extract_steam_id(response.identity_url) |
| 86 |
steam_api_key = self.app.secret |
| 87 |
response._extra = request_steam_account_summary(steam_api_key, steam_id) |
| 88 |
return super().sociallogin_from_response(request, response) |
| 89 |
|
| 90 |
def extract_uid(self, response): |
| 91 |
return response._extra["steamid"] |
| 92 |
|
| 93 |
def extract_extra_data(self, response): |
| 94 |
return response._extra.copy() |
| 95 |
|
| 96 |
def extract_common_fields(self, response): |
| 97 |
full_name = response._extra.get("realname", "").strip() |
| 98 |
if full_name.count(" ") == 1: |
| 99 |
first_name, last_name = full_name.split() |
| 100 |
else: |
| 101 |
first_name, last_name = full_name, "" |
| 102 |
|
| 103 |
username = response._extra.get("personaname", "") |
| 104 |
|
| 105 |
return { |
| 106 |
"username": username or response._extra["steamid"], |
| 107 |
"first_name": first_name, |
| 108 |
"last_name": last_name, |
| 109 |
"full_name": full_name, |
| 110 |
} |
| 111 |
|
| 112 |
def get_realm(self, request: HttpRequest): |
| 113 |
return self.get_settings().get("REALM", request.build_absolute_uri("/")) |
| 114 |
|
| 115 |
def redirect( |
| 116 |
self, request: HttpRequest, process, next_url=None, data=None, **kwargs |
| 117 |
): |
| 118 |
endpoint = STEAM_OPENID_URL |
| 119 |
realm = self.get_realm(request) |
| 120 |
try: |
| 121 |
client = _openid_consumer(request, self, endpoint) |
| 122 |
auth_request = client.begin(endpoint) |
| 123 |
except (UnicodeDecodeError, DiscoveryFailure) as e: |
| 124 |
return render_authentication_error(request, self, exception=e) |
| 125 |
|
| 126 |
self.stash_redirect_state(request, process, next_url, data, **kwargs) |
| 127 |
|
| 128 |
redirect_url = auth_request.redirectURL( |
| 129 |
realm, request.build_absolute_uri(reverse("steam_callback")) |
| 130 |
) |
| 131 |
return HttpResponseRedirect(redirect_url) |
| 132 |
|
| 133 |
|
| 134 |
provider_classes = [SteamOpenIDProvider] |