Search in sources :

Example 1 with UserAPIAccount

use of com.cloudcraftgaming.discal.api.object.web.UserAPIAccount in project DisCal-Discord-Bot by NovaFox161.

the class DatabaseManager method getAPIAccount.

public UserAPIAccount getAPIAccount(String APIKey) {
    try {
        if (databaseInfo.getMySQL().checkConnection()) {
            String dataTableName = String.format("%sapi", databaseInfo.getPrefix());
            String query = "SELECT * FROM " + dataTableName + " WHERE API_KEY = '" + APIKey + "';";
            PreparedStatement statement = databaseInfo.getConnection().prepareStatement(query);
            ResultSet res = statement.executeQuery();
            Boolean hasStuff = res.next();
            if (hasStuff && res.getString("API_KEY") != null) {
                UserAPIAccount account = new UserAPIAccount();
                account.setAPIKey(APIKey);
                account.setUserId(res.getString("USER_ID"));
                account.setBlocked(res.getBoolean("BLOCKED"));
                account.setTimeIssued(res.getLong("TIME_ISSUED"));
                account.setUses(res.getInt("USES"));
                statement.close();
                return account;
            } else {
                // Data not present.
                statement.close();
                return null;
            }
        }
    } catch (SQLException e) {
        Logger.getLogger().exception(null, "Failed to get API Account.", e, this.getClass(), true);
    }
    return null;
}
Also used : UserAPIAccount(com.cloudcraftgaming.discal.api.object.web.UserAPIAccount)

Example 2 with UserAPIAccount

use of com.cloudcraftgaming.discal.api.object.web.UserAPIAccount in project DisCal-Discord-Bot by NovaFox161.

the class DevCommand method blockAPIKey.

private void blockAPIKey(String[] args, MessageReceivedEvent event) {
    if (args.length == 2) {
        Message.sendMessage("Blocking API key...", event);
        String key = args[1];
        UserAPIAccount account = DatabaseManager.getManager().getAPIAccount(key);
        account.setBlocked(true);
        if (DatabaseManager.getManager().updateAPIAccount(account)) {
            Message.sendMessage("Successfully blocked API key!", event);
        } else {
            Message.sendMessage("Error occurred! Could not block API key!", event);
        }
    } else {
        Message.sendMessage("Please specify the API KEY!", event);
    }
}
Also used : UserAPIAccount(com.cloudcraftgaming.discal.api.object.web.UserAPIAccount)

Example 3 with UserAPIAccount

use of com.cloudcraftgaming.discal.api.object.web.UserAPIAccount in project DisCal-Discord-Bot by NovaFox161.

the class DevCommand method registerApiKey.

private void registerApiKey(String[] args, MessageReceivedEvent event) {
    if (args.length == 2) {
        Message.sendMessage("Registering new API key...", event);
        String userId = args[1];
        UserAPIAccount account = new UserAPIAccount();
        account.setUserId(userId);
        account.setAPIKey(KeyGenerator.csRandomAlphaNumericString(64));
        account.setTimeIssued(System.currentTimeMillis());
        account.setBlocked(false);
        account.setUses(0);
        if (DatabaseManager.getManager().updateAPIAccount(account)) {
            Message.sendMessage("Check your DMs for the new API Key!", event);
            Message.sendDirectMessage(account.getAPIKey(), event.getAuthor());
        } else {
            Message.sendMessage("Error occurred! Could not register new API key!", event);
        }
    } else {
        Message.sendMessage("Please specify the USER ID linked to the key!", event);
    }
}
Also used : UserAPIAccount(com.cloudcraftgaming.discal.api.object.web.UserAPIAccount)

Example 4 with UserAPIAccount

use of com.cloudcraftgaming.discal.api.object.web.UserAPIAccount in project DisCal-Discord-Bot by NovaFox161.

the class SparkUtils method initSpark.

@SuppressWarnings("ThrowableNotThrown")
public static void initSpark() {
    if (BotSettings.RUN_API.get().equalsIgnoreCase("true")) {
        port(Integer.valueOf(BotSettings.PORT.get()));
        // Main site location
        staticFileLocation("/web/public");
        notFound(((request, response) -> {
            response.redirect("/", 301);
            return response.body();
        }));
        // Register the API Endpoints
        before("/api/*", (request, response) -> {
            if (!request.requestMethod().equalsIgnoreCase("POST")) {
                Logger.getLogger().api("Denied '" + request.requestMethod() + "' access", request.ip());
                halt(405, "Method not allowed");
            }
            // Check authorization
            if (DiscordAccountHandler.getHandler().hasAccount(request.session().id())) {
                // User is logged in from website, no API key needed
                Logger.getLogger().api("API Call from website", request.ip());
            } else {
                // Requires "Authorization Header
                if (request.headers().contains("Authorization")) {
                    String key = request.headers("Authorization");
                    UserAPIAccount acc = DatabaseManager.getManager().getAPIAccount(key);
                    if (acc != null) {
                        if (acc.isBlocked()) {
                            Logger.getLogger().api("Attempted to use blocked API Key: " + acc.getAPIKey(), request.ip());
                            halt(401, "Unauthorized");
                        } else {
                            // Everything checks out!
                            acc.setUses(acc.getUses() + 1);
                            DatabaseManager.getManager().updateAPIAccount(acc);
                        }
                    } else {
                        Logger.getLogger().api("Attempted to use invalid API Key: " + key, request.ip());
                        halt(401, "Unauthorized");
                    }
                } else {
                    Logger.getLogger().api("Attempted to use API without authorization header", request.ip());
                    halt(400, "Bad Request");
                }
            }
        // Only accept json because its easier to parse and handle.
        /*
				if (!request.contentType().equalsIgnoreCase("application/json")) {
					halt(400, "Bad Request");
				}
				*/
        });
        // API endpoints
        path("/api/v1", () -> {
            before("/*", (q, a) -> Logger.getLogger().api("Received API Call", q.ip(), q.host(), q.pathInfo()));
            path("/guild", () -> {
                path("/settings", () -> {
                    post("/get", GuildEndpoint::getSettings);
                    post("/update", GuildEndpoint::updateSettings);
                });
                path("/info", () -> post("/from-user/list", GuildEndpoint::getUserGuilds));
            });
            path("/announcement", () -> {
                post("/get", AnnouncementEndpoint::getAnnouncement);
                post("/create", AnnouncementEndpoint::createAnnouncement);
                post("/update", AnnouncementEndpoint::updateAnnouncement);
                post("/delete", AnnouncementEndpoint::deleteAnnouncement);
                post("/list", AnnouncementEndpoint::listAnnouncements);
            });
            path("/calendar", () -> {
                post("/get", CalendarEndpoint::getCalendar);
                post("/list", CalendarEndpoint::listCalendars);
            });
            path("/events", () -> {
                path("/list", () -> {
                    post("/month", EventEndpoint::getEventsForMonth);
                    post("/date", EventEndpoint::getEventsForSelectedDate);
                });
                post("/update", EventEndpoint::updateEvent);
                post("/create", EventEndpoint::createEvent);
                post("/delete", EventEndpoint::deleteEvent);
            });
            path("/rsvp", () -> {
                post("/get", RsvpEndpoint::getRsvp);
                post("/update", RsvpEndpoint::updateRsvp);
            });
            path("/dashboard", () -> {
                path("/select", () -> {
                    post("/guild", DashboardHandler::handleGuildSelect);
                    post("/settings", DashboardHandler::handleSettingsSelect);
                });
                path("/create", () -> {
                    post("/calendar", DashboardHandler::handleCalendarCreate);
                    post("/announcement", DashboardHandler::handleAnnouncementCreate);
                });
                path("/update", () -> {
                    post("/announcement", DashboardHandler::handleAnnouncementUpdate);
                    post("/calendar", DashboardHandler::handleCalendarUpdate);
                    post("/settings", DashboardHandler::handleSettingsUpdate);
                });
                path("/delete", () -> {
                    post("/calendar", DashboardHandler::deleteCalendar);
                    post("/announcement", DashboardHandler::deleteAnnouncement);
                });
            });
        });
        // Various endpoints for thyme because yeah...
        path("/account", () -> {
            get("/login", DiscordLoginHandler::handleDiscordCode);
            get("/logout", DiscordLoginHandler::handleLogout);
        });
        // Templates and pages...
        get("/", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/index"), new ThymeleafTemplateEngine());
        get("/home", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/index"), new ThymeleafTemplateEngine());
        get("/about", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/about"), new ThymeleafTemplateEngine());
        get("/commands", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/commands"), new ThymeleafTemplateEngine());
        get("/lazy-discal", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/lazy-discal"), new ThymeleafTemplateEngine());
        get("/setup", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/setup"), new ThymeleafTemplateEngine());
        get("/policy/privacy", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/policy/privacy"), new ThymeleafTemplateEngine());
        // Dashboard pages
        get("/dashboard", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/dashboard/dashboard"), new ThymeleafTemplateEngine());
        get("/dashboard/guild", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/dashboard/guild"), new ThymeleafTemplateEngine());
        get("/dashboard/guild/calendar", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/dashboard/components/calendar"), new ThymeleafTemplateEngine());
        get("/dashboard/guild/events", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/dashboard/components/events"), new ThymeleafTemplateEngine());
        get("/dashboard/guild/announcements", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/dashboard/components/announcements"), new ThymeleafTemplateEngine());
        get("/dashboard/guild/rsvp", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/dashboard/components/rsvp"), new ThymeleafTemplateEngine());
        // Various other doc pages
        get("/docs/event/colors", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/events/event-colors"), new ThymeleafTemplateEngine());
        // API Doc Pages
        get("/docs/api/overview", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/overview"), new ThymeleafTemplateEngine());
        get("/docs/api/errors", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/errors"), new ThymeleafTemplateEngine());
        // API v1 Doc Pages
        get("/docs/api/v1/announcement", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/v1/announcement"), new ThymeleafTemplateEngine());
        get("/docs/api/v1/calendar", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/v1/calendar"), new ThymeleafTemplateEngine());
        get("/docs/api/v1/events", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/v1/events"), new ThymeleafTemplateEngine());
        get("/docs/api/v1/guild", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/v1/guild"), new ThymeleafTemplateEngine());
        get("/docs/api/v1/rsvp", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/v1/rsvp"), new ThymeleafTemplateEngine());
        get("/docs/api/v1/time", (rq, rs) -> new ModelAndView(DiscordAccountHandler.getHandler().getAccount(rq.session().id()), "pages/docs/api/v1/time"), new ThymeleafTemplateEngine());
    }
}
Also used : UserAPIAccount(com.cloudcraftgaming.discal.api.object.web.UserAPIAccount) Logger(com.cloudcraftgaming.discal.logger.Logger) DashboardHandler(com.cloudcraftgaming.discal.web.handler.DashboardHandler) DiscordLoginHandler(com.cloudcraftgaming.discal.api.network.discord.DiscordLoginHandler) ModelAndView(spark.ModelAndView) BotSettings(com.cloudcraftgaming.discal.api.object.BotSettings) com.cloudcraftgaming.discal.web.endpoints.v1(com.cloudcraftgaming.discal.web.endpoints.v1) DiscordAccountHandler(com.cloudcraftgaming.discal.web.handler.DiscordAccountHandler) Spark(spark.Spark) DatabaseManager(com.cloudcraftgaming.discal.api.database.DatabaseManager) DiscordLoginHandler(com.cloudcraftgaming.discal.api.network.discord.DiscordLoginHandler) UserAPIAccount(com.cloudcraftgaming.discal.api.object.web.UserAPIAccount) ModelAndView(spark.ModelAndView) DashboardHandler(com.cloudcraftgaming.discal.web.handler.DashboardHandler)

Aggregations

UserAPIAccount (com.cloudcraftgaming.discal.api.object.web.UserAPIAccount)4 DatabaseManager (com.cloudcraftgaming.discal.api.database.DatabaseManager)1 DiscordLoginHandler (com.cloudcraftgaming.discal.api.network.discord.DiscordLoginHandler)1 BotSettings (com.cloudcraftgaming.discal.api.object.BotSettings)1 Logger (com.cloudcraftgaming.discal.logger.Logger)1 com.cloudcraftgaming.discal.web.endpoints.v1 (com.cloudcraftgaming.discal.web.endpoints.v1)1 DashboardHandler (com.cloudcraftgaming.discal.web.handler.DashboardHandler)1 DiscordAccountHandler (com.cloudcraftgaming.discal.web.handler.DiscordAccountHandler)1 ModelAndView (spark.ModelAndView)1 Spark (spark.Spark)1