Search in sources :

Example 1 with Authenticator

use of at.xirado.bean.backend.Authenticator in project Bean by Xirado.

the class GuildsRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    String authHeader = request.headers("authorization");
    if (authHeader == null || !authHeader.startsWith("Token ")) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    String token = authHeader.substring(7);
    byte[] tokenBytes = token.getBytes(StandardCharsets.UTF_8);
    Authenticator authenticator = Bean.getInstance().getAuthenticator();
    if (!authenticator.isAuthenticated(tokenBytes)) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Invalid token (Try logging out and in again)").toString();
    }
    if (authenticator.isAccessTokenExpired(tokenBytes))
        authenticator.refreshAccessToken(tokenBytes);
    DataObject object = authenticator.getData(tokenBytes);
    DiscordCredentials credentials = authenticator.getCredentials(tokenBytes);
    String accessToken = credentials.getAccessToken();
    DataObject guilds = WebServer.retrieveGuilds(accessToken);
    if (guilds.isNull("guilds")) {
        DataObject o = DataObject.empty();
        o.put("http_code", guilds.getInt("http_code"));
        if (!guilds.isNull("code"))
            o.put("code", guilds.getInt("code"));
        if (!guilds.isNull("message"))
            o.put("message", guilds.getString("message"));
        return o.toString();
    }
    ShardManager shardManager = Bean.getInstance().getShardManager();
    List<DataObject> adminGuilds = guilds.getArray("guilds").stream(DataArray::getObject).filter(obj -> Permission.getPermissions(obj.getLong("permissions")).contains(Permission.ADMINISTRATOR) || obj.getBoolean("owner")).collect(Collectors.toList());
    DataArray guildArray = DataArray.empty();
    List<DataObject> nonInvitedGuilds = new ArrayList<>();
    for (DataObject guild : adminGuilds) {
        String name = guild.getString("name");
        long id = guild.getLong("id");
        String iconHash = guild.isNull("icon") ? null : guild.getString("icon");
        boolean botJoined = shardManager.getGuildById(id) != null;
        DataObject o = DataObject.empty();
        o.put("name", name);
        o.put("id", id + "");
        if (iconHash != null)
            o.put("icon", DiscordUtils.getIconUrl(id + "", iconHash));
        o.put("bot_joined", botJoined);
        StringBuilder initials = new StringBuilder();
        for (String s : name.split("\\s+")) {
            initials.append(s.charAt(0));
        }
        o.put("initials", initials.toString());
        if (botJoined)
            guildArray.add(o);
        else
            nonInvitedGuilds.add(o);
    }
    guildArray.addAll(nonInvitedGuilds);
    return DataObject.empty().put("guilds", guildArray).put("base_invite_url", InviteURLRoute.getInviteURL()).put("http_code", guilds.getInt("http_code")).toString();
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) DiscordCredentials(at.xirado.bean.backend.DiscordCredentials) DiscordUtils(at.xirado.bean.backend.DiscordUtils) Authenticator(at.xirado.bean.backend.Authenticator) Permission(net.dv8tion.jda.api.Permission) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ArrayList(java.util.ArrayList) List(java.util.List) WebServer(at.xirado.bean.backend.WebServer) Request(spark.Request) Response(spark.Response) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(spark.Route) Bean(at.xirado.bean.Bean) DiscordCredentials(at.xirado.bean.backend.DiscordCredentials) ArrayList(java.util.ArrayList) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) DataArray(net.dv8tion.jda.api.utils.data.DataArray) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Authenticator(at.xirado.bean.backend.Authenticator)

Example 2 with Authenticator

use of at.xirado.bean.backend.Authenticator in project Bean by Xirado.

the class UserRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    String authHeader = request.headers("authorization");
    if (authHeader == null || !authHeader.startsWith("Token ")) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    String token = authHeader.substring(7);
    byte[] tokenBytes = token.getBytes(StandardCharsets.UTF_8);
    Authenticator authenticator = Bean.getInstance().getAuthenticator();
    if (!authenticator.isAuthenticated(tokenBytes)) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Invalid token (Try logging out and in again)").toString();
    }
    if (authenticator.isAccessTokenExpired(tokenBytes))
        authenticator.refreshAccessToken(tokenBytes);
    DataObject user = authenticator.getData(tokenBytes);
    long id = user.getObject("user").getLong("id");
    int discriminator = Integer.parseInt(user.getObject("user").getString("discriminator"));
    String effectiveAvatarURL = "";
    if (user.getObject("user").isNull("avatar"))
        effectiveAvatarURL = "https://cdn.discordapp.com/embed/avatars/" + (discriminator % 5) + ".png";
    else {
        String avatarHash = user.getObject("user").getString("avatar");
        boolean animated = avatarHash.startsWith("a_");
        effectiveAvatarURL = "https://cdn.discordapp.com/avatars/" + id + "/" + avatarHash + (animated ? ".gif" : ".png");
    }
    user.getObject("user").put("effective_avatar", effectiveAvatarURL);
    return user.toString();
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) Authenticator(at.xirado.bean.backend.Authenticator)

Example 3 with Authenticator

use of at.xirado.bean.backend.Authenticator in project Bean by Xirado.

the class GuildPageRoute method handle.

@Override
public Object handle(Request request, Response response) throws Exception {
    String authHeader = request.headers("authorization");
    if (authHeader == null || !authHeader.startsWith("Token ")) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    String guildIdString = request.headers("guild_id");
    if (guildIdString == null) {
        response.status(400);
        return DataObject.empty().put("code", 400).put("message", "Missing guild id!").toString();
    }
    String token = authHeader.substring(7);
    byte[] tokenBytes = token.getBytes(StandardCharsets.UTF_8);
    Authenticator authenticator = Bean.getInstance().getAuthenticator();
    if (!authenticator.isAuthenticated(tokenBytes)) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Invalid token (Try logging out and in again)").toString();
    }
    if (authenticator.isAccessTokenExpired(tokenBytes))
        authenticator.refreshAccessToken(tokenBytes);
    DiscordCredentials credentials = authenticator.getCredentials(tokenBytes);
    String accessToken = credentials.getAccessToken();
    DataObject guilds = WebServer.retrieveGuilds(accessToken);
    if (guilds.isNull("guilds")) {
        DataObject object = DataObject.empty();
        object.put("http_code", guilds.getInt("http_code"));
        if (!guilds.isNull("code"))
            object.put("code", guilds.getInt("code"));
        if (!guilds.isNull("message"))
            object.put("message", guilds.getString("message"));
        return object.toString();
    }
    ShardManager shardManager = Bean.getInstance().getShardManager();
    List<Guild> mutualGuilds = guilds.getArray("guilds").stream(DataArray::getObject).filter(obj -> shardManager.getGuildById(obj.getLong("id")) != null).filter(obj -> Permission.getPermissions(obj.getLong("permissions")).contains(Permission.ADMINISTRATOR) || obj.getBoolean("owner")).map(obj -> shardManager.getGuildById(obj.getLong("id"))).collect(Collectors.toList());
    long guildId = Long.parseUnsignedLong(guildIdString);
    Guild guild = mutualGuilds.stream().map(ISnowflake::getIdLong).filter(id -> id.equals(guildId)).map(shardManager::getGuildById).findFirst().orElse(null);
    if (guild == null) {
        response.status(401);
        return DataObject.empty().put("code", 401).put("message", "Unauthorized").toString();
    }
    GuildData guildData = GuildManager.getGuildData(guild);
    DataObject data = guildData.toData();
    DataObject returnObject = DataObject.empty().put("guild", ObjectBuilder.serializeJDAGuild(guild)).put("http_code", guilds.getInt("http_code")).put("data", data);
    return returnObject.toString();
}
Also used : GuildManager(at.xirado.bean.data.GuildManager) DataArray(net.dv8tion.jda.api.utils.data.DataArray) ObjectBuilder(at.xirado.bean.backend.ObjectBuilder) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) DiscordCredentials(at.xirado.bean.backend.DiscordCredentials) Authenticator(at.xirado.bean.backend.Authenticator) Permission(net.dv8tion.jda.api.Permission) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) GuildData(at.xirado.bean.data.GuildData) Guild(net.dv8tion.jda.api.entities.Guild) WebServer(at.xirado.bean.backend.WebServer) Request(spark.Request) Response(spark.Response) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Route(spark.Route) Bean(at.xirado.bean.Bean) GuildData(at.xirado.bean.data.GuildData) DiscordCredentials(at.xirado.bean.backend.DiscordCredentials) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) DataArray(net.dv8tion.jda.api.utils.data.DataArray) DataObject(net.dv8tion.jda.api.utils.data.DataObject) Authenticator(at.xirado.bean.backend.Authenticator) ISnowflake(net.dv8tion.jda.api.entities.ISnowflake)

Aggregations

Authenticator (at.xirado.bean.backend.Authenticator)3 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 Bean (at.xirado.bean.Bean)2 DiscordCredentials (at.xirado.bean.backend.DiscordCredentials)2 WebServer (at.xirado.bean.backend.WebServer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Permission (net.dv8tion.jda.api.Permission)2 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)2 DataArray (net.dv8tion.jda.api.utils.data.DataArray)2 Request (spark.Request)2 Response (spark.Response)2 Route (spark.Route)2 DiscordUtils (at.xirado.bean.backend.DiscordUtils)1 ObjectBuilder (at.xirado.bean.backend.ObjectBuilder)1 GuildData (at.xirado.bean.data.GuildData)1 GuildManager (at.xirado.bean.data.GuildManager)1 ArrayList (java.util.ArrayList)1 Guild (net.dv8tion.jda.api.entities.Guild)1