def connect_handler(cv, provider): """Shared method to handle the connection process :param connection_values: A dictionary containing the connection values :param provider_id: The provider ID the connection shoudl be made to """ cv.setdefault('user_id', current_user.get_id()) connection = _datastore.find_connection(**cv) if connection is None: after_this_request(_commit) connection = _datastore.create_connection(**cv) msg = ('Connection established to %s' % provider.name, 'success') connection_created.send(current_app._get_current_object(), user=current_user._get_current_object(), connection=connection) else: msg = ('A connection is already established with %s ' 'to your account' % provider.name, 'notice') connection_failed.send(current_app._get_current_object(), user=current_user._get_current_object()) redirect_url = session.pop(config_value('POST_OAUTH_CONNECT_SESSION_KEY'), get_url(config_value('CONNECT_ALLOW_VIEW'))) do_flash(*msg) return redirect(redirect_url)
def login_handler(response, provider, query): """Shared method to handle the signin process""" connection = _datastore.find_connection(**query) if connection: after_this_request(_commit) user = connection.user login_user(user) key = _social.post_oauth_login_session_key redirect_url = session.pop(key, get_post_login_redirect()) login_completed.send(current_app._get_current_object(), provider=provider, user=user) return redirect(redirect_url) login_failed.send(current_app._get_current_object(), provider=provider, oauth_response=response) next = get_url(_security.login_manager.login_view) msg = '%s account not associated with an existing user' % provider.name do_flash(msg, 'error') return redirect(next)
def dispatch_request(self): form = forms.LoginForm() if form.validate_on_submit(): utils.login_user(form.user, remember=form.remember.data) flask.after_this_request(security_views._commit) return flask.redirect(utils.get_post_login_redirect()) form.next.data = ( utils.get_url(flask.request.args.get('next')) or utils.get_url(flask.request.form.get('next')) or '' ) return flask.render_template('login.html', form=form)
def connect(provider_id): """Starts the provider connection OAuth flow""" provider = get_provider_or_404(provider_id) callback_url = get_authorize_callback('connect', provider_id) allow_view = get_url(config_value('CONNECT_ALLOW_VIEW')) pc = request.form.get('next', allow_view) session[config_value('POST_OAUTH_CONNECT_SESSION_KEY')] = pc return provider.authorize(callback_url)
def confirm_email(token): """View function which handles a email confirmation request.""" expired, invalid, user = confirm_email_token_status(token) if not user or invalid: invalid = True do_flash(*get_message('INVALID_CONFIRMATION_TOKEN')) if expired: send_confirmation_instructions(user) do_flash(*get_message('CONFIRMATION_EXPIRED', email=user.email, within=_security.confirm_email_within)) if invalid or expired: return redirect(get_url(_security.confirm_error_view) or url_for_security('send_confirmation')) if user.confirmed_at is not None: do_flash(*get_message('ALREADY_CONFIRMED')) return redirect(get_url(_security.post_confirm_view) or get_url(_security.post_login_view)) if request.json: form_data = MultiDict(request.json) else: form_data = request.form form = forms.ConfirmEmailForm(form_data) if form.validate_on_submit(): user.password = form.password.data confirm_user(user) # this saves 'user' if user != current_user: logout_user() login_user(user) do_flash(*get_message('EMAIL_CONFIRMED')) return redirect(get_url(_security.post_confirm_view) or get_url(_security.post_login_view)) return render_template('security/confirm.html', token=token, confirm_form=form, **_ctx('change_password') )
def connect_callback(provider_id): provider = get_provider_or_404(provider_id) def connect(response): cv = get_connection_values_from_oauth_response(provider, response) return cv cv = provider.authorized_handler(connect)() if cv is None: do_flash('Access was denied by %s' % provider.name, 'error') return redirect(get_url(config_value('CONNECT_DENY_VIEW'))) return connect_handler(cv, provider)
def connect(response): cv = get_connection_values_from_oauth_response(provider, response) if cv is None: do_flash('Access was denied by %s' % provider.name, 'error') return redirect(get_url(config_value('CONNECT_DENY_VIEW'))) return cv