Skip to content

Commit

Permalink
Adding support for more property types
Browse files Browse the repository at this point in the history
  • Loading branch information
rnavagamuwa committed Mar 29, 2019
1 parent 8b06110 commit 2447f39
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class AuthController {

@RequestMapping("/auth")
@PreAuthorize("hasPermission('admin_xacml','{actionid:action-id,resourceid:resource-id}')")
@PreAuthorize("hasPermission('admin_xacml','{actionid:header.action-id,resourceid:header.resource-id}')")
public ResponseEntity sampleAuth(@CurrentUser User user, Model model) {

return new ResponseEntity<>("Successfully authorized", HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.wso2.spring.security.abac.util;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.Map;

/**
* @author Randika Navagamuwa
*/
class GeneralUtils {

static Map<String, String> extractValuesFromCookies(Cookie[] cookies) {
final Map<String, String> cookieValues = new HashMap<>();

for (Cookie currentCookie : cookies) {
cookieValues.put(currentCookie.getName(), currentCookie.getValue());
}
return cookieValues;
}

static String[] splitContextPath(String contextPath){
return contextPath.split("/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -95,21 +96,70 @@ private Map<String, Object> generateFreemakeTemplateData(String jsonKeyValuePair
while (keys.hasNext()) {
String key = keys.next();
String value = jsonObject.get(key).toString();
templateData.put(key, httpServletRequest.getHeader(value));
String[] proTypeArr = value.split("\\.", 2);

PropertyType propertyType = PropertyType.getEnum(proTypeArr[0]);
if (!proTypeArr[0].isEmpty()) {
value = proTypeArr[1];
}

switch (propertyType) {
case HEADER:
value = httpServletRequest.getHeader(value);
break;
case COOKIE:
value = GeneralUtils.extractValuesFromCookies(httpServletRequest.getCookies()).get(value);
break;
case QUERY_PARAM:
value = httpServletRequest.getParameter(value);
break;
case FORM_DATA:
value = httpServletRequest.getParameter(value);
break;
case PATH_PARAM:
value = GeneralUtils.splitContextPath(httpServletRequest.getContextPath())[Integer.parseInt(value)];
break;
}
templateData.put(key, value);
}

return templateData;
}

private String getTemplateDataAsAString(Map<String, Object> templateData) {

StringBuilder hash = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder();
for (Map.Entry<String, Object> entry : templateData.entrySet()) {
hash.append(entry.getKey()).append(entry.getValue());
stringBuilder.append(entry.getKey()).append(entry.getValue());
}

return hash.toString();
return stringBuilder.toString();

}

private enum PropertyType {
HEADER("header"),
QUERY_PARAM("queryParam"),
COOKIE("cookie"),
FORM_DATA("formdata"),
PATH_PARAM("pathParam");

private String property;

PropertyType(String property) {
this.property = property;
}

@Override
public String toString() {
return this.property;
}

public static PropertyType getEnum(String property) {
for (PropertyType v : values())
if (v.toString().equalsIgnoreCase(property)) return v;
return PropertyType.HEADER;
}
}

}

0 comments on commit 2447f39

Please sign in to comment.