def associate_success(request, identity_url, openid_response, redirect_field_name=REDIRECT_FIELD_NAME, send_email=True, **kwargs): """ function used when new openid association success. redirect the user """ openid_ = from_openid_response(openid_response) openids = request.session.get('openids', []) openids.append(openid_) request.session['openids'] = openids uassoc = UserAssociation( openid_url=str(openid_), user_id=request.user.id ) uassoc.save(send_email=send_email) redirect_to = request.GET.get(redirect_field_name, '') if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL return HttpResponseRedirect(redirect_to)
def register(request, template_name='authopenid/complete.html', redirect_field_name=REDIRECT_FIELD_NAME, register_form=OpenidRegisterForm, auth_form=AuthenticationForm, register_account=register_account, send_email=False, extra_context=None): """ register an openid. If user is already a member he can associate its openid with its account. A new account could also be created and automaticaly associated to the openid. :attr request: request object :attr template_name: string, name of template to use, 'authopenid/complete.html' by default :attr redirect_field_name: string, field name used for redirect. by default 'next' :attr register_form: form use to create a new account. By default `OpenidRegisterForm` :attr auth_form: form object used for legacy authentification. by default `OpenidVerifyForm` form auser auth contrib. :attr register_account: callback used to create a new account from openid. It take the register_form as param. :attr send_email: boolean, by default True. If True, an email will be sent to the user. :attr extra_context: A dictionary of variables to add to the template context. Any callable object in this dictionary will be called to produce the end result which appears in the context. """ is_redirect = False redirect_to = request.REQUEST.get(redirect_field_name, '') openid_ = request.session.get('openid', None) if openid_ is None or not openid_: return HttpResponseRedirect("%s?%s" % (reverse('user_signin'), urllib.urlencode({ redirect_field_name: redirect_to }))) nickname = '' email = '' if openid_.sreg is not None: nickname = openid_.sreg.get('nickname', '') email = openid_.sreg.get('email', '') if openid_.ax is not None and not nickname or not email: if openid_.ax.get('http://schema.openid.net/namePerson/friendly', False): nickname = openid_.ax.get('http://schema.openid.net/namePerson/friendly')[0] if openid_.ax.get('http://schema.openid.net/contact/email', False): email = openid_.ax.get('http://schema.openid.net/contact/email')[0] form1 = register_form(initial={ 'username': nickname, 'email': email, }) form2 = auth_form(initial={ 'username': nickname, }) if request.POST: user_ = None if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = settings.LOGIN_REDIRECT_URL if 'email' in request.POST.keys(): form1 = register_form(data=request.POST) if form1.is_valid(): user_ = register_account(form1, openid_) else: form2 = auth_form(data=request.POST) if form2.is_valid(): user_ = form2.get_user() if user_ is not None: # associate the user to openid uassoc = UserAssociation( openid_url=str(openid_), user_id=user_.id ) uassoc.save(send_email=send_email) login(request, user_) return HttpResponseRedirect(redirect_to) return render(template_name, { 'form1': form1, 'form2': form2, redirect_field_name: redirect_to, 'nickname': nickname, 'email': email }, context_instance=_build_context(request, extra_context=extra_context))