porting to smartgears 4

This commit is contained in:
Lucio Lelii 2022-06-15 17:52:46 +02:00
parent 3b509279a0
commit 6cf84fe511
3 changed files with 45 additions and 34 deletions

63
pom.xml
View File

@ -1,12 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-control-library</artifactId>
<version>2.0.0-SNAPSHOT</version>
<name>Authorization Control Library</name>
<description>Authorization Control Library</description>
<parent>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-control-library</artifactId>
<version>2.0.0-SNAPSHOT</version>
<name>Authorization Control Library</name>
<description>Authorization Control Library</description>
<parent>
<artifactId>maven-parent</artifactId>
<groupId>org.gcube.tools</groupId>
<version>1.1.0</version>
@ -15,27 +17,38 @@
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/authorization-control-library.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/authorization-control-library.git</developerConnection>
<url>https://code-repo.d4science.org/gCubeSystem/authorization-control-library</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-authorization</artifactId>
<version>[3.0.0-SNAPSHOT, 4.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>3.0.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>common-security</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
@ -82,12 +95,12 @@
</plugin>
</plugins>
</pluginManagement>
</pluginManagement>
<plugins>
<plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

View File

@ -14,9 +14,8 @@ import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.gcube.common.authorization.control.annotations.AuthorizationControl;
import org.gcube.common.authorization.library.exception.AuthorizationException;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.common.authorization.utils.user.User;
import org.gcube.common.security.Owner;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -41,18 +40,18 @@ public class AuthorizationAspect {
AuthorizationControl authAnn = (AuthorizationControl) method.getAnnotation(AuthorizationControl.class);
log.info("aspect before with annotation {} with action {}, allowed {} in method {}", authAnn.annotationType(), authAnn.actions(), authAnn.allowedRoles(), authAnn.allowedUsers(), method.getName());
User user = SecretManagerProvider.instance.get().getUser();
Owner user = SecretManagerProvider.instance.get().getOwner();
String userId = user.getUsername();
String userId = user.getId();
Collection<String> userRoles = user.getRoles();
log.info("user role is {} and user {}", userRoles, userId );
if (authAnn.allowedUsers().length!=0 && !Arrays.asList(authAnn.allowedUsers()).contains(userId)) {
RuntimeException ex = authAnn.exception().getConstructor(Throwable.class).newInstance(new AuthorizationException(String.format("user %s not allowed to call method %s", userId, method.getName())));
RuntimeException ex = authAnn.exception().getConstructor(Throwable.class).newInstance(new SecurityException(String.format("user %s not allowed to call method %s", userId, method.getName())));
throw ex;
}
List<String> allowedRoles = Arrays.asList(authAnn.allowedRoles());
if (authAnn.allowedRoles().length!=0 && userRoles.stream().filter(i -> allowedRoles.contains(i)).collect(Collectors.toList()).isEmpty()) {
RuntimeException ex = authAnn.exception().getConstructor(Throwable.class).newInstance(new AuthorizationException(String.format("user %s not allowed to call method %s (role not valid)", userId, method.getName())));
RuntimeException ex = authAnn.exception().getConstructor(Throwable.class).newInstance(new SecurityException(String.format("user %s not allowed to call method %s (role not valid)", userId, method.getName())));
throw ex;
}
}

View File

@ -6,14 +6,13 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.gcube.common.authorization.library.policies.Action;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AuthorizationControl {
Action[] actions() default {};
String[] actions() default {};
String[] allowedUsers() default {};
String[] allowedRoles() default {};
Class<? extends RuntimeException> exception();