|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +from openid.consumer import consumer |
| 4 | +from openid.extensions import ax |
| 5 | + |
| 6 | +from pyramid.security import NO_PERMISSION_REQUIRED |
| 7 | + |
| 8 | +from ..api import ( |
| 9 | + register_provider, |
| 10 | + AuthenticationDenied, |
| 11 | +) |
| 12 | + |
| 13 | +from ..exceptions import ThirdPartyFailure |
| 14 | + |
| 15 | +from .openid import ( |
| 16 | + OpenIDAuthenticationComplete, |
| 17 | + OpenIDConsumer, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +class SteamAuthenticationComplete(OpenIDAuthenticationComplete): |
| 22 | + """ Steam auth complete """ |
| 23 | + def __init__(self, claimed_id, provider_name, provider_type): |
| 24 | + self.claimed_id = claimed_id |
| 25 | + self.provider_name = provider_name |
| 26 | + self.provider_type = provider_type |
| 27 | + |
| 28 | + |
| 29 | +class SteamAuthenticationDenied(AuthenticationDenied): |
| 30 | + """ Steam auth denied """ |
| 31 | + |
| 32 | + |
| 33 | +def includeme(config): |
| 34 | + config.add_directive('add_steam_login', add_steam_login) |
| 35 | + |
| 36 | + |
| 37 | +def add_steam_login(config, |
| 38 | + name='steam', |
| 39 | + realm=None, |
| 40 | + storage=None, |
| 41 | + login_path='/login/steam', |
| 42 | + callback_path='/login/steam/callback'): |
| 43 | + """ Add a Steam login provider to the application """ |
| 44 | + provider = SteamConsumer(name, realm, storage) |
| 45 | + |
| 46 | + config.add_route(provider.login_route, login_path) |
| 47 | + config.add_view(provider, attr='login', route_name=provider.login_route, |
| 48 | + permission=NO_PERMISSION_REQUIRED) |
| 49 | + |
| 50 | + config.add_route(provider.callback_route, callback_path, |
| 51 | + use_global_views=True, factory=provider.callback) |
| 52 | + |
| 53 | + register_provider(config, name, provider) |
| 54 | + |
| 55 | + |
| 56 | +class SteamConsumer(OpenIDConsumer): |
| 57 | + def __init__(self, name, realm=None, storage=None): |
| 58 | + """ Handle Steam auth """ |
| 59 | + super(SteamConsumer, self).__init__(name, 'steam', realm, storage, |
| 60 | + context=SteamAuthenticationComplete) |
| 61 | + |
| 62 | + def _lookup_identifier(self, request, identifier): |
| 63 | + """ Return the Steam OpenID directed endpoint """ |
| 64 | + return 'http://steamcommunity.com/openid' |
| 65 | + |
| 66 | + def callback(self, request): |
| 67 | + """ Handle incoming redirect from Steam OpenID """ |
| 68 | + openid_session = request.session.pop('velruse.openid_session', None) |
| 69 | + |
| 70 | + if not openid_session: |
| 71 | + raise ThirdPartyFailure('No OpenID session has begun') |
| 72 | + |
| 73 | + # Setup the consumer and parse the information coming back |
| 74 | + oidconsumer = consumer.Consumer(openid_session, self.openid_store) |
| 75 | + return_to = request.route_url(self.callback_route) |
| 76 | + info = oidconsumer.complete(request.params, return_to) |
| 77 | + |
| 78 | + if info.status in [consumer.FAILURE, consumer.CANCEL]: |
| 79 | + return SteamAuthenticationDenied('OpenID failure', |
| 80 | + provider_name=self.name, |
| 81 | + provider_type=self.type) |
| 82 | + elif info.status == consumer.SUCCESS: |
| 83 | + claimed_id = str(info.identity_url) |
| 84 | + return SteamAuthenticationComplete(claimed_id=claimed_id, |
| 85 | + provider_name=self.name, |
| 86 | + provider_type=self.type) |
| 87 | + else: |
| 88 | + raise ThirdPartyFailure('OpenID failed') |
0 commit comments