cc.protea.foundation.utility.providers.ProteaSecurityContext Maven / Gradle / Ivy
package cc.protea.foundation.utility.providers;
import java.security.Principal;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.SecurityContext;
import cc.protea.foundation.utility.UserUtil;
public class ProteaSecurityContext implements SecurityContext {
ContainerRequestContext requestContext;
Integer userId;
public ProteaSecurityContext(final Integer userId, final ContainerRequestContext requestContext) {
this.userId = userId;
}
@Override
public Principal getUserPrincipal() {
if (userId == null) {
return null;
}
return new Principal() {
@Override
public String getName() {
return userId == null ? null : userId.toString();
}
};
}
@Override
public boolean isUserInRole(final String role) {
if ("loggedIn".equals(role)) {
return userId != null;
}
return userId == null ? false : UserUtil.isUserInRole(userId, role);
}
@Override
public boolean isSecure() {
return "https".equals(requestContext.getUriInfo().getBaseUri().getScheme());
}
@Override
public String getAuthenticationScheme() {
return SecurityContext.BASIC_AUTH;
}
}