SteamWar/BungeeCore
Archiviert
13
2

Add initial WebAPI
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-05-29 23:53:10 +02:00
Ursprung 1a54476cbe
Commit 14052c5d01
7 geänderte Dateien mit 214 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,47 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.api;
import lombok.experimental.UtilityClass;
import spark.Request;
import spark.Response;
import java.util.Optional;
@UtilityClass
public class AuthUtils {
public static Optional<Token> isAuthorized(Request request, Response response) {
String authorization = request.headers("Authorization");
if (authorization == null) {
response.type("application/json");
response.status(401);
return Optional.empty();
}
try {
return Optional.of(Token.decrypt(authorization));
} catch (SecurityException e) {
response.type("application/json");
response.status(401);
return Optional.empty();
}
}
}

Datei anzeigen

@ -23,6 +23,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import de.steamwar.bungeecore.sql.SteamwarUser; import de.steamwar.bungeecore.sql.SteamwarUser;
import lombok.Getter;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import java.io.*; import java.io.*;
@ -34,6 +35,7 @@ import java.security.spec.DSAPublicKeySpec;
import java.util.Base64; import java.util.Base64;
import java.util.UUID; import java.util.UUID;
@Getter
public class Token { public class Token {
private static KeyPair pair; private static KeyPair pair;

Datei anzeigen

@ -21,6 +21,8 @@ package de.steamwar.bungeecore.api;
import de.steamwar.bungeecore.api.v1.ServerStatusEndPoint; import de.steamwar.bungeecore.api.v1.ServerStatusEndPoint;
import de.steamwar.bungeecore.api.v1.ServerTeamEndPoint; import de.steamwar.bungeecore.api.v1.ServerTeamEndPoint;
import de.steamwar.bungeecore.api.v1.user.GetUUIDEndPoint;
import de.steamwar.bungeecore.api.v1.user.GetUserNameEndPoint;
import static spark.Spark.port; import static spark.Spark.port;
@ -31,5 +33,7 @@ public class WebAPI {
new ServerStatusEndPoint().ignite(); new ServerStatusEndPoint().ignite();
new ServerTeamEndPoint().ignite(); new ServerTeamEndPoint().ignite();
new GetUserNameEndPoint().ignite();
new GetUUIDEndPoint().ignite();
} }
} }

Datei anzeigen

@ -0,0 +1,50 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.api.v1.user;
import com.google.gson.JsonObject;
import de.steamwar.bungeecore.api.AuthUtils;
import de.steamwar.bungeecore.api.EndPoint;
import de.steamwar.bungeecore.api.Token;
import java.util.Optional;
import static spark.Spark.post;
public class GetUUIDEndPoint implements EndPoint {
@Override
public void ignite() {
post("/v1/user/token_to_uuid", (request, response) -> {
Optional<Token> optionalToken = AuthUtils.isAuthorized(request, response);
if (!optionalToken.isPresent()) {
return "{}";
}
Token token = optionalToken.get();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("uuid", token.getUuid().toString());
response.type("application/json");
response.status(200);
return jsonObject.toString();
});
}
}

Datei anzeigen

@ -0,0 +1,51 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.api.v1.user;
import com.google.gson.JsonObject;
import de.steamwar.bungeecore.api.AuthUtils;
import de.steamwar.bungeecore.api.EndPoint;
import de.steamwar.bungeecore.api.Token;
import de.steamwar.bungeecore.sql.SteamwarUser;
import java.util.Optional;
import static spark.Spark.post;
public class GetUserNameEndPoint implements EndPoint {
@Override
public void ignite() {
post("/v1/user/token_to_username", (request, response) -> {
Optional<Token> optionalToken = AuthUtils.isAuthorized(request, response);
if (!optionalToken.isPresent()) {
return "{}";
}
Token token = optionalToken.get();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("username", SteamwarUser.get(token.getUuid()).getUserName());
response.type("application/json");
response.status(200);
return jsonObject.toString();
});
}
}

Datei anzeigen

@ -0,0 +1,30 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.api.v1.website;
import de.steamwar.bungeecore.api.EndPoint;
public class LoginEndPoint implements EndPoint {
@Override
public void ignite() {
}
}

Datei anzeigen

@ -0,0 +1,30 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bungeecore.api.v1.website;
import de.steamwar.bungeecore.api.EndPoint;
public class LogoutEndPoint implements EndPoint {
@Override
public void ignite() {
}
}