Esempio n. 1
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());
        }
Esempio n. 2
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());
        }
Esempio n. 3
0
        public static async Task <Task> VerifyPayload(
            ServerState state,
            HTTPRequest request,
            Stream body,
            IProxyHTTPEncoder encoder)
        {
            var req = await Util.ReadJsonObjectFromStreamAsync <AuthVerifyPayloadRequest>(body, 1024 * 1024);

            var user = state.VerifyPayload(req.challenge, req.chash, req.phash);

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

            var resp = new AuthCheckResponse()
            {
                payload = "",
                success = true,
                user    = user,
            };

            return(await encoder.Response(200, "OK").ContentType_JSON().SendJsonFromObject(resp));
        }
Esempio n. 4
0
        public static async Task <Task> IsLoginValid(
            ServerState state,
            HTTPRequest request,
            Stream body,
            IProxyHTTPEncoder encoder)
        {
            var msg = await Util.ReadJsonObjectFromStreamAsync <Msg>(body, 1024);

            bool valid = false;

            User user;

            if (msg.payload == null || msg.auth.hash == null)
            {
                // Ensure the payload can never be accidentally used since this
                // authentication is without a payload hash.
                msg.payload = null;

                user = state.Verify(msg.auth.challenge, msg.auth.chash);

                if (user != null)
                {
                    valid = true;
                }
            }
            else
            {
                var payload_hash = BitConverter.ToString(
                    new SHA512Managed().ComputeHash(
                        Encoding.UTF8.GetBytes(msg.payload)
                        )
                    ).Replace("-", "").ToLower();

                user = state.VerifyPayload(
                    msg.auth.challenge,
                    msg.auth.chash,
                    payload_hash /* recompute it */
                    );

                if (user != null)
                {
                    valid = true;
                }
            }

            if (valid)
            {
                return(await encoder.Response(200, "Login Valid")
                       .CacheControlDoNotCache()
                       .ContentType_JSON()
                       .SendJsonFromObject(new AuthLoginValidResponse()
                {
                    success = true,
                    user = user,
                }));
            }
            else
            {
                return(await encoder.Response(403, "The login was not valid.")
                       .CacheControlDoNotCache()
                       .ContentType_JSON()
                       .SendJsonFromObject(new AuthLoginValidResponse()
                {
                    success = false,
                    user = null,
                }));
            }
        }