use of org.forgerock.services.context.SecurityContext in project OpenAM by OpenRock.
the class SessionResourceTest method actionCollectionShouldLogoutSessionAndReturnEmptyJsonObjectWhenSSOTokenValid.
@Test
public void actionCollectionShouldLogoutSessionAndReturnEmptyJsonObjectWhenSSOTokenValid() throws SSOException {
//Given
cookieResponse = "SSO_TOKEN_ID";
final AttributesContext attrContext = new AttributesContext(new SessionContext(new RootContext(), mock(Session.class)));
final AdviceContext adviceContext = new AdviceContext(attrContext, Collections.<String>emptySet());
final SecurityContext securityContext = new SecurityContext(adviceContext, null, null);
final Context context = ClientContext.newInternalClientContext(new SSOTokenContext(mock(Debug.class), null, securityContext));
final ActionRequest request = mock(ActionRequest.class);
final SSOTokenID ssoTokenId = mock(SSOTokenID.class);
given(request.getAction()).willReturn(LOGOUT_ACTION_ID);
given(authUtilsWrapper.logout(ssoTokenId.toString(), null, null)).willReturn(true);
//When
Promise<ActionResponse, ResourceException> promise = sessionResource.actionCollection(context, request);
//Then
assertThat(promise).succeeded().withContent().stringAt("result").isEqualTo("Successfully logged out");
}
use of org.forgerock.services.context.SecurityContext in project OpenAM by OpenRock.
the class AuditTestUtils method mockAuditContext.
public static Context mockAuditContext() throws Exception {
final Context httpContext = new HttpContext(jsonFromFile("/org/forgerock/openam/rest/fluent/httpContext.json"), AbstractAuditFilterTest.class.getClassLoader());
final Subject callerSubject = new Subject();
final Context securityContext = new SecurityContext(httpContext, null, null);
final Context subjectContext = new SSOTokenContext(mock(Debug.class), null, securityContext) {
@Override
public Subject getCallerSubject() {
return callerSubject;
}
@Override
public SSOToken getCallerSSOToken() {
SSOToken token = mock(SSOToken.class);
try {
given(token.getProperty(Constants.AM_CTX_ID)).willReturn("TRACKING_ID");
given(token.getProperty(Constants.UNIVERSAL_IDENTIFIER)).willReturn("USER_ID");
} catch (SSOException e) {
// won't happen - it's a mock
}
return token;
}
};
final Context clientContext = ClientContext.newInternalClientContext(subjectContext);
return new RequestAuditContext(new AuditInfoContext(clientContext, AuditConstants.Component.AUDIT));
}
use of org.forgerock.services.context.SecurityContext in project OpenAM by OpenRock.
the class ElevatedConnectionFactoryWrapperTest method requestGetsElevatedToAdminSession.
@Test
public void requestGetsElevatedToAdminSession() throws Exception {
// Given
SSOToken ssoToken = mock(SSOToken.class);
given(ssoTokenPrivilegedAction.run()).willReturn(ssoToken);
SSOPrincipal principal = new SSOPrincipal("test");
given(ssoToken.getPrincipal()).willReturn(principal);
SSOTokenID tokenID = mock(SSOTokenID.class);
given(ssoToken.getTokenID()).willReturn(tokenID);
given(internalConnectionFactory.getConnection()).willReturn(connection);
// When
RootContext context = new RootContext();
ReadRequest readRequest = Requests.newReadRequest("/test", "abc");
try (Connection connection = connectionFactory.getConnection()) {
connection.read(context, readRequest);
}
// Then
verify(connection).read(contextCaptor.capture(), eq(readRequest));
Context capturedContext = contextCaptor.getValue();
assertThat(capturedContext.containsContext(SecurityContext.class)).isTrue();
SecurityContext securityContext = capturedContext.asContext(SecurityContext.class);
assertThat(securityContext.getAuthenticationId()).isEqualTo("test");
assertThat(securityContext.getAuthorization()).containsOnlyKeys("authLevel", "tokenId");
}
use of org.forgerock.services.context.SecurityContext in project OpenAM by OpenRock.
the class ResourceTypesResourceTest method setUp.
@BeforeMethod
public void setUp() throws ResourceException {
callerSubject = new Subject();
// to mock the HTTP method, we need the following contexts
Context httpContext = new HttpContext(json(object(field(HttpContext.ATTR_HEADERS, Collections.singletonMap("method", Arrays.asList("PUT"))), field(HttpContext.ATTR_PARAMETERS, Collections.emptyMap()))), null);
Context securityContext = new SecurityContext(httpContext, null, null);
Context subjectContext = new SSOTokenContext(mock(Debug.class), null, securityContext) {
@Override
public Subject getCallerSubject() {
return callerSubject;
}
};
RealmContext realmContext = new RealmContext(subjectContext);
realmContext.setSubRealm("/", "/");
mockServerContext = ClientContext.newInternalClientContext(realmContext);
resourceTypeService = mock(MockResourceTypeService.class);
Debug debug = mock(Debug.class);
resourceTypesResource = new ResourceTypesResource(debug, new EntitlementsExceptionMappingHandler(EntitlementRestGuiceModule.getEntitlementsErrorHandlers()), resourceTypeService);
rawData.put("name", Collections.singleton("myResourceType"));
rawData.put("description", Collections.singleton("myResourceType"));
rawData.put("realm", Collections.singleton("/"));
rawData.put("actions", Collections.singleton("CREATE"));
rawData.put("patterns", Collections.singleton("http://example.com:80/*"));
rawData.put("creationDate", Collections.singleton(String.valueOf(new Date().getTime())));
rawData.put("lastModifiedDate", Collections.singleton(String.valueOf(new Date().getTime())));
}
use of org.forgerock.services.context.SecurityContext in project OpenAM by OpenRock.
the class RestRouterIT method mockRequiredContexts.
private Context mockRequiredContexts() {
final HttpContext httpContext = new HttpContext(json(object(field(HttpContext.ATTR_HEADERS, Collections.singletonMap("Accept-Language", Arrays.asList("en"))), field(HttpContext.ATTR_PARAMETERS, Collections.emptyMap()))), null);
SecurityContext securityContext = new SecurityContext(mockContext(httpContext), null, null);
return new SSOTokenContext(mock(Debug.class), null, securityContext) {
@Override
public Subject getCallerSubject() {
return new Subject();
}
@Override
public SSOToken getCallerSSOToken() {
SSOToken token = mock(SSOToken.class);
try {
given(token.getProperty(Constants.AM_CTX_ID)).willReturn("TRACKING_ID");
given(token.getProperty(Constants.UNIVERSAL_IDENTIFIER)).willReturn("USER_ID");
} catch (SSOException e) {
// won't happen - it's a mock
}
return token;
}
};
}
Aggregations