def validate_email(self, field): session = get_session(request) if session['email']: if session['email'] != field.data: raise ValidationError(u'不能修改第三方登录的email地址') user = User.objects(email=field.data).first() if user: raise ValidationError(u'Email地址已被使用')
def signin_post(): session = get_session(request) form = SigninForm(request.POST) if form.validate(): login.login_user(str(form.user.id)) if 'oauth_provider' in session: user_bind_oauth(form.user, session) redirect('/') else: return locals()
def manage(): form = UserInfoForm(request.forms) if form.validate(): if request.user: request.user.username = form.username.data return redirect('/') else: user = User(email=form.email.data, username=form.username.data, is_email_confirmed=True) user.save() login.login_user(str(user.id)) session = get_session(request) if 'email' in session: del(session['email']) return redirect('/') return {'form': form, 'token': request.csrf_token}
def oauth_callback(provider): logger.info("Oauth callback for %s" % provider) redirect_uri = app.config['oauth.%s.callback_url' % provider] oauth_service = oauth_services[provider] session = get_session(request) data = dict(code=request.params.get('code'), grant_type='authorization_code', redirect_uri=redirect_uri) if provider == 'google': oauth_session = oauth_service.get_auth_session(data=data, decoder=json.loads) user_info = oauth_session.get('userinfo').json() email = session['email'] = user_info['email'] username = user_info['given_name'] elif provider == 'github': oauth_session = oauth_service.get_auth_session(data=data) user_info = oauth_session.get('user').json() email = session['email'] = user_info['email'] username = user_info['login'] access_token = oauth_session.access_token user_info['id'] = str(user_info['id']) logger.info("%s oauth access token is: %s" % (provider, access_token)) logger.info("%s oauth user info is %s" % (provider, user_info)) user = User.find_by_oauth(provider, user_info['id']) if user: # TODO: 直接登录时更新 token. login.login_user(str(user.id)) return redirect('/') else: user = User.objects(email=email).first() if user: user_oauth = UserOauth(provider=provider, openid=user_info['id'], token=access_token) user_oauth.save() login.login_user(str(user.id)) return redirect('/') else: return { 'form': UserInfoForm(email=email, username=username), 'token': request.csrf_token }
def manage(): form = UserInfoForm(request.forms) if form.validate(): if request.user: request.user.username = form.username.data return redirect('/') else: user = User(email=form.email.data, username=form.username.data, is_email_confirmed=True) user.save() login.login_user(str(user.id)) session = get_session(request) if 'email' in session: del (session['email']) return redirect('/') return {'form': form, 'token': request.csrf_token}
def oauth_callback(provider): logger.info("Oauth callback for %s" % provider) redirect_uri = app.config['oauth.%s.callback_url' % provider] oauth_service = oauth_services[provider] session = get_session(request) data = dict(code=request.params.get('code'), grant_type='authorization_code', redirect_uri=redirect_uri) if provider == 'google': oauth_session = oauth_service.get_auth_session(data=data, decoder=json.loads) user_info = oauth_session.get('userinfo').json() email = session['email'] = user_info['email'] username = user_info['given_name'] elif provider == 'github': oauth_session = oauth_service.get_auth_session(data=data) user_info = oauth_session.get('user').json() email = session['email'] = user_info['email'] username = user_info['login'] access_token = oauth_session.access_token user_info['id'] = str(user_info['id']) logger.info("%s oauth access token is: %s" % (provider, access_token)) logger.info("%s oauth user info is %s" % (provider, user_info)) user = User.find_by_oauth(provider, user_info['id']) if user: # TODO: 直接登录时更新 token. login.login_user(str(user.id)) return redirect('/') else: user = User.objects(email=email).first() if user: user_oauth = UserOauth(provider=provider, openid=user_info['id'], token=access_token) user_oauth.save() login.login_user(str(user.id)) return redirect('/') else: return {'form': UserInfoForm(email=email, username=username), 'token': request.csrf_token}