def callback(sitename): code = request.GET.get('code') if not code: # error occurred redirect('/oautherror') s = import_oauth_class(socialsites[sitename])() s.get_access_token(code) # 到这里授权完毕,并且取到了用户信息,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, 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) #print storage.user #print storage.table # #print session._sessions redirect('/')
def social_login_callback(request, sitename): code = request.GET.get('code', None) if not code: # Maybe user not authorize return HttpResponseRedirect(SOCIAL_LOGIN_ERROR_REDIRECT_URL) s = import_oauth_class(socialsites[sitename])() try: s.get_access_token(code) except SocialAPIError: # see social_oauth example and docs return HttpResponseRedirect(SOCIAL_LOGIN_ERROR_REDIRECT_URL) user_info_model = get_model(*SOCIAL_LOGIN_USER_INFO_MODEL.split('.')) try: user = SocialUser.objects.get(site_uid=s.uid, site_id=s.site_id) # got user, update username and avatar user_info_model.objects.filter(user_id=user.user_id).update( username=s.name, avatar=s.avatar) except SocialUser.DoesNotExist: user = SocialUser.objects.create(site_uid=s.uid, site_id=s.site_id) user_info_model.objects.create(user_id=user.user_id, username=s.name, avatar=s.avatar) # set uid in session, then next time, this user will be auto loggin request.session['uid'] = user.user_id # done return HttpResponseRedirect(SOCIAL_LOGIN_DONE_REDIRECT_URL)
def login(): connections = dict() for socialsite in socialsites.list_sites(): provider = import_oauth_class(socialsite)() connections[provider.site_name] = provider return render_template('login.html', connections=connections)
def make_site(s): s = import_oauth_class(s)() return { 'site_id': s.site_id, 'site_name': s.site_name, 'site_name_zh': s.site_name_zh, 'authorize_url': s.authorize_url, }
def _link(s): _s = import_oauth_class(s)() if os.path.exists(os.path.join(IMAGE_PATH, _s.site_name + '.png')): a_content = '<img src="/static/images/%s.png" />' % _s.site_name else: a_content = '使用 %s 登录' % _s.site_name_zh return """<div style="margin: 20px;"> <a href="%s">%s</a> </div>""" % (_s.authorize_url, a_content)
def _make_user_info(u): info = {} info['id'] = u.id info['social'] = u.is_social if info['social']: site_id = u.social_user.site_id s = import_oauth_class( socialsites.get_site_class_by_id(site_id) )() info['social'] = s.site_name_zh info['username'] = u.user_info.username info['avatar'] = u.user_info.avatar info['current'] = request.siteuser and request.siteuser.id == u.id return info
def get_oauth_login_url(site): """ Get oauth login url according site """ from uliweb import settings from socialoauth import socialsites from socialoauth.utils import import_oauth_class socialsites._sites_id_name_table = {} socialsites.config(settings.SOCIALOAUTH) _s = import_oauth_class(socialsites[site])() return _s.authorize_url
def login_oauth(provider): code = request.args.get('code') if not code: redirect(url_for('index.index')) socialsite = import_oauth_class(socialsites[provider])() try: socialsite.get_access_token(code) except SocialAPIError as e: print e.site_name print e.url print e.error_msg raise session['user'] = socialsite return redirect(url_for('index.index'))
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(site): from socialoauth import socialsites from socialoauth.utils import import_oauth_class from socialoauth.exception import SocialAPIError from uliweb.contrib.auth import login from uliweb.utils import date code = request.GET.get('code') if not code: # error occurred error("Can't found the code from oauth return data") socialsites._sites_id_name_table = {} socialsites.config(settings.SOCIALOAUTH) s = import_oauth_class(socialsites[site])() try: s.get_access_token(code) except SocialAPIError as e: print e.site_name print e.url print e.error_msg raise # we can get uid, name, avatar from s # we should store the user info to database User = functions.get_model('user') user = User.get((User.c.username==s.uid) & (User.c.login_type=='1') & (User.c.login_site==site)) if not user: user = User(username=s.uid, nickname=s.name, image=s.avatar, login_type='1', login_site=site) user.save() login(user) #引导用户填写邮箱地址 return redirect('/') else: user.last_login = date.now() user.save() login(user) return 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 social_login_callback(request, sitename): code = request.GET.get('code', None) if not code: # Maybe user not authorize return HttpResponseRedirect(SOCIAL_LOGIN_ERROR_REDIRECT_URL) s = import_oauth_class(socialsites[sitename])() try: s.get_access_token(code) except SocialAPIError: # see social_oauth example and docs return HttpResponseRedirect(SOCIAL_LOGIN_ERROR_REDIRECT_URL) user_info_model = get_model(*SOCIAL_LOGIN_USER_INFO_MODEL.split('.')) try: user = SocialUser.objects.get(site_uid=s.uid, site_id=s.site_id) #got user, update username and avatar user_info_model.objects.filter(user_id=user.user_id).update( username=s.name, avatar=s.avatar ) except SocialUser.DoesNotExist: user = SocialUser.objects.create(site_uid=s.uid, site_id=s.site_id) user_info_model.objects.create( user_id=user.user_id, username=s.name, avatar=s.avatar ) # set uid in session, then next time, this user will be auto loggin request.session['uid'] = user.user_id # done return HttpResponseRedirect(SOCIAL_LOGIN_DONE_REDIRECT_URL)
def get_site_object_by_class(self, site_class): return import_oauth_class(site_class)()
def get_site_object_by_name(self, site_name): site_class = self.__getitem__(site_name) return import_oauth_class(site_class)()
def make_site(s): s = import_oauth_class(s)() return s.authorize_url
def _link(s): m = import_oauth_class(s) _s = m() return """<div style="margin: 20px;"> <a href="%s"><img src="/static/images/%s.png" /></a> </div>""" % (_s.authorize_url, _s.site_name)