use of org.sagebionetworks.bridge.models.accounts.UserSession in project BridgeServer2 by Sage-Bionetworks.
the class CacheProvider method getUserSession.
public UserSession getUserSession(String sessionToken) {
checkNotNull(sessionToken);
try {
CacheKey tokenToUserIdKey = CacheKey.tokenToUserId(sessionToken);
String userId = jedisOps.get(tokenToUserIdKey.toString());
if (userId != null) {
CacheKey userIdToSessionKey = CacheKey.userIdToSession(userId);
String ser = jedisOps.get(userIdToSessionKey.toString());
if (ser != null) {
JsonNode node = adjustJsonWithStudyIdentifier(ser);
UserSession session = BridgeObjectMapper.get().treeToValue(node, UserSession.class);
// invalidate its own session.
if (session.getSessionToken().equals(sessionToken)) {
return session;
}
// Otherwise, delete the key sessionToken key (it's known to be invalid)
removeObject(tokenToUserIdKey);
}
}
return null;
} catch (Throwable e) {
promptToStartRedisIfLocal(e);
throw new BridgeServiceException(e);
}
}
use of org.sagebionetworks.bridge.models.accounts.UserSession in project BridgeServer2 by Sage-Bionetworks.
the class RequestContextTest method updateFromSessionNullSponsorService.
@Test
public void updateFromSessionNullSponsorService() {
RequestContext context = new RequestContext.Builder().withRequestId(REQUEST_ID).build();
assertNotNull(context.getId());
assertNull(context.getCallerAppId());
assertEquals(ImmutableSet.of(), context.getCallerEnrolledStudies());
assertFalse(context.isAdministrator());
RequestContext.set(context);
UserSession session = new UserSession(new StudyParticipant.Builder().withStudyIds(USER_STUDY_IDS).withRoles(ImmutableSet.of(DEVELOPER)).withId(TEST_USER_ID).withOrgMembership(TEST_ORG_ID).withLanguages(LANGUAGES).build());
session.setAuthenticated(true);
session.setAppId(TEST_APP_ID);
RequestContext retValue = RequestContext.updateFromSession(session, mockSponsorService);
assertEquals(retValue.getId(), REQUEST_ID);
assertEquals(retValue.getCallerAppId(), TEST_APP_ID);
assertEquals(retValue.getCallerEnrolledStudies(), USER_STUDY_IDS);
assertEquals(retValue.getOrgSponsoredStudies(), ImmutableSet.of());
assertTrue(retValue.isAdministrator());
assertTrue(retValue.isInRole(DEVELOPER));
assertEquals(retValue.getCallerUserId(), TEST_USER_ID);
assertEquals(retValue.getCallerOrgMembership(), TEST_ORG_ID);
assertEquals(retValue.getCallerLanguages(), LANGUAGES);
RequestContext threadValue = RequestContext.get();
assertSame(retValue, threadValue);
}
use of org.sagebionetworks.bridge.models.accounts.UserSession in project BridgeServer2 by Sage-Bionetworks.
the class RequestContextTest method updateFromSession.
@Test
public void updateFromSession() {
RequestContext context = new RequestContext.Builder().withRequestId(REQUEST_ID).build();
assertNotNull(context.getId());
assertNull(context.getCallerAppId());
assertEquals(ImmutableSet.of(), context.getCallerEnrolledStudies());
assertFalse(context.isAdministrator());
RequestContext.set(context);
when(mockSponsorService.getSponsoredStudyIds(TEST_APP_ID, TEST_ORG_ID)).thenReturn(USER_STUDY_IDS);
UserSession session = new UserSession(new StudyParticipant.Builder().withStudyIds(USER_STUDY_IDS).withRoles(ImmutableSet.of(DEVELOPER)).withId(TEST_USER_ID).withOrgMembership(TEST_ORG_ID).withLanguages(LANGUAGES).build());
session.setAuthenticated(true);
session.setAppId(TEST_APP_ID);
RequestContext retValue = RequestContext.updateFromSession(session, mockSponsorService);
assertEquals(retValue.getId(), REQUEST_ID);
assertEquals(retValue.getCallerAppId(), TEST_APP_ID);
assertEquals(retValue.getCallerEnrolledStudies(), USER_STUDY_IDS);
assertEquals(retValue.getOrgSponsoredStudies(), USER_STUDY_IDS);
assertTrue(retValue.isAdministrator());
assertTrue(retValue.isInRole(DEVELOPER));
assertEquals(retValue.getCallerUserId(), TEST_USER_ID);
assertEquals(retValue.getCallerOrgMembership(), TEST_ORG_ID);
assertEquals(retValue.getCallerLanguages(), LANGUAGES);
RequestContext threadValue = RequestContext.get();
assertSame(retValue, threadValue);
}
use of org.sagebionetworks.bridge.models.accounts.UserSession in project BridgeServer2 by Sage-Bionetworks.
the class RequestContextTest method updateFromSessionNoOrgMembership.
// Non-admins who have an organizational relationship are given a specific set of studies
// that they will have to match in some security checks. Verify this is skipped for accounts
// with no organizational membership.
@Test
public void updateFromSessionNoOrgMembership() {
when(mockSponsorService.getSponsoredStudyIds(TEST_APP_ID, TEST_ORG_ID)).thenReturn(USER_STUDY_IDS);
UserSession session = new UserSession(new StudyParticipant.Builder().withStudyIds(USER_STUDY_IDS).withRoles(ImmutableSet.of(DEVELOPER)).withId(TEST_USER_ID).withLanguages(LANGUAGES).build());
session.setAuthenticated(true);
session.setAppId(TEST_APP_ID);
RequestContext retValue = RequestContext.updateFromSession(session, mockSponsorService);
assertEquals(retValue.getOrgSponsoredStudies(), ImmutableSet.of());
RequestContext threadValue = RequestContext.get();
assertEquals(threadValue.getOrgSponsoredStudies(), ImmutableSet.of());
verify(mockSponsorService, never()).getSponsoredStudyIds(any(), any());
}
use of org.sagebionetworks.bridge.models.accounts.UserSession in project BridgeServer2 by Sage-Bionetworks.
the class CacheProviderTest method testSetUserSessionNullUser.
@Test
public void testSetUserSessionNullUser() throws Exception {
UserSession session = new UserSession();
session.setSessionToken(DECRYPTED_SESSION_TOKEN);
try {
cacheProvider.setUserSession(session);
} catch (NullPointerException e) {
assertTrue(true, "NPE expected.");
} catch (Throwable e) {
fail(e.getMessage());
}
verify(transaction, never()).setex(eq(TOKEN_TO_USER_ID.toString()), anyInt(), anyString());
verify(transaction, never()).setex(eq(USER_ID_TO_SESSION.toString()), anyInt(), eq(DECRYPTED_SESSION_TOKEN));
verify(transaction, never()).exec();
}
Aggregations