diff --git a/src/de/steamwar/bungeecore/api/AuthUtils.java b/src/de/steamwar/bungeecore/api/AuthUtils.java new file mode 100644 index 00000000..2a055230 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/AuthUtils.java @@ -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 . + */ + +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 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(); + } + } +} diff --git a/src/de/steamwar/bungeecore/api/Token.java b/src/de/steamwar/bungeecore/api/Token.java index 5acb2326..c82f9c06 100644 --- a/src/de/steamwar/bungeecore/api/Token.java +++ b/src/de/steamwar/bungeecore/api/Token.java @@ -23,6 +23,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.stream.JsonWriter; import de.steamwar.bungeecore.sql.SteamwarUser; +import lombok.Getter; import javax.crypto.Cipher; import java.io.*; @@ -34,6 +35,7 @@ import java.security.spec.DSAPublicKeySpec; import java.util.Base64; import java.util.UUID; +@Getter public class Token { private static KeyPair pair; diff --git a/src/de/steamwar/bungeecore/api/WebAPI.java b/src/de/steamwar/bungeecore/api/WebAPI.java index 830bb1f7..6d9dd430 100644 --- a/src/de/steamwar/bungeecore/api/WebAPI.java +++ b/src/de/steamwar/bungeecore/api/WebAPI.java @@ -21,6 +21,8 @@ package de.steamwar.bungeecore.api; import de.steamwar.bungeecore.api.v1.ServerStatusEndPoint; 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; @@ -31,5 +33,7 @@ public class WebAPI { new ServerStatusEndPoint().ignite(); new ServerTeamEndPoint().ignite(); + new GetUserNameEndPoint().ignite(); + new GetUUIDEndPoint().ignite(); } } diff --git a/src/de/steamwar/bungeecore/api/v1/user/GetUUIDEndPoint.java b/src/de/steamwar/bungeecore/api/v1/user/GetUUIDEndPoint.java new file mode 100644 index 00000000..bf1b0a72 --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/user/GetUUIDEndPoint.java @@ -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 . + */ + +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 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(); + }); + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/user/GetUserNameEndPoint.java b/src/de/steamwar/bungeecore/api/v1/user/GetUserNameEndPoint.java new file mode 100644 index 00000000..9b5a848b --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/user/GetUserNameEndPoint.java @@ -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 . + */ + +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 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(); + }); + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java b/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java new file mode 100644 index 00000000..c1103b4c --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/website/LoginEndPoint.java @@ -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 . + */ + +package de.steamwar.bungeecore.api.v1.website; + +import de.steamwar.bungeecore.api.EndPoint; + +public class LoginEndPoint implements EndPoint { + + @Override + public void ignite() { + + } +} diff --git a/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java b/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java new file mode 100644 index 00000000..c1db3bbe --- /dev/null +++ b/src/de/steamwar/bungeecore/api/v1/website/LogoutEndPoint.java @@ -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 . + */ + +package de.steamwar.bungeecore.api.v1.website; + +import de.steamwar.bungeecore.api.EndPoint; + +public class LogoutEndPoint implements EndPoint { + + @Override + public void ignite() { + + } +}