Search in sources :

Example 1 with AuthSPrincipal

use of com.sun.identity.rest.AuthSPrincipal in project OpenAM by OpenRock.

the class ScriptConditionTest method successfulEvaluation.

@Test
public void successfulEvaluation() throws EntitlementException, ScriptException, javax.script.ScriptException, IdRepoException, SSOException {
    // Given
    Subject subject = new Subject();
    SSOToken token = mock(SSOToken.class);
    subject.getPrivateCredentials().add(token);
    subject.getPrincipals().add(new AuthSPrincipal("user"));
    Map<String, Set<String>> env = new HashMap<>();
    Map<String, Set<String>> advice = new HashMap<>();
    final ScriptConfiguration configuration = ScriptConfiguration.builder().setId("123-456-789").setName("test-script").setContext(ScriptContext.POLICY_CONDITION).setLanguage(SupportedScriptingLanguage.JAVASCRIPT).setScript("some-script-here").build();
    scriptCondition = new ScriptCondition() {

        @Override
        protected ScriptConfiguration getScriptConfiguration(String realm) throws ScriptException {
            return configuration;
        }
    };
    given(coreWrapper.getIdentity(token)).willReturn(mock(AMIdentity.class));
    // When
    scriptCondition.setScriptId("123-456-789");
    ConditionDecision decision = scriptCondition.evaluate("/abc", subject, "http://a:b/c", env);
    // Then
    // Hard to test true scenario
    assertThat(decision.isSatisfied()).isFalse();
    verify(scriptEvaluator).evaluateScript(scriptObjectCaptor.capture(), bindingsCaptor.capture());
    ScriptObject scriptObject = scriptObjectCaptor.getValue();
    assertThat(scriptObject.getName()).isEqualTo("test-script");
    assertThat(scriptObject.getLanguage()).isEqualTo(SupportedScriptingLanguage.JAVASCRIPT);
    assertThat(scriptObject.getScript()).isEqualTo("some-script-here");
    Bindings bindings = bindingsCaptor.getValue();
    assertThat(bindings.get("logger")).isEqualTo(PolicyConstants.DEBUG);
    assertThat(bindings.get("username")).isEqualTo("user");
    assertThat(bindings.get("resourceURI")).isEqualTo("http://a:b/c");
    assertThat(bindings.get("environment")).isEqualTo(env);
    assertThat(bindings.get("advice")).isEqualTo(advice);
    assertThat(bindings.get("httpClient")).isEqualTo(restletHttpClient);
    assertThat(bindings.get("authorized")).isEqualTo(Boolean.FALSE);
    assertThat(bindings.get("ttl")).isEqualTo(Long.MAX_VALUE);
}
Also used : ScriptObject(org.forgerock.openam.scripting.ScriptObject) SSOToken(com.iplanet.sso.SSOToken) Set(java.util.Set) HashMap(java.util.HashMap) ConditionDecision(com.sun.identity.entitlement.ConditionDecision) Bindings(javax.script.Bindings) Subject(javax.security.auth.Subject) ScriptException(org.forgerock.openam.scripting.ScriptException) AMIdentity(com.sun.identity.idm.AMIdentity) AuthSPrincipal(com.sun.identity.rest.AuthSPrincipal) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) Test(org.testng.annotations.Test)

Example 2 with AuthSPrincipal

use of com.sun.identity.rest.AuthSPrincipal in project OpenAM by OpenRock.

the class ScriptConditionTest method missingScriptConfiguration.

@Test(expectedExceptions = EntitlementException.class, expectedExceptionsMessageRegExp = "Script condition is unable to load script 123-456-789.")
public void missingScriptConfiguration() throws ScriptException, EntitlementException {
    // Given
    Subject subject = new Subject();
    subject.getPrincipals().add(new AuthSPrincipal("user"));
    Map<String, Set<String>> env = new HashMap<>();
    scriptCondition = new ScriptCondition() {

        @Override
        protected ScriptConfiguration getScriptConfiguration(String realm) throws ScriptException {
            return null;
        }
    };
    // When
    scriptCondition.setScriptId("123-456-789");
    scriptCondition.evaluate("/abc", subject, "http://a:b/c", env);
}
Also used : ScriptException(org.forgerock.openam.scripting.ScriptException) Set(java.util.Set) HashMap(java.util.HashMap) AuthSPrincipal(com.sun.identity.rest.AuthSPrincipal) ScriptConfiguration(org.forgerock.openam.scripting.service.ScriptConfiguration) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 3 with AuthSPrincipal

use of com.sun.identity.rest.AuthSPrincipal in project OpenAM by OpenRock.

the class AuthUtils method createSubject.

public static Subject createSubject(String uuid) {
    Set<Principal> userPrincipals = new HashSet<Principal>(2);
    userPrincipals.add(new AuthSPrincipal(uuid));
    return new Subject(false, userPrincipals, new HashSet(), new HashSet());
}
Also used : AuthSPrincipal(com.sun.identity.rest.AuthSPrincipal) AuthSPrincipal(com.sun.identity.rest.AuthSPrincipal) Principal(java.security.Principal) Subject(javax.security.auth.Subject) HashSet(java.util.HashSet)

Aggregations

AuthSPrincipal (com.sun.identity.rest.AuthSPrincipal)3 Subject (javax.security.auth.Subject)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 ScriptException (org.forgerock.openam.scripting.ScriptException)2 ScriptConfiguration (org.forgerock.openam.scripting.service.ScriptConfiguration)2 Test (org.testng.annotations.Test)2 SSOToken (com.iplanet.sso.SSOToken)1 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)1 AMIdentity (com.sun.identity.idm.AMIdentity)1 Principal (java.security.Principal)1 HashSet (java.util.HashSet)1 Bindings (javax.script.Bindings)1 ScriptObject (org.forgerock.openam.scripting.ScriptObject)1