hello-world-service/src/main/java/org/gcube/service/helloworld/services/AuthenticatedService.java

68 lines
2.1 KiB
Java
Raw Normal View History

2024-02-22 09:56:36 +01:00
package org.gcube.service.helloworld.services;
2024-02-29 17:21:49 +01:00
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
2024-02-22 09:56:36 +01:00
import org.gcube.common.authorization.control.annotations.AuthorizationControl;
import org.gcube.common.security.providers.SecretManagerProvider;
import org.gcube.common.security.secrets.Secret;
import org.gcube.smartgears.utils.InnerMethodName;
2024-02-23 17:35:59 +01:00
/**
2024-02-29 17:21:49 +01:00
* @author Lucio Lelii (ISTI - CNR)
2024-02-23 17:35:59 +01:00
* @author Alfredo Oliviero (ISTI - CNR)
*/
2024-02-22 09:56:36 +01:00
@Path("auth")
2024-02-23 17:35:59 +01:00
public class AuthenticatedService {
2024-02-22 09:56:36 +01:00
private static final String ALLOWED_ROLE = "myRole";
2024-02-23 12:14:09 +01:00
private static final String ALLOWED_ROLE_ORG = "OrganizationMember";
2024-02-23 19:01:34 +01:00
private static final String ALLOWED_ROLE_MEMBER = "Member";
2024-02-23 17:35:59 +01:00
@AuthorizationControl(allowedRoles = { ALLOWED_ROLE_ORG })
2024-02-23 12:14:09 +01:00
@GET
@Path("org_member")
@Produces(MediaType.TEXT_PLAIN)
public String authorizedOrg() {
InnerMethodName.set("auth");
Secret secret = SecretManagerProvider.get();
String userId = secret.getOwner().getId();
String context = secret.getContext();
2024-02-23 17:35:59 +01:00
return String.format(
2024-02-26 10:05:14 +01:00
"User %s in context %s is authorized to execute this method because he has the role %s", userId,
context, ALLOWED_ROLE_ORG);
2024-02-23 12:14:09 +01:00
}
2024-02-23 17:35:59 +01:00
2024-02-23 19:01:34 +01:00
@AuthorizationControl(allowedRoles = { ALLOWED_ROLE_MEMBER })
@GET
@Path("member")
@Produces(MediaType.TEXT_PLAIN)
public String authorizedMember() {
InnerMethodName.set("auth");
Secret secret = SecretManagerProvider.get();
String userId = secret.getOwner().getId();
String context = secret.getContext();
return String.format(
2024-02-26 10:05:14 +01:00
"User %s in context %s is authorized to execute this method because he has the role %s", userId,
context, ALLOWED_ROLE_MEMBER);
2024-02-23 19:01:34 +01:00
}
2024-02-23 17:35:59 +01:00
@AuthorizationControl(allowedRoles = { ALLOWED_ROLE })
2024-02-22 09:56:36 +01:00
@GET
2024-02-23 12:14:09 +01:00
@Path("")
@Produces(MediaType.TEXT_PLAIN)
2024-02-22 09:56:36 +01:00
public String authorized() {
InnerMethodName.set("auth");
Secret secret = SecretManagerProvider.get();
String userId = secret.getOwner().getId();
String context = secret.getContext();
2024-02-23 17:35:59 +01:00
return String.format(
2024-02-26 10:05:14 +01:00
"User %s in context %s is authorized to execute this method because he has the role %s", userId,
context, ALLOWED_ROLE);
2024-02-22 09:56:36 +01:00
}
2024-02-23 17:35:59 +01:00
2024-02-22 09:56:36 +01:00
}