Skip to content

Commit

Permalink
Steam OpenId support. Closes #596
Browse files Browse the repository at this point in the history
  • Loading branch information
omab committed Feb 14, 2013
1 parent cd68682 commit 24481f0
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/example/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
'social_auth.backends.contrib.stocktwits.StocktwitsBackend',
'social_auth.backends.contrib.behance.BehanceBackend',
'social_auth.backends.contrib.readability.ReadabilityBackend',
'social_auth.backends.steam.SteamBackend',
'django.contrib.auth.backends.ModelBackend',
)

Expand Down
72 changes: 72 additions & 0 deletions social_auth/backends/steam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""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})
details = {}
try:
player = simplejson.load(urllib2.urlopen(url))
except (ValueError, IOError):
pass
else:
if len(player['response']['players']) > 0:
player = player['response']['players'][0]
details = {USERNAME: player.get('personaname'),
'email': '',
'fullname': '',
'first_name': '',
'last_name': '',
'player': player}
return details

def extra_data(self, user, uid, response, details):
return details['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
}

0 comments on commit 24481f0

Please sign in to comment.