def index(): session_id = request.get_cookie('session_id') if session_id: session = Session() data = session.get(session_id) uid = data.get('uid', None) if uid: storage = UserStorage() user = storage.get_user(uid) html = """<!DOCTYPE html> <html> <body> <h2>Welcome. %s</h2> <p>you from %s, and your social id is %s<p> <img src="%s" /> <p><a href="/logout">Logout</a></p> </body> </html>""" % (user['name'], user['site_name'], user['uid'], user['avatar']) return html if not session_id: response.set_cookie('session_id', gen_session_id()) html = """<html> <body><a href="/login">Login</a></body> </html>""" return html
def logout(): session_id = request.get_cookie('session_id') if not session_id: redirect('/') session = Session() data = session.get(session_id) session.rem(session_id) uid = data.get('uid', None) if uid: # 重置其session_id Session.refresh_session_id(uid) response.set_cookie('session_id', '') redirect('/')
def callback(sitename): code = request.GET.get('code') if not code: # error occurred redirect('/oautherror') s = import_oauth_class(socialsites[sitename])() try: s.get_access_token(code) except SocialAPIError as e: # 这里可能会发生错误 print e.site_name # 哪个站点的OAuth2发生错误? print e.url # 请求的url print e.error_msg # 由站点返回的错误信息 / urllib2 的错误信息 raise # 到这里授权完毕,并且取到了用户信息,uid, name, avatar... storage = UserStorage() UID = storage.get_uid(s.site_name, s.uid) if not UID: # 此用户第一次登录,为其绑定一个自身网站的UID UID = storage.bind_new_user(s.site_name, s.uid) storage.set_user( UID, site_name = s.site_name, site_id = s.site_id, uid = s.uid, name = s.name, avatar = s.avatar ) session_id = request.get_cookie('session_id') if not session_id: session_id = Session.make_session_id(UID) session = Session() session.set(session_id, uid=UID) response.set_cookie('session_id', session_id) redirect('/')
def callback(sitename): code = request.GET.get('code') if not code: # error occurred redirect('/oautherror') socialsites = SocialSites(SOCIALOAUTH_SITES) s = socialsites.get_site_object_by_name(sitename) try: s.get_access_token(code) except SocialAPIError as e: # 这里可能会发生错误 print e.site_name # 哪个站点的OAuth2发生错误? print e.url # 请求的url print e.error_msg # 由站点返回的错误信息 / urllib2 的错误信息 raise # 到这里授权完毕,并且取到了用户信息,uid, name, avatar... storage = UserStorage() UID = storage.get_uid(s.site_name, s.uid) if not UID: # 此用户第一次登录,为其绑定一个自身网站的UID UID = storage.bind_new_user(s.site_name, s.uid) storage.set_user(UID, site_name=s.site_name, uid=s.uid, name=s.name, avatar=s.avatar) session_id = request.get_cookie('session_id') if not session_id: session_id = Session.make_session_id(UID) session = Session() session.set(session_id, uid=UID) response.set_cookie('session_id', session_id) redirect('/')