package eu.musesproject.server.eventprocessor.policy.manage; import org.wso2.balana.attr.AttributeValue; import org.wso2.balana.attr.BagAttribute; import org.wso2.balana.attr.StringAttribute; import org.wso2.balana.cond.EvaluationResult; import org.wso2.balana.ctx.EvaluationCtx; import org.wso2.balana.finder.AttributeFinderModule; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /* * #%L * MUSES Server * %% * Copyright (C) 2013 - 2014 S2 Grupo * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ /** * Sample attribute finder module */ public class MusesAttributeFinderModule extends AttributeFinderModule{ private URI defaultSubjectId; public MusesAttributeFinderModule() { try { defaultSubjectId = new URI("urn:oasis:names:tc:xacml:1.0:subject:subject-id"); } catch (URISyntaxException e) { //ignore } } @Override public Set<String> getSupportedCategories() { Set<String> categories = new HashSet<String>(); categories.add("urn:oasis:names:tc:xacml:1.0:subject-category:access-subject"); return categories; } @Override public Set getSupportedIds() { Set<String> ids = new HashSet<String>(); ids.add("http://test.org/claim/role"); return ids; } @Override public EvaluationResult findAttribute(URI attributeType, URI attributeId, String issuer, URI category, EvaluationCtx context) { String roleName = null; List<AttributeValue> attributeValues = new ArrayList<AttributeValue>(); EvaluationResult result = context.getAttribute(attributeType, defaultSubjectId, issuer, category); if(result != null && result.getAttributeValue() != null && result.getAttributeValue().isBag()){ BagAttribute bagAttribute = (BagAttribute) result.getAttributeValue(); if(bagAttribute.size() > 0){ String userName = ((AttributeValue) bagAttribute.iterator().next()).encode(); roleName = findRole(userName); } } if (roleName != null) { attributeValues.add(new StringAttribute(roleName)); } return new EvaluationResult(new BagAttribute(attributeType, attributeValues)); } @Override public boolean isDesignatorSupported() { return true; } private String findRole(String userName){ if(userName.equals("bob")){ return "User"; } else if(userName.equals("alice")){ return "Employee"; } else if(userName.equals("peter")){ return "Manager"; } return null; } }