|
|
@ -23,6 +23,7 @@ import io.swagger.models.Swagger;
|
|
|
|
import org.apache.catalina.core.StandardContext;
|
|
|
|
import org.apache.catalina.core.StandardContext;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import org.wso2.carbon.apimgt.api.model.Scope;
|
|
|
|
import org.wso2.carbon.device.mgt.common.permission.mgt.Permission;
|
|
|
|
import org.wso2.carbon.device.mgt.common.permission.mgt.Permission;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
|
import javax.servlet.ServletContext;
|
|
|
@ -38,10 +39,7 @@ import java.net.URI;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.security.AccessController;
|
|
|
|
import java.security.AccessController;
|
|
|
|
import java.security.PrivilegedAction;
|
|
|
|
import java.security.PrivilegedAction;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import java.util.StringTokenizer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class AnnotationProcessor {
|
|
|
|
public class AnnotationProcessor {
|
|
|
|
|
|
|
|
|
|
|
@ -56,11 +54,14 @@ public class AnnotationProcessor {
|
|
|
|
private static final String STRING_ARR = "string_arr";
|
|
|
|
private static final String STRING_ARR = "string_arr";
|
|
|
|
private static final String STRING = "string";
|
|
|
|
private static final String STRING = "string";
|
|
|
|
|
|
|
|
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_AUTHORIZATIONS = "authorizations";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PROPERTIES = "properties";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PERMISSION = "permission";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_EXTENSIONS = "extensions";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_SCOPES = "scopes";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PROPERTIES_VALUE = "value";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_SCOPE = "scope";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PROPERTIES_NAME = "name";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_DESCRIPTION = "description";
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PROPERTIES_DESCRIPTION = "description";
|
|
|
|
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PROPERTIES_KEY = "key";
|
|
|
|
|
|
|
|
private static final String SWAGGER_ANNOTATIONS_PROPERTIES_PERMISSIONS = "permissions";
|
|
|
|
|
|
|
|
private static final String ANNOTATIONS_SCOPES = "scopes";
|
|
|
|
|
|
|
|
|
|
|
|
private StandardContext context;
|
|
|
|
private StandardContext context;
|
|
|
|
private Method[] pathClazzMethods;
|
|
|
|
private Method[] pathClazzMethods;
|
|
|
@ -74,6 +75,11 @@ public class AnnotationProcessor {
|
|
|
|
private Class<io.swagger.annotations.ApiOperation> apiOperation;
|
|
|
|
private Class<io.swagger.annotations.ApiOperation> apiOperation;
|
|
|
|
private Class<io.swagger.annotations.Authorization> authorizationClass;
|
|
|
|
private Class<io.swagger.annotations.Authorization> authorizationClass;
|
|
|
|
private Class<io.swagger.annotations.AuthorizationScope> authorizationScopeClass;
|
|
|
|
private Class<io.swagger.annotations.AuthorizationScope> authorizationScopeClass;
|
|
|
|
|
|
|
|
private Class<io.swagger.annotations.Extension> extensionClass;
|
|
|
|
|
|
|
|
private Class<io.swagger.annotations.ExtensionProperty> extensionPropertyClass;
|
|
|
|
|
|
|
|
private Class<org.wso2.carbon.apimgt.annotations.api.Scope> scopeClass;
|
|
|
|
|
|
|
|
private Class<org.wso2.carbon.apimgt.annotations.api.Scopes> scopesClass;
|
|
|
|
|
|
|
|
private Map<String, Scope> apiScopes;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public AnnotationProcessor(final StandardContext context) {
|
|
|
|
public AnnotationProcessor(final StandardContext context) {
|
|
|
@ -90,6 +96,15 @@ public class AnnotationProcessor {
|
|
|
|
.loadClass((io.swagger.annotations.Authorization.class.getName()));
|
|
|
|
.loadClass((io.swagger.annotations.Authorization.class.getName()));
|
|
|
|
authorizationScopeClass = (Class<io.swagger.annotations.AuthorizationScope>)classLoader
|
|
|
|
authorizationScopeClass = (Class<io.swagger.annotations.AuthorizationScope>)classLoader
|
|
|
|
.loadClass((io.swagger.annotations.AuthorizationScope.class.getName()));
|
|
|
|
.loadClass((io.swagger.annotations.AuthorizationScope.class.getName()));
|
|
|
|
|
|
|
|
extensionClass = (Class<io.swagger.annotations.Extension>)classLoader
|
|
|
|
|
|
|
|
.loadClass((io.swagger.annotations.Extension.class.getName()));
|
|
|
|
|
|
|
|
extensionPropertyClass = (Class<io.swagger.annotations.ExtensionProperty>)classLoader
|
|
|
|
|
|
|
|
.loadClass(io.swagger.annotations.ExtensionProperty.class.getName());
|
|
|
|
|
|
|
|
scopeClass = (Class<org.wso2.carbon.apimgt.annotations.api.Scope>) classLoader
|
|
|
|
|
|
|
|
.loadClass(org.wso2.carbon.apimgt.annotations.api.Scope.class.getName());
|
|
|
|
|
|
|
|
scopesClass = (Class<org.wso2.carbon.apimgt.annotations.api.Scopes>) classLoader
|
|
|
|
|
|
|
|
.loadClass(org.wso2.carbon.apimgt.annotations.api.Scopes.class.getName());
|
|
|
|
|
|
|
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
log.error("An error has occurred while loading classes ", e);
|
|
|
|
log.error("An error has occurred while loading classes ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -135,6 +150,7 @@ public class AnnotationProcessor {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
clazz = classLoader.loadClass(className);
|
|
|
|
clazz = classLoader.loadClass(className);
|
|
|
|
Annotation apiAnno = clazz.getAnnotation(apiClazz);
|
|
|
|
Annotation apiAnno = clazz.getAnnotation(apiClazz);
|
|
|
|
|
|
|
|
apiScopes = processAPIScopes(apiAnno);
|
|
|
|
List<Permission> resourceList;
|
|
|
|
List<Permission> resourceList;
|
|
|
|
if (apiAnno != null) {
|
|
|
|
if (apiAnno != null) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
@ -165,7 +181,9 @@ public class AnnotationProcessor {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
log.error("Error when passing the api annotation for device type apis.");
|
|
|
|
log.error("Error when passing the api annotation for device type apis.", e);
|
|
|
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
|
|
|
log.error("Error when passing the scopes annotation for device type apis.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiPermissions;
|
|
|
|
return apiPermissions;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -342,28 +360,53 @@ public class AnnotationProcessor {
|
|
|
|
|
|
|
|
|
|
|
|
private void setPermission(Annotation currentMethod, Permission permission) throws Throwable {
|
|
|
|
private void setPermission(Annotation currentMethod, Permission permission) throws Throwable {
|
|
|
|
InvocationHandler methodHandler = Proxy.getInvocationHandler(currentMethod);
|
|
|
|
InvocationHandler methodHandler = Proxy.getInvocationHandler(currentMethod);
|
|
|
|
Annotation[] authorizations = (Annotation[]) methodHandler.invoke(currentMethod
|
|
|
|
Annotation[] extensions = (Annotation[]) methodHandler.invoke(currentMethod,
|
|
|
|
, apiOperation.getMethod(SWAGGER_ANNOTATIONS_AUTHORIZATIONS,null),null);
|
|
|
|
apiOperation.getMethod(SWAGGER_ANNOTATIONS_EXTENSIONS, null), null);
|
|
|
|
for(int i=0; i<authorizations.length; i++){
|
|
|
|
methodHandler = Proxy.getInvocationHandler(extensions[0]);
|
|
|
|
methodHandler = Proxy.getInvocationHandler(authorizations[i]);
|
|
|
|
Annotation[] properties = (Annotation[])methodHandler.invoke(extensions[0], extensionClass
|
|
|
|
String value =(String)methodHandler.invoke(authorizations[i], authorizationClass.getMethod("value", null)
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES,null), null);
|
|
|
|
,null);
|
|
|
|
Scope scope;
|
|
|
|
if(SWAGGER_ANNOTATIONS_PERMISSION.equals(value)){
|
|
|
|
for (Annotation property : properties) {
|
|
|
|
Annotation[] scopes =(Annotation[])methodHandler.invoke(authorizations[i], authorizationClass
|
|
|
|
methodHandler = Proxy.getInvocationHandler(property);
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_SCOPES, null),null);
|
|
|
|
String scopeKey = (String) methodHandler.invoke(property, extensionPropertyClass
|
|
|
|
String[] scopesList = new String[scopes.length];
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_VALUE, null),null);
|
|
|
|
String[] descriptions = new String[scopes.length];
|
|
|
|
if (!scopeKey.isEmpty()) {
|
|
|
|
for(int j=0; j<scopes.length; j++){
|
|
|
|
scope = apiScopes.get(scopeKey);
|
|
|
|
methodHandler = Proxy.getInvocationHandler(scopes[j]);
|
|
|
|
permission.setName(scope.getName());
|
|
|
|
scopesList[j] = (String)methodHandler.invoke(authorizations[j], authorizationScopeClass
|
|
|
|
//TODO: currently permission tree supports only adding one permission per API point.
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_SCOPE, null),null);
|
|
|
|
permission.setPath(scope.getRoles().split(" ")[0]);
|
|
|
|
descriptions[j] = (String)methodHandler.invoke(authorizations[j], authorizationScopeClass
|
|
|
|
}
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_DESCRIPTION, null),null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//todo currently permission tree supports only adding one permission per API point.
|
|
|
|
|
|
|
|
permission.setName(descriptions[0]);
|
|
|
|
|
|
|
|
permission.setPath(scopesList[0]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<String,Scope> processAPIScopes(Annotation annotation) throws Throwable {
|
|
|
|
|
|
|
|
Map<String, Scope> scopes = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InvocationHandler methodHandler = Proxy.getInvocationHandler(annotation);
|
|
|
|
|
|
|
|
Annotation[] annotatedScopes = (Annotation[]) methodHandler.invoke(annotation, scopesClass
|
|
|
|
|
|
|
|
.getMethod(ANNOTATIONS_SCOPES, null), null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Scope scope;
|
|
|
|
|
|
|
|
String permissions[];
|
|
|
|
|
|
|
|
StringBuilder aggregatedPermissions = new StringBuilder();
|
|
|
|
|
|
|
|
for(int i=0; i<annotatedScopes.length; i++){
|
|
|
|
|
|
|
|
methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]);
|
|
|
|
|
|
|
|
scope = new Scope();
|
|
|
|
|
|
|
|
scope.setName(invokeMethod(scopeClass
|
|
|
|
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_NAME), annotatedScopes[i], STRING));
|
|
|
|
|
|
|
|
scope.setDescription(invokeMethod(scopeClass
|
|
|
|
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_DESCRIPTION), annotatedScopes[i], STRING));
|
|
|
|
|
|
|
|
scope.setKey(invokeMethod(scopeClass
|
|
|
|
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_KEY), annotatedScopes[i], STRING));
|
|
|
|
|
|
|
|
permissions = (String[])methodHandler.invoke(annotatedScopes[i], scopeClass
|
|
|
|
|
|
|
|
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_PERMISSIONS, null),null);
|
|
|
|
|
|
|
|
for (String permission : permissions) {
|
|
|
|
|
|
|
|
aggregatedPermissions.append(permission);
|
|
|
|
|
|
|
|
aggregatedPermissions.append(" ");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.setRoles(aggregatedPermissions.toString());
|
|
|
|
|
|
|
|
scopes.put(scope.getKey(), scope);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return scopes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|