Exemplo n.º 1
0
        public override async Task GrantResourceOwnerCredentials(Microsoft.Owin.Security.OAuth.OAuthGrantResourceOwnerCredentialsContext context)
        {
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

            if (String.IsNullOrEmpty(context.UserName))
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                context.Rejected();
                return;
            }

            if (OTA.Protection.IpLimiting.Register(context.Request.RemoteIpAddress, ServerManager.MaxRequestsPerLapse, ServerManager.RequestLockoutDuration))
            {
                //Prevent console spamming
                if (OTA.Protection.IpLimiting.GetJustLockedOut(context.Request.RemoteIpAddress))
                {
                    ProgramLog.Web.Log("API client reached request limit for user/ip {0}", context.UserName, context.Request.RemoteIpAddress);
                }

                context.SetError("request_limit", "You have reached the service limit");
                context.Rejected();
                return;
            }

            var user = await APIAccountManager.FindByNameAsync(context.UserName);

            if (user != null && user.ComparePassword(context.Password))
            {
                var identity = new ClaimsIdentity(context.Options.AuthenticationType);
                identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));

                //Load permissions for user
                foreach (var role in await APIAccountManager.GetRolesForAccount(user.Id))
                {
                    identity.AddClaim(new Claim(role.Type, role.Value));
                    //                    identity.AddClaim(new Claim(ClaimTypes.Role, "player"));
                }

                //                    var ticket = new AuthenticationTicket(identity, new AuthenticationProperties()
                //                        {
                //                            IsPersistent = true,
                //                            IssuedUtc = DateTime.UtcNow
                //                        });
                context.Validated(identity);
            }
            else
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                context.Rejected();
            }
        }
Exemplo n.º 2
0
        void AddAccount(ISender sender, ArgumentList args)
        {
            if (!Storage.IsAvailable)
            {
                throw new CommandError("No permissions plugin or data plugin is attached");
            }

            var        a = 0;
            string     name, pass;
            APIAccount acc = null;

            //api addaccount "username" "password"
            if (!args.TryGetString(a++, out name))
            {
                throw new CommandError("Expected username after addaccount");
            }

            if (!args.TryGetString(a++, out pass))
            {
                throw new CommandError("Expected password after username");
            }

            acc = APIAccountManager.FindByName(name);
            if (acc == null)
            {
                acc = APIAccountManager.Create(name, pass);
                if (acc.Id > 0)
                {
                    sender.SendMessage("Successfully created account.", R: 0, B: 0);
                }
                else
                {
                    sender.SendMessage("Failed to create account.", G: 0, B: 0);
                }
            }
            else
            {
                throw new CommandError("Existing API account found by " + name);
            }
        }
Exemplo n.º 3
0
        void ManageApi(ISender sender, ArgumentList args)
        {
            if (!Storage.IsAvailable)
            {
                throw new CommandError("No permissions plugin or data plugin is attached");
            }

            var        a = 0;
            string     name, pass, type, value;
            APIAccount acc = null;
            var        cmd = args.GetString(a++);

            switch (cmd)
            {
            case "addaccount":
                //api addaccount "username" "password"
                if (!args.TryGetString(a++, out name))
                {
                    throw new CommandError("Expected username after [" + cmd + "]");
                }

                if (!args.TryGetString(a++, out pass))
                {
                    throw new CommandError("Expected password after username");
                }

                acc = APIAccountManager.FindByName(name);
                if (acc == null)
                {
                    acc = APIAccountManager.Create(name, pass);
                    if (acc.Id > 0)
                    {
                        sender.SendMessage("Successfully created account.", R: 0, B: 0);
                    }
                    else
                    {
                        sender.SendMessage("Failed to create account.", G: 0, B: 0);
                    }
                }
                else
                {
                    throw new CommandError("Existing API account found by " + name);
                }
                break;

            case "removeaccount":
                //api removeaccount "username"
                if (!args.TryGetString(a++, out name))
                {
                    throw new CommandError("Expected username after [" + cmd + "]");
                }

                acc = APIAccountManager.FindByName(name);
                if (acc != null)
                {
                    if (APIAccountManager.DeleteAccount(acc.Id))
                    {
                        sender.SendMessage("Successfully removed account.", R: 0, B: 0);
                    }
                    else
                    {
                        sender.SendMessage("Failed to remove account.", G: 0, B: 0);
                    }
                }
                else
                {
                    throw new CommandError("No API account found by " + name);
                }
                break;

            case "addrole":
                //api addrole "account" "type" "value"
                if (!args.TryGetString(a++, out name))
                {
                    throw new CommandError("Expected username after [" + cmd + "]");
                }

                if (!args.TryGetString(a++, out type))
                {
                    throw new CommandError("Expected type after username");
                }

                if (!args.TryGetString(a++, out value))
                {
                    throw new CommandError("Expected value after type");
                }

                acc = APIAccountManager.FindByName(name);
                if (acc != null)
                {
                    var role = APIAccountManager.AddType(acc.Id, type, value);
                    if (role != null && role.Id > 0)
                    {
                        sender.SendMessage("Successfully added role account.", R: 0, B: 0);
                    }
                    else
                    {
                        sender.SendMessage("Failed to add role to account.", G: 0, B: 0);
                    }
                }
                else
                {
                    throw new CommandError("No API account found by " + name);
                }
                break;

            case "removerole":
                //api removerole "account" "type" "value"
                if (!args.TryGetString(a++, out name))
                {
                    throw new CommandError("Expected username after [" + cmd + "]");
                }

                if (!args.TryGetString(a++, out type))
                {
                    throw new CommandError("Expected type after username");
                }

                if (!args.TryGetString(a++, out value))
                {
                    throw new CommandError("Expected value after type");
                }

                acc = APIAccountManager.FindByName(name);
                if (acc != null)
                {
                    var role = APIAccountManager.DeleteType(acc.Id, type, value);
                    if (role)
                    {
                        sender.SendMessage("Successfully removed role account.", R: 0, B: 0);
                    }
                    else
                    {
                        sender.SendMessage("Failed to removed role from account.", G: 0, B: 0);
                    }
                }
                else
                {
                    throw new CommandError("No API account found by " + name);
                }
                break;

            case "search":
                //api search "part"
                if (!args.TryGetString(a++, out name))
                {
                    throw new CommandError("Expected part of a acount name after [" + cmd + "]");
                }

                var matches = APIAccountManager.FindAccountsByPrefix(name);
                if (matches != null && matches.Length > 0)
                {
                    sender.Message("Matches:");
                    foreach (var mth in matches)
                    {
                        sender.Message("\t" + mth);
                    }
                }
                else
                {
                    sender.Message("There are no registered accounts matching " + name);
                }
                break;

            default:
                throw new CommandError("Invalid command " + cmd);
            }
        }