def open_prompt(request, site=None, popup=False) : """ Redirect the user to the approprate OAuth provider to get credientals """ c = consumer.Consumer(request.session, util.OpenIDStore()) c.setAssociationPreference([('HMAC-SHA1', 'no-encryption')]) if popup : return_to = request.build_absolute_uri(reverse('openauth:pop_callback', kwargs={'site':site})) else : return_to = request.build_absolute_uri(reverse('openauth:callback', kwargs={'site':site})) if site not in settings.OPENAUTH_DATA : raise Http404("Unknown OAuth service %s" % site) if site == 'google' : auth_request = c.begin('https://www.google.com/accounts/o8/id') auth_request.addExtensionArg('http://openid.net/srv/ax/1.0', 'mode', 'fetch_request') auth_request.addExtensionArg('http://openid.net/srv/ax/1.0', 'required', 'email,firstname,lastname') auth_request.addExtensionArg('http://openid.net/srv/ax/1.0', 'type.email', 'http://schema.openid.net/contact/email') auth_request.addExtensionArg('http://openid.net/srv/ax/1.0', 'type.firstname', 'http://axschema.org/namePerson/first') auth_request.addExtensionArg('http://openid.net/srv/ax/1.0', 'type.lastname', 'http://axschema.org/namePerson/last') auth_request.addExtensionArg('http://specs.openid.net/extensions/oauth/1.0', 'consumer', settings.OPENAUTH_DATA[site]['key']) auth_request.addExtensionArg('http://specs.openid.net/extensions/oauth/1.0', 'scope', 'http://www.google.com/m8/feeds') parts = list(urlparse.urlparse(return_to)) realm = urlparse.urlunparse(parts[0:2] + [''] * 4) url = auth_request.redirectURL(realm, return_to) elif site == 'yahoo' : auth_request = c.begin('http://open.login.yahooapis.com/openid20/www.yahoo.com/xrds') auth_request.message.namespaces.addAlias('http://specs.openid.net/extensions/oauth/1.0', 'oauth') auth_request.message.setArg(openid.message.OPENID2_NS, 'identity', 'http://specs.openid.net/auth/2.0/identifier_select') auth_request.addExtensionArg('http://specs.openid.net/extensions/oauth/1.0', 'consumer', settings.OPENAUTH_DATA[site]['key']) parts = list(urlparse.urlparse(return_to)) realm = urlparse.urlunparse(parts[0:2] + [''] * 4) url = auth_request.redirectURL(realm, return_to) elif site == 'facebook' : import urllib #return render_to_response('openauth/facebook_r.html', { # 'apikey' : settings.OPENAUTH_DATA[site]['key'] # }, context_instance=RequestContext(request)) if 'code' in request.GET : url = 'https://graph.facebook.com/oauth/access_token?%s' % urllib.urlencode({ 'client_id' : settings.OPENAUTH_DATA[site]['appid'], 'redirect_uri' : return_to, 'code' : request.GET['code'], 'client_secret' : settings.OPENAUTH_DATA[site]['secret'], }) else : url = 'https://graph.facebook.com/oauth/authorize?%s' % urllib.urlencode({ 'client_id' : settings.OPENAUTH_DATA[site]['appid'], 'redirect_uri' : return_to, 'scope' : 'email,user_status', }) elif site in OPENID_SERVICES : try : c = OpenUser.client_for(site) except OpenUser.NoProvider : raise Http404("Unknown OAuth service %s" % site) token = c.get_request_token(return_to) #print 'TOKEN = ', token['oauth_token'] #print 'SECRET = ', token['oauth_token_secret'] cache.set('oauth_token_%s' % token['oauth_token'], token['oauth_token_secret']) url = c.get_authorize_url(token['oauth_token'], callback=return_to) else : raise Http404("Unknown OAuth service") return HttpResponseRedirect(url)
# 'expires' : facebook.session_key_expires, #} elif 'oauth_token' in request.GET : # # OAuth Login (LinkedIn) # if site not in OPENID_SERVICES : return HttpResponse('Invalid site') tok = request.GET['oauth_token'] tok_secret = cache.get('oauth_token_%s' % tok, None) #print "OAUTH Login" try : c = OpenUser.client_for(site, oauth_token=tok, oauth_token_secret=tok_secret) except OpenUser.NoProvider : raise Http404("No provider for serivce %s" % site) if 'oauth_verifier' in request.GET : token = c.get_access_token(token=tok, verifier=request.GET['oauth_verifier']) else : token = c.get_access_token(token=tok) identity = "oauth:%s:%s" % ( site, token['oauth_token'] ) else : #print "OpenID Login" #for k, v in request.GET.items() : print k, v # # OpenID Login (Google for Example)