Search in sources :

Example 1 with UserSession

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);
    }
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 2 with UserSession

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);
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Example 3 with UserSession

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);
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) Test(org.testng.annotations.Test)

Example 4 with UserSession

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());
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) Test(org.testng.annotations.Test)

Example 5 with UserSession

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();
}
Also used : UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) Test(org.testng.annotations.Test)

Aggregations

UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)483 PostMapping (org.springframework.web.bind.annotation.PostMapping)149 GetMapping (org.springframework.web.bind.annotation.GetMapping)114 App (org.sagebionetworks.bridge.models.apps.App)104 Test (org.testng.annotations.Test)102 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)89 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)65 StatusMessage (org.sagebionetworks.bridge.models.StatusMessage)60 Account (org.sagebionetworks.bridge.models.accounts.Account)56 BeforeMethod (org.testng.annotations.BeforeMethod)47 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)45 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)43 UnauthorizedException (org.sagebionetworks.bridge.exceptions.UnauthorizedException)38 CriteriaContext (org.sagebionetworks.bridge.models.CriteriaContext)31 DateTime (org.joda.time.DateTime)27 AccountId (org.sagebionetworks.bridge.models.accounts.AccountId)27 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 ResourceList (org.sagebionetworks.bridge.models.ResourceList)21 SubpopulationGuid (org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid)21 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)18