def process(request, environ, start_response): """Handle the redirect from the OpenID server and eventually login the user. """ session, oi_session = get_sessions(environ) consumer = Consumer(oi_session, OPENID_STORE) # Ask the library to check the response that the server sent us. # Status is a code indicating the response type. # Info is either None or a string containing more information about # the return type. url = request.host_url + request.path info = consumer.complete(request.params, url) oi_session.clear() if info.status == "success": user_id = info.getDisplayIdentifier() user_to_save = False user = socialauth.User.getByOpenIdIdentifier(user_id) if user is None: user = socialauth.User(openid_identifier=user_id) user_to_save = True sreg_resp = sreg.SRegResponse.fromSuccessResponse(info) if sreg_resp: for k, v in sreg_resp.iteritems(): if v and getattr(user, k) != v: setattr(user, k, v) user_to_save = True ax_rep = ax.FetchResponse.fromSuccessResponse(info) if ax_rep: data = {} for alias, url in AX_FIELDS.iteritems(): try: data[alias] = ax_rep.get(url) and ax_rep.get(url)[0] except KeyError, IndexError: pass for k, v in data.iteritems(): if v and getattr(user, k) != v: setattr(user, k, v) user_to_save = True if user_to_save: user.save() session['user_id'] = user._id session['user_human_id'] = user.human_id session.save() return utils.close_window_refresh_opener(start_response)
def process(request, environ, start_response): """Verify the returned values from twitter. If the user cancel once on twitter site, he/she is not redirected here, so not this case to handle... """ session = environ['beaker.session'] oauth_token = request.params['oauth_token'] oauth_token_secret = session.pop('socialauth.twitter_token') # Step 1. Use the request token in the session to build a new client. token = oauth.Token(oauth_token, oauth_token_secret) client = oauth.Client(consumer, token) # Step 2. Request the authorized access token from Twitter. resp, content = client.request(access_token_url, "GET") if resp['status'] != '200': raise Exception("Invalid response from Twitter.") """ This is what you'll get back from Twitter. Note that it includes the user's user_id and screen_name. { 'oauth_token_secret': 'IcJXPiJh8be3BjDWW50uCY31chyhsMHEhqJVsphC3M', 'user_id': '120889797', 'oauth_token': '120889797-H5zNnM3qE0iFoTTpNEHIz3noL9FKzXiOxwtnyVOD', 'screen_name': 'heyismysiteup' } """ access_token = dict(cgi.parse_qsl(content)) # Step 3. Lookup the user or create them if they don't exist. user_to_save = False user_id = access_token['user_id'] user = socialauth.User.getByTwitterId(user_id) if user is None: user = socialauth.User(twitter_id=user_id) user_to_save = True email = "*****@*****.**" % access_token['screen_name'] if user.email != email: user.email = email user_to_save = True if user_to_save: user.save() session['user_id'] = user._id session['user_human_id'] = user.human_id session.save() return utils.close_window_refresh_opener(start_response)
def process(request, environ, start_response): """Handle the redirect from the OpenID server and eventually login the user. """ session, oi_session = get_sessions(environ) consumer = Consumer(oi_session, OPENID_STORE) # Ask the library to check the response that the server sent us. # Status is a code indicating the response type. # Info is either None or a string containing more information about # the return type. url = request.host_url + request.path info = consumer.complete(request.params, url) oi_session.clear() if info.status == "success": user_id = info.getDisplayIdentifier() user_to_save = False user = socialauth.User.getByOpenIdIdentifier(user_id) if user is None: user = socialauth.User(openid_identifier=user_id) user_to_save = True sreg_resp = sreg.SRegResponse.fromSuccessResponse(info) if sreg_resp: for k, v in sreg_resp.iteritems(): if v and getattr(user, k) != v: setattr(user, k, v) user_to_save = True ax_rep = ax.FetchResponse.fromSuccessResponse(info) if ax_rep: data = {} for alias, url in AX_FIELDS.iteritems(): try: data[alias] = ax_rep.get(url) and ax_rep.get(url)[0] except KeyError, IndexError: pass for k, v in data.iteritems(): if v and getattr(user, k) != v: setattr(user, k, v) user_to_save = True if user_to_save: user.save() session['user_id'] = user._id session['user_human_id'] = user.human_id session.save() return utils.close_window_refresh_opener(start_response)
def process(request, environ, start_response): """Process information returned by client and server to log in the user. """ code = request.GET.get('code') if not code: # Probably cancel from user start_response('302 Redirect', [('Location', utils.LOGIN_PATH)]) return [''] args = dict(client_id=app_id, redirect_uri=redirect_uri % request.host_url, client_secret=application_secret, code=code) url = fb_access_token_url % urllib.urlencode(args) res = urllib.urlopen(url).read() response = cgi.parse_qs(res) access_token = response['access_token'][-1] # The token contains the FB userid, but the token changes from times to # times, so we can not really rely on it to find users in DB. # The token format is not garanted, so we do not try to extract # userid from it. req = urllib.urlopen(fb_profile_url % access_token) profile = json.load(req) fb_userid = profile['id'] user = socialauth.User.getByFacebookUID(fb_userid) if not user: user = socialauth.User.create(firstname=profile.get('first_name'), lastname=profile.get('last_name'), fb_id=profile['id'], fb_oauth2_token=access_token) elif user.fb_oauth2_token != access_token: user.fb_oauth2_token = access_token user.save() session = environ['beaker.session'] session['user_id'] = user._id session['user_human_id'] = user.human_id session.save() return utils.close_window_refresh_opener(start_response)
def process(request, environ, start_response): """Process information returned by client and server to log in the user. """ code = request.GET.get('code') if not code: # Probably cancel from user start_response('302 Redirect', [('Location', utils.LOGIN_PATH)]) return [''] args = dict(client_id=app_id, redirect_uri=redirect_uri % request.host_url, client_secret=application_secret, code=code) url = fb_access_token_url % urllib.urlencode(args) res = urllib.urlopen(url).read() response = cgi.parse_qs(res) access_token = response['access_token'][-1] # The token contains the FB userid, but the token changes from times to # times, so we can not really rely on it to find users in DB. # The token format is not garanted, so we do not try to extract # userid from it. req = urllib.urlopen(fb_profile_url % access_token) profile = json.load(req) fb_userid = profile['id'] user = socialauth.User.getByFacebookUID(fb_userid) if not user: user = socialauth.User.create(firstname=profile.get('first_name'), lastname=profile.get('last_name'), fb_id=profile['id'], fb_oauth2_token=access_token) elif user.fb_oauth2_token != access_token: user.fb_oauth2_token = access_token user.save() session = environ['beaker.session'] session['user_id'] = user._id session['user_human_id'] = user.human_id session.save() return utils.close_window_refresh_opener(start_response)