|
| 1 | +package de.triology.universeadm.multifactor; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.google.gson.JsonParseException; |
| 8 | + |
| 9 | +import javax.ws.rs.DELETE; |
| 10 | +import javax.ws.rs.GET; |
| 11 | +import javax.ws.rs.Path; |
| 12 | +import javax.ws.rs.PathParam; |
| 13 | +import javax.ws.rs.Produces; |
| 14 | +import javax.ws.rs.core.MediaType; |
| 15 | +import javax.ws.rs.core.Response; |
| 16 | + |
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
| 19 | + |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.net.HttpURLConnection; |
| 24 | +import java.net.URL; |
| 25 | +import java.nio.charset.StandardCharsets; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Base64; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +@Path("/mfa") |
| 31 | +@Produces(MediaType.APPLICATION_JSON) |
| 32 | +public class MultifactorResource { |
| 33 | + |
| 34 | + private static final Logger LOG = LoggerFactory.getLogger(MultifactorResource.class); |
| 35 | + private static final Gson GSON = new Gson(); |
| 36 | + private static final String FQDN = System.getProperty("cas.mfa.fqdn"); |
| 37 | + private static final String user = System.getProperty("cas.mfa.user"); |
| 38 | + private static final String password = System.getProperty("cas.mfa.password"); |
| 39 | + private static final String CAS_MFA_ENDPOINT = "https://" + FQDN + "/cas/actuator/gauthCredentialRepository"; |
| 40 | + |
| 41 | + public MultifactorResource() { |
| 42 | + } |
| 43 | + |
| 44 | + @GET |
| 45 | + @Path("/{username}") |
| 46 | + public Response getMfa(@PathParam("username") String username) { |
| 47 | + try { |
| 48 | + String jsonResponse = callCasMfaGetApi(username); |
| 49 | + MfaDTO credentials = parse(jsonResponse); |
| 50 | + return Response.ok(credentials, MediaType.APPLICATION_JSON).build(); |
| 51 | + } catch (IOException e) { |
| 52 | + LOG.error("Failed to load MFA data from CAS", e); |
| 53 | + return Response.status(Response.Status.BAD_GATEWAY) |
| 54 | + .entity("{\"message\":\"Failed to load MFA data from CAS\"}") |
| 55 | + .type(MediaType.APPLICATION_JSON) |
| 56 | + .build(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @DELETE |
| 61 | + @Path("/{username}") |
| 62 | + public Response deleteMfa(@PathParam("username") String username) { |
| 63 | + try { |
| 64 | + callCasMfaDeleteApi(username); |
| 65 | + return Response.noContent().build(); |
| 66 | + } catch (IOException e) { |
| 67 | + LOG.error("Failed to delete MFA credentials for user: {}", username, e); |
| 68 | + return Response.status(Response.Status.BAD_GATEWAY) |
| 69 | + .entity("{\"message\":\"Failed to delete MFA credentials\"}") |
| 70 | + .type(MediaType.APPLICATION_JSON) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parses the JSON response from the CAS API and extracts only username and (device-)name. |
| 77 | + */ |
| 78 | + private MfaDTO parse(String jsonResponse) { |
| 79 | + MfaDTO result = new MfaDTO(); |
| 80 | + |
| 81 | + try { |
| 82 | + JsonArray jsonArray = GSON.fromJson(jsonResponse, JsonArray.class); |
| 83 | + |
| 84 | + for (JsonElement element : jsonArray) { |
| 85 | + JsonObject obj = element.getAsJsonObject(); |
| 86 | + |
| 87 | + String username = obj.has("username") ? obj.get("username").getAsString() : null; |
| 88 | + String name = obj.has("name") ? obj.get("name").getAsString() : null; |
| 89 | + |
| 90 | + result.setUsername(username); |
| 91 | + result.setName(name); |
| 92 | + } |
| 93 | + } catch (JsonParseException | IllegalStateException e) { |
| 94 | + LOG.error("Failed to parse MFA credentials JSON", e); |
| 95 | + } |
| 96 | + |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Calls the CAS MFA API to list all MFA credentials. |
| 102 | + */ |
| 103 | + private String callCasMfaGetApi(String username) throws IOException { |
| 104 | + HttpURLConnection connection = null; |
| 105 | + try { |
| 106 | + URL url = new URL(CAS_MFA_ENDPOINT + "/" + username); |
| 107 | + connection = (HttpURLConnection) url.openConnection(); |
| 108 | + connection.setRequestMethod("GET"); |
| 109 | + |
| 110 | + addBasicAuthentication(connection); |
| 111 | + |
| 112 | + int status = connection.getResponseCode(); |
| 113 | + if (status < 200 || status > 299) { |
| 114 | + throw new IOException("CAS MFA endpoint returned status " + status); |
| 115 | + } |
| 116 | + |
| 117 | + try (BufferedReader reader = |
| 118 | + new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"))) { |
| 119 | + StringBuilder sb = new StringBuilder(); |
| 120 | + String line; |
| 121 | + while ((line = reader.readLine()) != null) { |
| 122 | + sb.append(line); |
| 123 | + } |
| 124 | + return sb.toString(); |
| 125 | + } |
| 126 | + } finally { |
| 127 | + if (connection != null) { |
| 128 | + connection.disconnect(); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Calls the CAS MFA API to delete MFA credentials for a user. |
| 135 | + * |
| 136 | + * @param username username of the user whose MFA credentials should be deleted |
| 137 | + */ |
| 138 | + private void callCasMfaDeleteApi(String username) throws IOException { |
| 139 | + HttpURLConnection connection = null; |
| 140 | + try { |
| 141 | + String urlString = CAS_MFA_ENDPOINT + "/" + username; |
| 142 | + URL url = new URL(urlString); |
| 143 | + connection = (HttpURLConnection) url.openConnection(); |
| 144 | + connection.setRequestMethod("DELETE"); |
| 145 | + |
| 146 | + addBasicAuthentication(connection); |
| 147 | + |
| 148 | + int status = connection.getResponseCode(); |
| 149 | + |
| 150 | + if ((status < 200 || status > 299)) { |
| 151 | + throw new IOException("CAS MFA endpoint returned status " + status); |
| 152 | + } |
| 153 | + } finally { |
| 154 | + if (connection != null) { |
| 155 | + connection.disconnect(); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Adds Basic Authentication Header to the HTTP connection. |
| 162 | + * |
| 163 | + * @param connection die HTTP-Verbindung |
| 164 | + */ |
| 165 | + private void addBasicAuthentication(HttpURLConnection connection) { |
| 166 | + if (user != null && password != null) { |
| 167 | + String basicAuth = Base64.getEncoder() |
| 168 | + .encodeToString((user + ":" + password).getBytes(StandardCharsets.UTF_8)); |
| 169 | + connection.setRequestProperty("Authorization", "Basic " + basicAuth); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private HttpURLConnection createConnection(String username, String method) throws IOException { |
| 174 | + HttpURLConnection connection = null; |
| 175 | + String urlString = CAS_MFA_ENDPOINT + "/" + username; |
| 176 | + URL url = new URL(urlString); |
| 177 | + connection = (HttpURLConnection) url.openConnection(); |
| 178 | + connection.setRequestMethod(method); |
| 179 | + |
| 180 | + addBasicAuthentication(connection); |
| 181 | + |
| 182 | + return connection; |
| 183 | + } |
| 184 | +} |
0 commit comments