예제 #1
0
def _callback_url(request):
    site_domain = Site.objects.get_current().domain
    if site_domain != 'example.com':
        # Site is properly configured.  Use it
        return 'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        )

    # django.site is not configured. Figure out the domain from the request
    # Why not just always do this?
    url = request.build_absolute_uri( reverse('openid_callback') )
    if _https():
        url = re.sub('http://','https://',url)
    return url
def facebook_js():
    id = getattr(settings, 'FACEBOOK_APP_ID', None)
    key = getattr(settings, 'FACEBOOK_API_KEY', None)
    perms = getattr(settings, 'FACEBOOK_REQUEST_PERMISSIONS', None)
    if not id:
        warnings.warn("django-socialregistration: Please update your settings.py and add a FACEBOOK_APP_ID key", Warning)
    return {'facebook_app_id': id, 'facebook_api_key': key, 'is_https' : bool(_https()), 'facebook_req_perms' : perms }
def facebook_js(requested_perms=""):
    return {
        'facebook_api_key': getattr(settings, 'FACEBOOK_API_KEY', ''),
        'is_https': bool(_https()),
        'requested_perms': requested_perms,
        'MEDIA_URL': getattr(settings, 'MEDIA_URL', ''),
        'STATIC_MEDIA_URL': getattr(settings, 'STATIC_MEDIA_URL', '')
    }
def facebook_js():
    id = getattr(settings, "FACEBOOK_APP_ID", None)
    key = getattr(settings, "FACEBOOK_API_KEY", None)
    perms = getattr(settings, "FACEBOOK_REQUEST_PERMISSIONS", None)
    if not id:
        warnings.warn(
            "django-socialregistration: Please update your settings.py and add a FACEBOOK_APP_ID key", Warning
        )
    return {"facebook_app_id": id, "facebook_api_key": key, "is_https": bool(_https()), "facebook_req_perms": perms}
예제 #5
0
def openid_callback(request, template='socialregistration/openid.html',
    extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html',
    client_class = None):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = client_class(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )

    if client.is_valid():
        identity = client.result.identity_url
        if request.user.is_authenticated():
            # Handling already logged in users just connecting their accounts
            try:
                profile = OpenIDProfile.objects.get(identity=identity)
            except OpenIDProfile.DoesNotExist: # There can only be one profile with the same identity
                profile = OpenIDProfile.objects.create(user=request.user,
                    identity=identity)
                _connect(user, profile, client)

            return HttpResponseRedirect(_get_next(request))

        user = authenticate(identity=identity)
        if user is None:
            request.session['socialregistration_user'] = User()
            request.session['socialregistration_profile'] = OpenIDProfile(
                identity=identity
            )
            # Client is not pickleable with the request on it
            client.request = None
            request.session['socialregistration_client'] = client
            return HttpResponseRedirect(reverse('socialregistration_setup'))

        if not user.is_active:
            return render_to_response(
                account_inactive_template,
                extra_context,
                context_instance=RequestContext(request)
            )

        _login(request, user, OpenIDProfile.objects.get(user = user), client)
        return HttpResponseRedirect(_get_next(request))

    return render_to_response(
        template,
        dict(),
        context_instance=RequestContext(request)
    )
예제 #6
0
def openid_callback(request, template='socialregistration/openid.html',
    extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html',
    client_class = None):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = client_class(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )

    if client.is_valid():
        identity = client.result.identity_url
        if request.user.is_authenticated():
            # Handling already logged in users just connecting their accounts
            try:
                profile = OpenIDProfile.objects.get(identity=identity)
            except OpenIDProfile.DoesNotExist: # There can only be one profile with the same identity
                profile = OpenIDProfile.objects.create(user=request.user,
                    identity=identity)
                _connect(request.user, profile, client)

            return HttpResponseRedirect(_get_next(request))

        user = authenticate(identity=identity)
        if user is None:
            request.session['socialregistration_user'] = User()
            request.session['socialregistration_profile'] = OpenIDProfile(
                identity=identity
            )
            # Client is not pickleable with the request on it
            client.request = None
            request.session['socialregistration_client'] = client
            return HttpResponseRedirect(reverse('socialregistration_setup'))

        if not user.is_active:
            return render_to_response(
                account_inactive_template,
                extra_context,
                context_instance=RequestContext(request)
            )

        _login(request, user, OpenIDProfile.objects.get(user = user), client)
        return HttpResponseRedirect(_get_next(request))

    return render_to_response(
        template,
        dict(),
        context_instance=RequestContext(request)
    )
예제 #7
0
def openid_callback(request, template='socialregistration/openid.html',
    extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )

    if client.is_valid():
        if request.user.is_authenticated():
            # Handling already logged in users just connecting their accounts
            try:
                profile = OpenIDProfile.objects.get(identity=request.GET.get('openid.claimed_id'))
            except OpenIDProfile.DoesNotExist: # There can only be one profile with the same identity
                profile = OpenIDProfile.objects.create(user=request.user,
                    identity=request.GET.get('openid.claimed_id'))

            return HttpResponseRedirect(_get_next(request))

        user = authenticate(identity=request.GET.get('openid.claimed_id'))
        if user is None:
            request.session['socialregistration_user'] = User()
            request.session['socialregistration_profile'] = OpenIDProfile(
                identity=request.GET.get('openid.claimed_id')
            )
            for key, value in getattr(client, 'registration_data', {}).items():
                request.session['social_suggested_%s' % key] = value
            return HttpResponseRedirect(reverse('socialregistration_setup'))
        else:
            login(request, user)
            return HttpResponseRedirect(_get_next(request))

        if not user.is_active:
            return render_to_response(
                account_inactive_template,
                extra_context,
                context_instance=RequestContext(request)
            )

        login(request, user)
        return HttpResponseRedirect(_get_next(request))            
    
    return render_to_response(
        template,
        dict(),
        context_instance=RequestContext(request)
    )
예제 #8
0
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session["next"] = _get_next(request)
    request.session["openid_provider"] = request.GET.get("openid_provider")

    client = OpenID(
        request,
        "http%s://%s%s" % (_https(), Site.objects.get_current().domain, reverse("openid_callback")),
        request.GET.get("openid_provider"),
    )
    return client.get_redirect()
예제 #9
0
def facebook_js():
    id = getattr(settings, 'FACEBOOK_APP_ID', None)
    key = getattr(settings, 'FACEBOOK_API_KEY', None)
    perms = getattr(settings, 'FACEBOOK_REQUEST_PERMISSIONS', None)
    if not id:
        warnings.warn(
            "django-socialregistration: Please update your settings.py and add a FACEBOOK_APP_ID key",
            Warning)
    return {
        'facebook_app_id': id,
        'facebook_api_key': key,
        'is_https': bool(_https()),
        'facebook_req_perms': perms
    }
예제 #10
0
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session["next"] = _get_next(request)
    request.session["openid_provider"] = request.GET.get("openid_provider")
    client = OpenID(
        request,
        "http%s://%s%s" % (_https(), request.get_host(), reverse("openid_callback")),
        request.GET.get("openid_provider"),
    )
    try:
        return client.get_redirect()
    except DiscoveryFailure:
        request.session["openid_error"] = True
        return HttpResponseRedirect(settings.LOGIN_URL)
예제 #11
0
def openid_callback(request, template='socialregistration/openid.html',
    extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )
    
    if client.is_valid():
        if request.user.is_authenticated():
            # Handling already logged in users just connecting their accounts
            try:
                profile, created = OpenIDProfile.objects.get_or_create(user=request.user, identity=request.GET.get('openid.claimed_id'))
            except IntegrityError:
                request.user.message_set.create(message=_('This openid has already been connected to a user.'))
            return HttpResponseRedirect(_get_next(request))
        
        user = authenticate(identity=request.GET.get('openid.claimed_id'))
        if user is None:
            request.session['socialregistration_user'] = User()
            request.session['socialregistration_profile'] = OpenIDProfile(
                identity=request.GET.get('openid.claimed_id')
            )
            return HttpResponseRedirect(reverse('socialregistration_setup'))
        
        if not user.is_active:
            return render_to_response(
                account_inactive_template,
                extra_context,
                context_instance=RequestContext(request)
            )
        
        login(request, user)
        return HttpResponseRedirect(_get_next(request))            
    
    return render_to_response(
        template,
        dict(),
        context_instance=RequestContext(request)
    )
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session['next'] = _get_next(request)
    request.session['openid_provider'] = request.GET.get('openid_provider')

    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.GET.get('openid_provider')
    )
    return client.get_redirect()
예제 #13
0
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session['next'] = _get_next(request)
    request.session['openid_provider'] = request.GET.get('openid_provider')
    
    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.GET.get('openid_provider')
    )
    return client.get_redirect()
예제 #14
0
def openid_redirect(request, client_class=None):
    """
    Redirect the user to the openid provider
    """
    request.session["next"] = _get_next(request)
    request.session["openid_provider"] = request.GET.get("openid_provider")

    client = client_class(
        request,
        "http%s://%s%s" % (_https(), Site.objects.get_current().domain, reverse("openid_callback")),
        request.GET.get("openid_provider"),
    )
    try:
        return client.get_redirect()
    except DiscoveryFailure:
        request.session["openid_error"] = True
        return HttpResponseRedirect(settings.LOGIN_URL)
예제 #15
0
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session['next'] = _get_next(request)
    request.session['openid_provider'] = request.GET.get('openid_provider')

    client = OpenID(
        request,
        'http%s://%s%s' % (_https(), Site.objects.get_current().domain,
                           reverse('openid_callback')),
        request.GET.get('openid_provider'))
    try:
        return client.get_redirect()
    except DiscoveryFailure:
        request.session['openid_error'] = True
        return HttpResponseRedirect(settings.LOGIN_URL)
def openid_callback(
    request,
    template="socialregistration/openid.html",
    extra_context=dict(),
    account_inactive_template="socialregistration/account_inactive.html",
):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        "http%s://%s%s" % (_https(), Site.objects.get_current().domain, reverse("openid_callback")),
        request.session.get("openid_provider"),
    )

    if client.is_valid():
        identity = client.result.identity_url
        if request.user.is_authenticated():
            # Handling already logged in users just connecting their accounts
            try:
                profile = OpenIDProfile.objects.get(identity=identity)
            except OpenIDProfile.DoesNotExist:  # There can only be one profile with the same identity
                profile = OpenIDProfile.objects.create(user=request.user, identity=identity)

            return HttpResponseRedirect(_get_next(request))

        user = authenticate(identity=identity)
        if user is None:
            request.session["socialregistration_user"] = User()
            request.session["socialregistration_profile"] = OpenIDProfile(identity=identity)
            return HttpResponseRedirect(reverse("socialregistration_setup"))

        if not user.is_active:
            return render_to_response(
                account_inactive_template, extra_context, context_instance=RequestContext(request)
            )

        login(request, user)
        return HttpResponseRedirect(_get_next(request))

    return render_to_response(template, dict(), context_instance=RequestContext(request))
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session['next'] = _get_next(request)
    request.session['openid_provider'] = request.GET.get('openid_provider')

    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.GET.get('openid_provider')
    )
    try:
        return client.get_redirect()
    except DiscoveryFailure:
        request.session['openid_error'] = True
        return HttpResponseRedirect(settings.LOGIN_URL)
예제 #18
0
def openid_redirect(request):
    """
    Redirect the user to the openid provider
    """
    request.session['next'] = _get_next(request)
    request.session['openid_provider'] = request.GET.get('openid_provider')
    request.session['socialregistration_connect_object'] = get_object(request.GET)

    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.GET.get('openid_provider')
    )
    try:
        logger.info("Received redirect to %s from OpenID" % client.get_redirect())
        return client.get_redirect()
    except DiscoveryFailure:
        request.session['openid_error'] = True
        logger.info("OpenID failure, sending user to login.")
        return HttpResponseRedirect(settings.LOGIN_URL)
예제 #19
0
def hyves_js():
    return {'hyves_consumer_key' : settings.HYVES_CONSUMER_KEY,
            'site': Site.objects.get_current(),
            'is_https' : bool(_https())}
예제 #20
0
def twitter_js_tag():
    return {'is_https': bool(_https())}
예제 #21
0
def facebook_js_tag():
    return {
        'facebook_api_key': settings.FACEBOOK_API_KEY,
        'is_https': bool(_https())
    }
예제 #22
0
def openxspc_js_tag():
    return {'is_https': bool(_https())}
def facebook_js():
    return {'facebook_api_key' : get_setting_for_current_site('FACEBOOK_API_KEY'), 'is_https' : bool(_https())}
def facebook_js():
    return {"facebook_api_key": settings.FACEBOOK_API_KEY, "is_https": bool(_https())}
예제 #25
0
def facebook_js():
    return {'facebook_api_key' : settings.FACEBOOK_API_KEY, 'is_https' : bool(_https())}
def facebook_js():
    return {'facebook_app_id' : settings.FACEBOOK_APP_ID, 'is_https' : bool(_https())}
예제 #27
0
def _openid_callback_url():
    return 'http%s://%s%s' % (
        _https(),
        Site.objects.get_current().domain,
        reverse('openid_callback')
    )
예제 #28
0
def twitter_js_tag():
    return {
            'is_https' : bool( _https() )
    }
def openid_callback(request, template='socialregistration/openid.html', extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )
#    print 'hi'
#    print request.GET
    if client.is_valid():
        if request.user.is_authenticated():
            # Handling already logged in users just connecting their accounts
            try:
                profile = OpenIDProfile.all().filter('identity=',request.GET.get('openid.claimed_id')).fetch(1)[0]
            except IndexError: # There can only be one profile with the same identity
                profile = OpenIDProfile(
                    user = request.user,
                    identity=request.GET.get('openid.claimed_id'),
                    real_name=request.GET.get('openid.ax.value.fullname'),
                    username=request.GET.get('openid.ax.value.nickname'),
                    email=request.GET.get('openid.ax.value.email'),
                    pic_url=request.GET.get('openid.ax.value.image'),
                )
                profile.save()

            return HttpResponseRedirect(_get_next(request))

        user = authenticate(identity=request.GET.get('openid.claimed_id'))
        if user is None:
            user = User(username='******')
            request.session['social_suggested_username'] = request.GET.get('openid.ax.value.nickname')
            request.session['socialregistration_user'] = user
            if request.GET.get('openid.ext2.value.email'):
                request.session['social_suggested_username'] = request.GET.get('openid.ext2.value.email').split('@')[0]
                request.session['socialregistration_profile'] = OpenIDProfile(
                    identity=request.GET.get('openid.claimed_id'),
                    internal_username=request.session['social_suggested_username'],
                    email=request.GET.get('openid.ext2.value.email'),
                    pic_url= "http://www.gravatar.com/avatar/" + md5.md5(request.GET.get('openid.ext2.value.email')).hexdigest() ,
                )
            elif request.GET.get('openid.sreg.email'):
                request.session['social_suggested_username'] = request.GET.get('openid.sreg.nickname')
                request.session['socialregistration_profile'] = OpenIDProfile(
                    identity=request.GET.get('openid.claimed_id'),
                    real_name=request.GET.get('openid.sreg.fullname'),
                    internal_username=request.GET.get('openid.sreg.nickname'),
                    email=request.GET.get('openid.sreg.email'),
                    pic_url="http://www.gravatar.com/avatar/" + md5.md5(request.GET.get('openid.sreg.email')).hexdigest(),
                )
            elif request.GET.get('openid.ext1.value.email'):
                request.session['social_suggested_username'] = request.GET.get('openid.ext1.value.email').split('@')[0]
                request.session['socialregistration_profile'] = OpenIDProfile(
                    identity=request.GET.get('openid.claimed_id'),
                    internal_username=request.session['social_suggested_username'],
                    email=request.GET.get('openid.ext1.value.email'),
                    pic_url= "http://www.gravatar.com/avatar/" + md5.md5(request.GET.get('openid.ext1.value.email')).hexdigest() ,
                )
            else:
                request.session['socialregistration_profile'] = OpenIDProfile(
                    identity=request.GET.get('openid.claimed_id'),
                    real_name=request.GET.get('openid.ax.value.fullname'),
                    internal_username=request.GET.get('openid.ax.value.nickname'),
                    email=request.GET.get('openid.ax.value.email'),
                    pic_url=request.GET.get('openid.ax.value.image'),
                )
            for key, value in getattr(client, 'registration_data', {}).items():
                request.session['social_suggested_%s' % key] = value

            return HttpResponseRedirect(reverse('socialregistration_setup'))
        else:
            login(request, user)
            return HttpResponseRedirect(_get_next(request))

        if not user.is_active:
            return render_to_response(
                account_inactive_template,
                extra_context,
                context_instance=RequestContext(request)
            )

        login(request, user)
        return HttpResponseRedirect(_get_next(request))            
    
    return render_to_response(
        template,
        dict(),
        context_instance=RequestContext(request)
    )
def openid_callback(
        request,
        template='socialregistration/openid.html',
        extra_context=dict(),
        account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (_https(), Site.objects.get_current().domain,
                           reverse('openid_callback')),
        request.session.get('openid_provider'))

    try:
        request_args = util.normalDict(request.GET)

        if request.method == 'POST':
            request_args.update(util.normalDict(request.POST))

        if request_args:
            client.complete()
            c = client.consumer

        return_to = util.getViewURL(request, openid_callback)

        response = client.result

        ax_items = {}

        if response.status == consumer.SUCCESS:
            provider = request.session.get('openid_provider')
            # Set the schema uri depending on who the openid provier is:
            # request only name and email by default (same as Google schemas):
            schemas = GoogleOpenIDSchemas()
            if 'yahoo' in provider:
                schemas = YahooOpenIDSchemas()

            if 'myopenid' in provider:
                schemas = MyOpenIDSchemas()

            ax_response = {}
            ax_response = ax.FetchResponse.fromSuccessResponse(response)
            if ax_response:
                # Name and email schemas are always set, but not others so check if they are not empty first:
                birth_date = zip = gender = []
                if schemas.birth_date_schema:
                    birth_date = ax_response.get(schemas.birth_date_schema)
                if schemas.zip_schema:
                    zip = ax_response.get(schemas.zip_schema)
                if schemas.gender_schema:
                    gender = ax_response.get(schemas.gender_schema)
                ax_items = {
                    'display_name': ax_response.get(schemas.name_schema),
                    'email': ax_response.get(schemas.email_schema),
                    'birth_date': birth_date,
                    'home_zip': zip,
                    'gender': gender,
                }

        request.session['ax_items'] = ax_items

    except Exception, e:
        pass
def openid_callback(request, template='socialregistration/openid.html',
    extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )

    try:
        request_args = util.normalDict(request.GET)

        if request.method == 'POST':
            request_args.update(util.normalDict(request.POST))

        if request_args:
            client.complete()
            c = client.consumer

        return_to = util.getViewURL(request, openid_callback)

        response = client.result

        ax_items = {}

        if response.status == consumer.SUCCESS:
            provider = request.session.get('openid_provider')
            # Set the schema uri depending on who the openid provier is:
            # request only name and email by default (same as Google schemas):
            schemas = GoogleOpenIDSchemas()
            if 'yahoo' in provider:
                schemas = YahooOpenIDSchemas()

            if 'myopenid' in provider:
                schemas = MyOpenIDSchemas()

            ax_response = {}
            ax_response = ax.FetchResponse.fromSuccessResponse(response)
            if ax_response:
                # Name and email schemas are always set, but not others so check if they are not empty first:
                birth_date = zip = gender = []
                if schemas.birth_date_schema:
                    birth_date = ax_response.get(schemas.birth_date_schema)
                if schemas.zip_schema:
                    zip =  ax_response.get(schemas.zip_schema)
                if schemas.gender_schema:
                    gender = ax_response.get(schemas.gender_schema)
                ax_items = {
                    'display_name': ax_response.get(schemas.name_schema),
                    'email': ax_response.get(schemas.email_schema),
                    'birth_date': birth_date,
                    'home_zip': zip,
                    'gender': gender,
                }

        request.session['ax_items'] = ax_items

    except Exception, e:
        pass
def facebook_js(requested_perms=""):
    return {'facebook_api_key' : getattr(settings, 'FACEBOOK_API_KEY', ''), 'is_https' : bool(_https()), 'requested_perms': requested_perms, 'MEDIA_URL': getattr(settings, 'MEDIA_URL', ''), 'STATIC_MEDIA_URL': getattr(settings, 'STATIC_MEDIA_URL', '')}
예제 #33
0
def openid_callback(request, template='socialregistration/openid.html',
    extra_context=dict(), account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (
            _https(),
            Site.objects.get_current().domain,
            reverse('openid_callback')
        ),
        request.session.get('openid_provider')
    )

    if client.is_valid():
        identity = client.result.identity_url

        if 'socialregistration_connect_object' in request.session and request.session['socialregistration_connect_object'] != None:
            # this exists so that social credentials can be attached to any arbitrary object using the same callbacks.
            # Under normal circumstances it will not be used. Put an object in request.session named 'socialregistration_connect_object' and it will be used instead.
            # After the connection is made it will redirect to request.session value 'socialregistration_connect_redirect' or settings.LOGIN_REDIRECT_URL or /
            try:
                # get the profile for this facebook UID and type of connected object
                profile = OpenIDProfile.objects.get(identity=identity, content_type=ContentType.objects.get_for_model(request.session['socialregistration_connect_object'].__class__), object_id=request.session['socialregistration_connect_object'].pk)
            except OpenIDProfile.DoesNotExist:
                OpenIDProfile.objects.create(content_object=request.session['socialregistration_connect_object'], identity=identity)

            del request.session['socialregistration_connect_object']
        else:
            if request.user.is_authenticated():
                # Handling already logged in users just connecting their accounts
                try:
                    profile = OpenIDProfile.objects.get(identity=identity, content_type=ContentType.objects.get_for_model(User))
                except OpenIDProfile.DoesNotExist: # There can only be one profile with the same identity
                    profile = OpenIDProfile.objects.create(content_object=request.user,
                        identity=identity)

                return HttpResponseRedirect(_get_next(request))

            user = authenticate(identity=identity)
            if user is None:
                request.session['socialregistration_user'] = User()
                request.session['socialregistration_profile'] = OpenIDProfile(
                    identity=identity
                )
                return HttpResponseRedirect(reverse('socialregistration_setup'))

            if not user.is_active:
                return render_to_response(
                    account_inactive_template,
                    extra_context,
                    context_instance=RequestContext(request)
                )

            login(request, user)
        return HttpResponseRedirect(_get_next(request))

    return render_to_response(
        template,
        dict(),
        context_instance=RequestContext(request)
    )
예제 #34
0
def hyves_js():
    return {
        'hyves_consumer_key': settings.HYVES_CONSUMER_KEY,
        'site': Site.objects.get_current(),
        'is_https': bool(_https())
    }
def facebook_js():
    return {'facebook_api_key' : getattr(settings, 'FACEBOOK_API_KEY', ''), 'is_https' : bool(_https())}
def openid_callback(
        request,
        template='socialregistration/openid.html',
        extra_context=dict(),
        account_inactive_template='socialregistration/account_inactive.html'):
    """
    Catches the user when he's redirected back from the provider to our site
    """
    client = OpenID(
        request,
        'http%s://%s%s' % (_https(), Site.objects.get_current().domain,
                           reverse('openid_callback')),
        request.session.get('openid_provider'))

    if client.is_valid():
        identity = client.result.identity_url

        if 'socialregistration_connect_object' in request.session and request.session[
                'socialregistration_connect_object'] != None:
            # this exists so that social credentials can be attached to any arbitrary object using the same callbacks.
            # Under normal circumstances it will not be used. Put an object in request.session named 'socialregistration_connect_object' and it will be used instead.
            # After the connection is made it will redirect to request.session value 'socialregistration_connect_redirect' or settings.LOGIN_REDIRECT_URL or /
            try:
                # get the profile for this facebook UID and type of connected object
                profile = OpenIDProfile.objects.get(
                    identity=identity,
                    content_type=ContentType.objects.get_for_model(
                        request.session['socialregistration_connect_object'].
                        __class__),
                    object_id=request.
                    session['socialregistration_connect_object'].pk)
            except OpenIDProfile.DoesNotExist:
                OpenIDProfile.objects.create(
                    content_object=request.
                    session['socialregistration_connect_object'],
                    identity=identity)

            del request.session['socialregistration_connect_object']
        else:
            if request.user.is_authenticated():
                # Handling already logged in users just connecting their accounts
                try:
                    profile = OpenIDProfile.objects.get(
                        identity=identity,
                        content_type=ContentType.objects.get_for_model(User))
                except OpenIDProfile.DoesNotExist:  # There can only be one profile with the same identity
                    profile = OpenIDProfile.objects.create(
                        content_object=request.user, identity=identity)

                return HttpResponseRedirect(_get_next(request))

            user = authenticate(identity=identity)
            if user is None:
                request.session['socialregistration_user'] = User()
                request.session['socialregistration_profile'] = OpenIDProfile(
                    identity=identity)
                return HttpResponseRedirect(
                    reverse('socialregistration_setup'))

            if not user.is_active:
                return render_to_response(
                    account_inactive_template,
                    extra_context,
                    context_instance=RequestContext(request))

            login(request, user)
        return HttpResponseRedirect(_get_next(request))

    return render_to_response(template,
                              dict(),
                              context_instance=RequestContext(request))