|
|
@@ -3,9 +3,13 @@ package com.deliveryproject.easydelivery;
|
|
|
import com.deliveryproject.easydelivery.Configuration.Constants;
|
|
|
import com.deliveryproject.easydelivery.Models.KeycloakUser.User;
|
|
|
import com.deliveryproject.easydelivery.RequestBody.Token;
|
|
|
+import com.deliveryproject.easydelivery.Roles.RoleInterface;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import com.fasterxml.jackson.databind.ObjectWriter;
|
|
|
-import jdk.net.SocketFlow;
|
|
|
+import com.mashape.unirest.http.HttpResponse;
|
|
|
+import com.mashape.unirest.http.Unirest;
|
|
|
+import com.mashape.unirest.http.exceptions.UnirestException;
|
|
|
+import org.json.JSONObject;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
@@ -14,12 +18,11 @@ import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.*;
|
|
|
import java.net.HttpURLConnection;
|
|
|
-import java.net.MalformedURLException;
|
|
|
-import java.net.ProtocolException;
|
|
|
import java.net.URL;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import static com.deliveryproject.easydelivery.Configuration.Constants.*;
|
|
|
+import static com.deliveryproject.easydelivery.Utils.JWTUtils.decode;
|
|
|
|
|
|
@RestController
|
|
|
public class KeycloakController {
|
|
|
@@ -44,6 +47,7 @@ public class KeycloakController {
|
|
|
return exchange;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@RequestMapping(value = "/keycloak/adminLogin")
|
|
|
@ResponseBody
|
|
|
public ResponseEntity<Token> getAdminToken() {
|
|
|
@@ -64,10 +68,61 @@ public class KeycloakController {
|
|
|
return exchange;
|
|
|
}
|
|
|
|
|
|
+ @RequestMapping(value = "/keycloak/listOfRoles")
|
|
|
+ @ResponseBody
|
|
|
+ public String getListOfRoles() {
|
|
|
+
|
|
|
+ try {
|
|
|
+ URL url = new URL(KEYCLOAK_ADDRESS + "/auth/admin/realms/SpringBootKeycloak/roles");
|
|
|
+ HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
|
|
+ con.setRequestMethod("GET");
|
|
|
+ String token = Objects.requireNonNull(getAdminToken().getBody()).access_token;
|
|
|
+ con.setRequestProperty("Authorization", "Bearer " + token);
|
|
|
+ int responseCode = con.getResponseCode();
|
|
|
+ System.out.println("Response code: " + responseCode);
|
|
|
+
|
|
|
+ BufferedReader in = new BufferedReader(
|
|
|
+ new InputStreamReader(con.getInputStream()));
|
|
|
+ String inputLine;
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+
|
|
|
+ return response.toString();
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println(e);
|
|
|
+ return e.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void assignRoleToUser(String userId, RoleInterface roleInterface) {
|
|
|
+
|
|
|
+ String url = KEYCLOAK_ADDRESS + "/auth/admin/realms/SpringBootKeycloak/users/" + userId + "/role-mappings/realm";
|
|
|
+
|
|
|
+ String json = "[{ \"id\": \"" + roleInterface.getRoleId() + "\", \"name\": \"" + roleInterface.getRoleName() + "\" }]";
|
|
|
+ String token = Objects.requireNonNull(getAdminToken().getBody()).access_token;
|
|
|
+ System.out.println(json);
|
|
|
+ try {
|
|
|
+ HttpResponse<String> response = Unirest.post(url)
|
|
|
+ .header("Authorization", "Bearer " + token)
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
+ .body(json)
|
|
|
+ .asString();
|
|
|
+ System.out.println(response.getBody());
|
|
|
+ System.out.println(response.getStatus());
|
|
|
+ } catch (UnirestException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
@RequestMapping(value = "/keycloak/createUser")
|
|
|
@ResponseBody
|
|
|
public ResponseEntity<String> createUser(@RequestBody User user) {
|
|
|
+
|
|
|
try {
|
|
|
URL url = new URL(KEYCLOAK_ADDRESS + "/auth/admin/realms/SpringBootKeycloak/users");
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
@@ -93,8 +148,15 @@ public class KeycloakController {
|
|
|
response.append(responseLine.trim());
|
|
|
}
|
|
|
System.out.println(response.toString());
|
|
|
- //TODO We need to implement adding a new user to a group
|
|
|
conn.disconnect();
|
|
|
+
|
|
|
+
|
|
|
+ Token token = getToken(user.username, user.credentials.get(0).value).getBody();
|
|
|
+ JSONObject payload = new JSONObject(decode(token.access_token.split("\\.")[1]));
|
|
|
+ String user_id = payload.getString("sub").toString();
|
|
|
+
|
|
|
+ assignRoleToUser(user_id, new com.deliveryproject.easydelivery.Roles.User());
|
|
|
+
|
|
|
return new ResponseEntity<>(HttpStatus.OK);
|
|
|
} catch (Exception e) {
|
|
|
System.err.println(e);
|