use of org.forgerock.openam.entitlement.rest.PolicyStore in project OpenAM by OpenRock.
the class ConfigureOAuth2 method execute.
public String execute(Locale locale, Map params) throws WorkflowException {
final String type = getString(params, TYPE);
final String realm = getString(params, REALM);
final SSOToken token = AccessController.doPrivileged(AdminTokenAction.getInstance());
if (StringUtils.isEmpty(type)) {
throw new WorkflowException("type parameter is required");
}
//replace service attributes
final Map<String, Set<String>> attrValues = getDefaultOAuth2ProviderAttributes(token);
attrValues.putAll(PROFILE_SETTINGS.get(type));
attrValues.put(SUPPORTED_SCOPES, translate(realm, SUPPORTED_SCOPE_KEYS.get(type)));
attrValues.put(SUPPORTED_CLAIMS, translate(realm, SUPPORTED_CLAIM_KEYS.get(type)));
attrValues.put(REFRESH_TOKEN_LIFETIME_NAME, singleton(getString(params, RTL)));
attrValues.put(AUTHZ_CODE_LIFETIME_NAME, singleton(getString(params, ACL)));
attrValues.put(ACCESS_TOKEN_LIFETIME_NAME, singleton(getString(params, ATL)));
attrValues.put(ISSUE_REFRESH_TOKEN, singleton(getString(params, IRT)));
attrValues.put(ISSUE_REFRESH_TOKEN_ON_REFRESHING_TOKEN, singleton(getString(params, IRTR)));
attrValues.put(SCOPE_PLUGIN_CLASS, singleton(getString(params, SIC)));
createProvider(OAUTH2_SERVICE_NAME, token, realm, attrValues);
final boolean createUmaService = "uma".equals(type);
if (createUmaService) {
createProvider(UMA_SERVICE_NAME, token, realm, Collections.<String, Set<String>>emptyMap());
}
String policyURL = getRequestURL(params) + format(OAUTH2_AUTHORIZE_ENDPOINT, "/".equals(realm) ? "" : realm);
//check if policy exists
boolean createPolicy = false;
try {
Subject adminSubject = SubjectUtils.createSuperAdminSubject();
PolicyStore policyStore = storeProvider.getPolicyStore(adminSubject, realm);
try {
if (policyStore.read(POLICY_NAME) == null) {
createPolicy = true;
}
} catch (Exception e) {
createPolicy = true;
}
if (createPolicy) {
Privilege toStore = Privilege.getNewInstance();
Map<String, Boolean> actions = new HashMap<>();
actions.put("POST", true);
actions.put("GET", true);
Entitlement entitlement = new Entitlement();
entitlement.setActionValues(actions);
entitlement.setResourceName(policyURL);
entitlement.setApplicationName(POLICY_APPLICATION_NAME);
toStore.setResourceTypeUuid(getUrlResourceTypeId(adminSubject, realm));
toStore.setSubject(new AuthenticatedUsers());
toStore.setName(POLICY_NAME);
toStore.setEntitlement(entitlement);
policyStore.create(toStore);
}
} catch (EntitlementException e) {
DEBUG.error("ConfigureOAuth2.execute() : Unable to create policy", e);
throw new WorkflowException("oauth2.provider.policy.failed");
}
String messageTemplate = getMessage(MESSAGE, locale);
return format(messageTemplate, createUmaService ? getMessage(UMA_SERVICE_CREATED, locale) : "", realm, format(getMessage(createPolicy ? POLICY_CREATED : POLICY_EXISTS, locale), POLICY_NAME));
}
Aggregations