Beispiel #1
0
def ip_to_geoip(ip_address):
    return get_geoip_string(ip_address)
Beispiel #2
0
def ip_to_geoip(ip_address):
    return get_geoip_string(ip_address)
Beispiel #3
0
def sessions(request):
    """ Shows sessions to the user. """
    user = request.browser.user
    ret = {}
    if request.method == "POST":
        if request.POST.get("logout"):
            bid_public = request.POST.get("logout")
            if bid_public == "all":
                # Log out all sessions
                custom_log(request, "sessions: user requested signing out all sessions", level="info")
                bid_public = [obj.bid_public for obj in Browser.objects.filter(user=user).exclude(bid_public=request.browser.bid_public)]
            else:
                bid_public = [bid_public]

            custom_log(request, "sessions: signing out sessions: %s" % bid_public, level="debug")

            self_logout = False
            for bid in bid_public:
                try:
                    browser_logout = Browser.objects.get(bid_public=bid)
                    if browser_logout.user != user:
                        custom_log(request, "sessions: Tried to sign out browser that belongs to another user: %s" % bid, level="warn")
                        ret["message"] = "That browser belongs to another user."
                    else:
                        if browser_logout == request.browser:
                            custom_log(request, "sessions: signing out current browser", level="info")
                            self_logout = True
                        browser_identification = browser_logout.get_readable_ua()
                        if browser_logout.name:
                            browser_identification = "%s (%s)" % (browser_logout.name, browser_identification)
                        request_browser_identification = request.browser.get_readable_ua()
                        if request.browser.name:
                            request_browser_identification = "%s (%s)" % (request.browser.name, request_browser_identification)

                        browser_logout.logout()
                        browser_logout.forced_sign_out = True
                        browser_logout.save()

                        custom_log(request, "sessions: Signed out browser %s" % browser_logout.bid_public, level="info")
                        add_user_log(request, "Signed out browser %s" % browser_identification, "sign-out")
                        if not self_logout:
                            add_user_log(request, "Signed out from browser %s" % request_browser_identification, "sign-out", bid_public=browser_logout.bid_public)
                        messages.success(request, "Signed out browser %s" % browser_identification)
                except Browser.DoesNotExist:
                    ret["message"] = "Invalid browser"

            if self_logout:
                get_params = request.GET.dict()
                get_params["logout"] = "on"
                return redirect_with_get_params("login_frontend.views.logoutview", get_params)

        elif request.POST.get("action") == "rename":
            try:
                abrowser = Browser.objects.get(bid_public=request.POST.get("bid_public"))
                if abrowser.user != request.browser.user:
                    raise Browser.DoesNotExist

            except Browser.DoesNotExist:
                messages.warning(request, "Invalid browser. Your changes were not saved")
                return redirect_with_get_params("login_frontend.views.sessions", request.GET)
            val = request.POST.get("name")
            abrowser.name = val
            abrowser.save()
            if val:
                messages.success(request, "Browser was renamed as '%s'" % val)
            else:
                messages.success(request, "Browser name was removed")
        return redirect_with_get_params("login_frontend.views.sessions", request.GET)

    browsers = Browser.objects.filter(user=user)
    sessions = []
    for browser in browsers:
        session = BrowserUsers.objects.get(user=user, browser=browser)
        details = {"session": session, "browser": browser}
        if browser == request.browser:
            details["this_session"] = True
        details["geo"] = get_geoip_string(session.remote_ip)
        details["icons"] = browser.get_ua_icons()

        try:
            details["p0f"] = BrowserP0f.objects.filter(browser=browser).latest()
        except BrowserP0f.DoesNotExist:
            pass

        try:
            details["timesync"] = BrowserTime.objects.filter(browser=browser).latest()
        except BrowserTime.DoesNotExist:
            pass

        logins = BrowserLogin.objects.filter(user=user, browser=browser).filter(can_logout=False).filter(signed_out=False).filter(Q(expires_at__gte=timezone.now()) | Q(expires_at=None)).filter(expires_at__lte=timezone.now()+datetime.timedelta(days=30))
        details["logins"] = logins
        cache_keys = [("last_known_location", "last-known-location-%s"), ("last_known_location_from", "last-known-location-from-%s"), ("last_known_location_timestamp", "last-known-location-timestamp-%s")]
        for tk, k in cache_keys:
            r_k = k % browser.bid_public
            val = bcache.get(r_k)
            if val:
                if tk == "last_known_location_timestamp":
                    val = datetime.datetime.fromtimestamp(float(val))
                details[tk] = val

        sessions.append(details)
    try:
        sessions.sort(key=lambda item:item.get("session").last_seen, reverse=True)
    except Exception, e:
        # In certain cases, session.last_seen is None.
        custom_log(request, "Unable to sort sessions: %s" % e, level="error")
Beispiel #4
0
def sessions(request):
    """ Shows sessions to the user. """
    user = request.browser.user
    ret = {}
    if request.method == "POST":
        if request.POST.get("logout"):
            bid_public = request.POST.get("logout")
            if bid_public == "all":
                # Log out all sessions
                custom_log(request,
                           "sessions: user requested signing out all sessions",
                           level="info")
                bid_public = [
                    obj.bid_public
                    for obj in Browser.objects.filter(user=user).exclude(
                        bid_public=request.browser.bid_public)
                ]
            else:
                bid_public = [bid_public]

            custom_log(request,
                       "sessions: signing out sessions: %s" % bid_public,
                       level="debug")

            self_logout = False
            for bid in bid_public:
                try:
                    browser_logout = Browser.objects.get(bid_public=bid)
                    if browser_logout.user != user:
                        custom_log(
                            request,
                            "sessions: Tried to sign out browser that belongs to another user: %s"
                            % bid,
                            level="warn")
                        ret["message"] = "That browser belongs to another user."
                    else:
                        if browser_logout == request.browser:
                            custom_log(request,
                                       "sessions: signing out current browser",
                                       level="info")
                            self_logout = True
                        browser_identification = browser_logout.get_readable_ua(
                        )
                        if browser_logout.name:
                            browser_identification = "%s (%s)" % (
                                browser_logout.name, browser_identification)
                        request_browser_identification = request.browser.get_readable_ua(
                        )
                        if request.browser.name:
                            request_browser_identification = "%s (%s)" % (
                                request.browser.name,
                                request_browser_identification)

                        browser_logout.logout()
                        browser_logout.forced_sign_out = True
                        browser_logout.save()

                        custom_log(request,
                                   "sessions: Signed out browser %s" %
                                   browser_logout.bid_public,
                                   level="info")
                        add_user_log(
                            request,
                            "Signed out browser %s" % browser_identification,
                            "sign-out")
                        if not self_logout:
                            add_user_log(request,
                                         "Signed out from browser %s" %
                                         request_browser_identification,
                                         "sign-out",
                                         bid_public=browser_logout.bid_public)
                        messages.success(
                            request,
                            "Signed out browser %s" % browser_identification)
                except Browser.DoesNotExist:
                    ret["message"] = "Invalid browser"

            if self_logout:
                get_params = request.GET.dict()
                get_params["logout"] = "on"
                return redirect_with_get_params(
                    "login_frontend.views.logoutview", get_params)

        elif request.POST.get("action") == "rename":
            try:
                abrowser = Browser.objects.get(
                    bid_public=request.POST.get("bid_public"))
                if abrowser.user != request.browser.user:
                    raise Browser.DoesNotExist

            except Browser.DoesNotExist:
                messages.warning(
                    request, "Invalid browser. Your changes were not saved")
                return redirect_with_get_params(
                    "login_frontend.views.sessions", request.GET)
            val = request.POST.get("name")
            abrowser.name = val
            abrowser.save()
            if val:
                messages.success(request, "Browser was renamed as '%s'" % val)
            else:
                messages.success(request, "Browser name was removed")
        return redirect_with_get_params("login_frontend.views.sessions",
                                        request.GET)

    browsers = Browser.objects.filter(user=user)
    sessions = []
    for browser in browsers:
        session = BrowserUsers.objects.get(user=user, browser=browser)
        details = {"session": session, "browser": browser}
        if browser == request.browser:
            details["this_session"] = True
        details["geo"] = get_geoip_string(session.remote_ip)
        details["icons"] = browser.get_ua_icons()

        try:
            details["p0f"] = BrowserP0f.objects.filter(
                browser=browser).latest()
        except BrowserP0f.DoesNotExist:
            pass

        try:
            details["timesync"] = BrowserTime.objects.filter(
                browser=browser).latest()
        except BrowserTime.DoesNotExist:
            pass

        logins = BrowserLogin.objects.filter(
            user=user, browser=browser).filter(can_logout=False).filter(
                signed_out=False).filter(
                    Q(expires_at__gte=timezone.now()) | Q(expires_at=None))
        details["logins"] = logins
        cache_keys = [("last_known_location", "last-known-location-%s"),
                      ("last_known_location_from",
                       "last-known-location-from-%s"),
                      ("last_known_location_timestamp",
                       "last-known-location-timestamp-%s")]
        for tk, k in cache_keys:
            r_k = k % browser.bid_public
            val = dcache.get(r_k)
            if val:
                if tk == "last_known_location_timestamp":
                    val = datetime.datetime.fromtimestamp(float(val))
                details[tk] = val

        sessions.append(details)
    try:
        sessions.sort(key=lambda item: item.get("session").last_seen,
                      reverse=True)
    except Exception, e:
        # In certain cases, session.last_seen is None.
        custom_log(request, "Unable to sort sessions: %s" % e, level="error")