secret
Created

  • Download Gist
steam.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
"""Steam OpenId support"""
import re
import urllib
import urllib2
 
from django.utils import simplejson
 
from social_auth.backends import OpenIdAuth, OpenIDBackend, USERNAME
from social_auth.exceptions import AuthFailed
from social_auth.utils import setting
 
 
STEAM_ID = re.compile('steamcommunity.com/openid/id/(.*?)$')
USER_INFO = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?'
 
 
class SteamBackend(OpenIDBackend):
"""Steam OpenId authentication backend"""
name = 'steam'
 
def get_user_id(self, details, response):
"""Return user unique id provided by service"""
return self._user_id(response)
 
def get_user_details(self, response):
user_id = self._user_id(response)
url = USER_INFO + urllib.urlencode({'key': setting('STEAM_API_KEY'),
'steamids': user_id})
player = {}
try:
response = simplejson.load(urllib2.urlopen(url))
except (ValueError, IOError):
pass
else:
player = response['response']['players']['player'][0] or {}
if player:
player = {USERNAME: player.get('personaname'),
'email': '',
'fullname': '',
'first_name': '',
'last_name': ''}
return player
 
def _user_id(self, response):
match = STEAM_ID.search(response.identity_url)
if match is None:
raise AuthFailed(self, 'Missing Steam Id')
return match.group(1)
 
 
class SteamAuth(OpenIdAuth):
"""Steam OpenId authentication"""
AUTH_BACKEND = SteamBackend
 
def openid_url(self):
"""Return Steam OpenId service url"""
return 'http://steamcommunity.com/openid'
 
@classmethod
def enabled(cls):
"""Steam OpenId is enabled when STEAM_API_KEY is defined"""
return setting('STEAM_API_KEY') is not None
 
 
# Backend definition
BACKENDS = {
'steam': SteamAuth
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.