Ejemplo n.º 1
0
        private BridgeUser GetByRedirectUriAndProfileId(string uri, string profileId)
        {
            Uri    parsed    = new Uri(uri);
            string authority = parsed.Host;

            // TODO: access DB
            BridgeUser user1 = new BridgeUser()
            {
                ProfileId = "818727",
                AccountFriendlySubdomain = "business.yrudakova.name",
                AccountTechSubdomain     = "localhost",
                AccountId = 54,
                UserGuid  = "e37d71d3-62bb-4534-8d0b-dbe45ac40cbe"
            };
            BridgeUser user2 = new BridgeUser()
            {
                ProfileId = "818727",
                AccountFriendlySubdomain = "sales.yrudakova.name",
                AccountTechSubdomain     = "testtech.yrudakova.name",
                AccountId = 97,
                UserGuid  = "422f7eec-8712-4cb0-bc95-d4ecc54b46c4"
            };

            List <BridgeUser> users = new List <BridgeUser>
            {
                user1,
                user2
            };

            return(users.FirstOrDefault(x => x.ProfileId == profileId &&
                                        (authority == x.AccountTechSubdomain ||
                                         authority == x.AccountFriendlySubdomain)));
        }
Ejemplo n.º 2
0
        public Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            if (context.Client.ClientId != "myclient")
            {
                return(Task.FromResult(0));
            }

            context.IssuedClaims = context.Subject.Claims.ToList();

            // Userinfo endpoint = we don't know the request
            if (context.ValidatedRequest == null ||
                !(context.ValidatedRequest is ValidatedTokenRequest))
            {
                return(Task.FromResult(0));
            }

            var    contextValidatedRequest = (ValidatedTokenRequest)context.ValidatedRequest;
            string uri = contextValidatedRequest.AuthorizationCode.RedirectUri;

            //string profileId = contextValidatedRequest.AuthorizationCode.Subject.Claims.First(x => x.Type == "sub").Value;
            string profileId = context.IssuedClaims.First(x => x.Type == "sub").Value;

            BridgeUser user = GetByRedirectUriAndProfileId(uri, profileId);

            if (user == null)
            {
                return(Task.FromResult(0));
            }

            var claims = context.IssuedClaims;

            AddUserClaims(user, claims);
            context.IssuedClaims = claims;
            return(Task.FromResult(0));
        }
Ejemplo n.º 3
0
        private void AddUserClaims(BridgeUser user, List <Claim> claims)
        {
            UpsertClaim(claims, new Claim("friendly_subdomain", user.AccountFriendlySubdomain));
            UpsertClaim(claims, new Claim("tech_subdomain", user.AccountTechSubdomain));

            UpsertClaim(claims, new Claim("account_id", user.AccountId.ToString()));
            UpsertClaim(claims, new Claim("profile_id", user.ProfileId));
            UpsertClaim(claims, new Claim("user_guid", user.UserGuid));
        }
Ejemplo n.º 4
0
        private void initDiscordCommands()
        {
            // Note: ParameterType.Unparsed catches all remaining text as a single optional parameter
            Client.GetService <CommandService>().CreateCommand("do")
            .Alias("execute", "run")
            .Description("Executes a TShock command.")
            .Parameter("command", ParameterType.Required)
            .Parameter("parameters", ParameterType.Unparsed)
            .Do(async e =>
            {
                BridgeUser player = await Client.LoadUser(e.User);

                if (!player.IsLoggedIn)
                {
                    await e.User.SendMessage("You must be logged in to use TShock commands.\n"
                                             + $"Message me with `{Config.BotPrefix}login <username> <password>` using your TShock credentials to begin.");
                    return;
                }

                // Blacklist commands which must be run through their discord command counterparts
                var blacklist = new List <string>
                {
                    "login",
                    "logout"
                };

                if (blacklist.Contains(e.GetArg("command")))
                {
                    await e.Channel.SendMessage($"This is a discord command, so use `{Config.BotPrefix}{e.GetArg("command")}` (without the `{Config.BotPrefix}do` prefix) instead.");
                    return;
                }

                TSCommand command = Commands.ChatCommands.Find(c => !c.Names.Contains("login") &&
                                                               !c.Names.Contains("logout") &&
                                                               c.Names.Contains(e.GetArg("command")));

                if (command == null && !player.AwaitingResponse.ContainsKey(e.GetArg("command")))
                {
                    await e.Channel.SendMessage($"`{e.GetArg("command")}` is not a TShock command.");
                    return;
                }

                var sb = new StringBuilder();

                if (!e.GetArg("command").StartsWith(Commands.Specifier) && !e.GetArg("command").StartsWith(Commands.Specifier))
                {
                    sb.Append(Commands.Specifier);
                }

                // Temporarily set their command channel so that messages end in the right place
                player.CommandChannel = e.Channel;

                // Disable auto flush to reduce consumption of the discord API for multiple messages
                player.AutoFlush = false;

                if (Commands.HandleCommand(player, sb.Append(e.GetArg("command")).Append(' ').Append(e.GetArg("parameters")).ToString()))
                {
                    await player.FlushMessages();
                }
                else
                {
                    await e.Channel.SendMessage("Command failed, check logs for details.");
                }

                player.AutoFlush      = true;
                player.CommandChannel = null;
            });

            #region Account Commands

            Client.GetService <CommandService>().CreateCommand("login")
            .Description("Log in to a TShock user account to use its permissions when using the `do` command.")
            .Parameter("username", ParameterType.Required)
            .Parameter("password", ParameterType.Required)
            .Do(async e =>
            {
                BridgeUser player = await Client.LoadUser(e.User);

                if (e.Channel != e.User.PrivateChannel)
                {
                    // Delete the message
                    await e.Message.Delete();
                }

                if (player.IsLoggedIn)
                {
                    await e.Channel.SendMessage($"You are already logged in. Use `{Config.BotPrefix}logout` first if you wish to log in to a different account.");
                    return;
                }

                string username = e.GetArg("username");
                string password = e.GetArg("password");

                TShockAPI.DB.User user = TShock.Users.GetUserByName(username);
                if (user == null)
                {
                    await e.Channel.SendMessage("A user by that name does not exist.");
                }

                else if (!user.VerifyPassword(password))
                {
                    await e.Channel.SendMessage("Invalid password!");
                }

                else
                {
                    await Logins.SetData(e.User, user);
                    player = await Logins.Authenticate(e.User.Id);
                    await e.Channel.SendMessage($"Authenticated as {player.Name} successfully.");
                }
            });

            Client.GetService <CommandService>().CreateCommand("logout")
            .Description("Log out of your current TShock user account.")
            .Do(async e =>
            {
                BridgeUser player = await Client.LoadUser(e.User);

                if (!player.IsLoggedIn)
                {
                    await e.Channel.SendMessage("You are not logged in.");
                    return;
                }

                await Logins.RemoveData(e.User.Id);
                await Logins.Authenticate(e.User.Id);
                await e.Channel.SendMessage("You have been successfully logged out of your account.");
            });

            #endregion

            #region Administrative Commands

            Client.GetService <CommandService>().CreateCommand("echo")
            .Alias("bc", "say")
            .Description("Make this bot say something.")
            .Parameter("text", ParameterType.Unparsed)
            .AddCheck((c, u, ch) => !ch.IsPrivate)
            .AddCheck((cmd, user, channel) => user.ServerPermissions.Administrator)
            .Do(async e => await e.Channel.SendMessage(e.GetArg("text") ?? "Hi!"));

            Client.GetService <CommandService>().CreateCommand("addbot")
            .Description("Add another connected Discord Bridge bot to the list of ServerBots for multi-server broadcasting.")
            .Parameter("name", ParameterType.Required)
            .AddCheck((cmd, user, channel) => user.ServerPermissions.Administrator)
            .Do(async e =>
            {
                User botUser = Client.CurrentServer.FindUsers(e.GetArg("name"), true).FirstOrDefault();

                if (botUser == null &&
                    (botUser = Client.CurrentServer.Users.FirstOrDefault(u => u.Nickname.Equals(e.GetArg("name"), StringComparison.OrdinalIgnoreCase))) == null)
                {
                    await e.Channel.SendMessage($"User `{e.GetArg("name")}` is not on this server.");
                    return;
                }

                if (!botUser.IsBot)
                {
                    await e.Channel.SendMessage($"`{e.GetArg("name")}` is not a bot.");
                    return;
                }

                string mention = String.IsNullOrWhiteSpace(botUser.Nickname) ? botUser.Mention : botUser.NicknameMention;
                if (Config.AddBot(botUser.Id))
                {
                    await e.Channel.SendMessage($"Added {mention} to the broadcasting list.");
                }
                else
                {
                    await e.Channel.SendMessage($"{mention} is already on the broadcast list.");
                }
            });

            #endregion
        }