'The setup flow creates the auth provider as well as an identity linked
to the active user.'

@transaction.atomic
def _finish_setup_pipeline(self, identity):

    request = self.request
    if (not request.user.is_authenticated()):
        return self.error(ERR_NOT_AUTHED)
    if (request.user.id != request.session['auth']['uid']):
        return self.error(ERR_UID_MISMATCH)
    state = request.session['auth']['state']
    config = self.provider.build_config(state)
    try:
        om = OrganizationMember.objects.get(user=request.user, organization=self.organization)
    except OrganizationMember.DoesNotExist:
        return self.error(ERR_UID_MISMATCH)
    self.auth_provider = AuthProvider.objects.create(organization=self.organization, provider=self.provider.key, config=config)
    self._handle_attach_identity(identity, om)
    auth.mark_sso_complete(request, self.organization.id)
    AuditLogEntry.objects.create(organization=self.organization, actor=request.user, ip_address=request.META['REMOTE_ADDR'], target_object=self.auth_provider.id, event=AuditLogEntryEvent.SSO_ENABLE, data=self.auth_provider.get_audit_log_data())
    email_missing_links.delay(organization_id=self.organization.id)
    messages.add_message(self.request, messages.SUCCESS, OK_SETUP_SSO)
    self.clear_session()
    next_uri = reverse('sentry-organization-auth-settings', args=[self.organization.slug])
    return HttpResponseRedirect(next_uri)
