Exemple #1
0
        public static async Task <Task> UserList(
            ServerState state,
            HTTPRequest request,
            Stream body,
            IProxyHTTPEncoder encoder)
        {
            var msg = await Util.ReadJsonObjectFromStreamAsync <Msg>(body, 1024);

            var(user, req) = state.AuthenticateMessage <JObject>(msg);

            if (user == null)
            {
                return(await encoder.Response(403, "The user list request was denied due to an authentication failure.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }


            var users = state.GetUserList();

            return(await encoder.Response(200, "OK")
                   .ContentType_JSON()
                   .SendJsonFromObject(state.GetUserList()));
        }
Exemple #2
0
        public static async Task <Task> UserSet(
            ServerState state,
            HTTPRequest request,
            Stream body,
            IProxyHTTPEncoder encoder)
        {
            var msg = await Util.ReadJsonObjectFromStreamAsync <Msg>(body, 1024);

            var(user, req) = state.AuthenticateMessage <AuthUserSetRequest>(msg);

            if (user == null)
            {
                return(await encoder.Response(403, "Authentication based on user failed.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }

            if (!user.admin && user.user != req.user.user)
            {
                return(await encoder.Response(403, "Disallowed modification of another user.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }

            if (!await state.SetUser(req.user))
            {
                return(await encoder.Response(500, "The set user command failed to execute.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }

            return(await encoder.Response(200, "OK")
                   .ContentType("text/plain")
                   .CacheControlDoNotCache()
                   .SendNothing());
        }
Exemple #3
0
        public static async Task <Task> UserDelete(
            ServerState state,
            HTTPRequest request,
            Stream body,
            IProxyHTTPEncoder encoder)
        {
            var msg = await Util.ReadJsonObjectFromStreamAsync <Msg>(body, 1024);

            var(user, req) = state.AuthenticateMessage <AuthUserDeleteRequest>(msg);

            if (user == null)
            {
                return(await encoder.Response(403, "Authentication failed for the user used.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }

            if (!user.admin)
            {
                return(await encoder.Response(403, "Disallowed delete of user by non-administrator.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }

            if (!await state.DeleteUser(req.username))
            {
                return(await encoder.Response(500, "The delete user command failed on the server.")
                       .ContentType("text/plain")
                       .CacheControlDoNotCache()
                       .SendNothing());
            }

            return(await encoder.Response(200, "OK")
                   .ContentType("text/plain")
                   .CacheControlDoNotCache()
                   .SendNothing());
        }