Example #1
0
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
    """Parse a query given as a string argument.

    Arguments:

    qs: URL-encoded query string to be parsed

    keep_blank_values: flag indicating whether blank values in
        URL encoded queries should be treated as blank strings.  A
        true value indicates that blanks should be retained as blank
        strings.  The default false value indicates that blank values
        are to be ignored and treated as if they were  not included.

    strict_parsing: flag indicating what to do with parsing errors. If
        false (the default), errors are silently ignored. If true,
        errors raise a ValueError exception.

    Returns a list, as G-d intended.
    """
    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
    r = []
    for name_value in pairs:
        nv = name_value.split('=', 1)
        if len(nv) != 2:
            if strict_parsing:
                raise ValueError, "bad query field: %s" % `name_value`
            continue
        if len(nv[1]) or keep_blank_values:
            name = urlparse.unquote(nv[0].replace('+', ' '))
            value = urlparse.unquote(nv[1].replace('+', ' '))
            r.append((name, value))

    return r
Example #2
0
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0):
    """Parse a query given as a string argument.

    Arguments:

    qs: URL-encoded query string to be parsed

    keep_blank_values: flag indicating whether blank values in
        URL encoded queries should be treated as blank strings.  A
        true value indicates that blanks should be retained as blank
        strings.  The default false value indicates that blank values
        are to be ignored and treated as if they were  not included.

    strict_parsing: flag indicating what to do with parsing errors. If
        false (the default), errors are silently ignored. If true,
        errors raise a ValueError exception.

    Returns a list, as G-d intended.
    """
    pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')]
    r = []
    for name_value in pairs:
        nv = name_value.split('=', 1)
        if len(nv) != 2:
            if strict_parsing:
                raise ValueError, "bad query field: %s" % ` name_value `
            continue
        if len(nv[1]) or keep_blank_values:
            name = urlparse.unquote(nv[0].replace('+', ' '))
            value = urlparse.unquote(nv[1].replace('+', ' '))
            r.append((name, value))

    return r
Example #3
0
def handle_async_authentication(request):
    name = request.POST[b"username"]
    cram = request.POST[b"password"]
    key = request.POST[b"key"]
    redir = request.POST[b"redir"]
    dbsession = models.SessionMaker()
    try:
        user = dbsession.query(models.User).filter_by(username=name).one()
    except models.NoResultFound:
        return framework.JSONResponse((401, b"No such user."))
    try:
        good = Authenticator(user).authenticate(cram, unquote(key))
    except AuthenticationError as err:
        request.log_error("Did NOT authenticate by async: %r\n" % (name, ))
        return framework.JSONResponse((401, str(err)))
    if good:
        request.log_error("Authenticated by async: %s\n" % (name, ))
        user.set_last_login()
        resp = framework.JSONResponse((200, redir))
        session = _set_session(resp, user, request.get_host(), "/",
                               request.config.get("LOGIN_TIME", 24))
        dbsession.add(session)
        dbsession.commit()
        dbsession.close()
        return resp
    else:
        request.log_error("Invalid async authentication for %r\n" % (name, ))
        return framework.JSONResponse((401, "Failed to authenticate."))
Example #4
0
def handle_async_authentication(request):
    name = request.POST[b"username"]
    cram = request.POST[b"password"]
    key = request.POST[b"key"]
    redir = request.POST[b"redir"]
    dbsession = models.SessionMaker()
    try:
        user = dbsession.query(models.User).filter_by(username=name).one()
    except models.NoResultFound:
        return framework.JSONResponse((401, b"No such user."))
    try:
        good = Authenticator(user).authenticate(cram, unquote(key))
    except AuthenticationError as err:
        request.log_error("Did NOT authenticate by async: %r\n" % (name, ))
        return framework.JSONResponse((401, str(err)))
    if good:
        request.log_error("Authenticated by async: %s\n" % (name,))
        user.set_last_login()
        resp = framework.JSONResponse((200, redir))
        session = _set_session(resp, user, request.get_host(), "/",
                    request.config.get("LOGIN_TIME", 24))
        dbsession.add(session)
        dbsession.commit()
        dbsession.close()
        return resp
    else:
        request.log_error("Invalid async authentication for %r\n" % (name, ))
        return framework.JSONResponse((401, "Failed to authenticate."))
Example #5
0
def handle_async_authentication(request):
    name = request.POST[b"username"]
    cram = request.POST[b"password"]
    key = request.POST[b"key"]
    redir = request.POST[b"redir"]
    try:
        user = models.dbsession.query(models.User).filter_by(username=name).one()
    except models.NoResultFound:
        return framework.JSONResponse((401, b"No such user."))
    try:
        good = Authenticator(user).authenticate(cram, unquote(key))
    except AuthenticationError, err:
        request.log_error("Did NOT authenticate by async: %r\n" % (name, ))
        return framework.JSONResponse((401, str(err)))
Example #6
0
def handle_async_authentication(request):
    name = request.POST[b"username"]
    cram = request.POST[b"password"]
    key = request.POST[b"key"]
    redir = request.POST[b"redir"]
    dbsession = models.SessionMaker()
    try:
        user = dbsession.query(models.User).filter_by(username=name).one()
    except models.NoResultFound:
        return framework.JSONResponse((401, b"No such user."))
    try:
        good = Authenticator(user).authenticate(cram, unquote(key))
    except AuthenticationError, err:
        request.log_error("Did NOT authenticate by async: %r\n" % (name, ))
        return framework.JSONResponse((401, str(err)))