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();
}
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();
}
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();
}
Aggregations