Search in sources :

Example 1 with CriteriaContext

use of org.sagebionetworks.bridge.models.CriteriaContext in project BridgeServer2 by Sage-Bionetworks.

the class NotificationTopicServiceTest method manageCriteriaBasedSubscriptions.

@Test
public void manageCriteriaBasedSubscriptions() {
    // 2 criteria-based topics, 2 manual topics.
    when(mockTopicDao.listTopics(TEST_APP_ID, true)).thenReturn(ImmutableList.of(CRITERIA_TOPIC_1, CRITERIA_TOPIC_2, MANUAL_TOPIC_1, MANUAL_TOPIC_2));
    // 2 registrations.
    when(mockRegistrationDao.listRegistrations(HEALTH_CODE)).thenReturn(ImmutableList.of(PUSH_REGISTRATION, SMS_REGISTRATION));
    // Each registration is subscribed to criteria topic 1 and manual topic 1.
    when(mockSubscriptionDao.listSubscriptions(PUSH_REGISTRATION)).thenReturn((List) ImmutableList.of(getSub(CRITERIA_TOPIC_1.getGuid()), getSub(MANUAL_TOPIC_1.getGuid())));
    when(mockSubscriptionDao.listSubscriptions(SMS_REGISTRATION)).thenReturn((List) ImmutableList.of(getSub(CRITERIA_TOPIC_1.getGuid()), getSub(MANUAL_TOPIC_1.getGuid())));
    // Create criteria context with data group 2.
    CriteriaContext context = new CriteriaContext.Builder().withContext(EMPTY_CONTEXT).withUserDataGroups(ImmutableSet.of(CRITERIA_GROUP_2)).build();
    // Execute test.
    service.manageCriteriaBasedSubscriptions(TEST_APP_ID, context, HEALTH_CODE);
    // We un-sub from criteria topic 1 and sub to criteria topic 2.
    verify(mockSubscriptionDao).unsubscribe(PUSH_REGISTRATION, CRITERIA_TOPIC_1);
    verify(mockSubscriptionDao).unsubscribe(SMS_REGISTRATION, CRITERIA_TOPIC_1);
    verify(mockSubscriptionDao).subscribe(PUSH_REGISTRATION, CRITERIA_TOPIC_2);
    verify(mockSubscriptionDao).subscribe(SMS_REGISTRATION, CRITERIA_TOPIC_2);
    // We do not sub or unsub to any manual topics.
    verify(mockSubscriptionDao, never()).unsubscribe(any(), eq(MANUAL_TOPIC_1));
    verify(mockSubscriptionDao, never()).unsubscribe(any(), eq(MANUAL_TOPIC_2));
    verify(mockSubscriptionDao, never()).subscribe(any(), eq(MANUAL_TOPIC_1));
    verify(mockSubscriptionDao, never()).subscribe(any(), eq(MANUAL_TOPIC_2));
}
Also used : CriteriaContext(org.sagebionetworks.bridge.models.CriteriaContext) Test(org.testng.annotations.Test)

Example 2 with CriteriaContext

use of org.sagebionetworks.bridge.models.CriteriaContext in project BridgeServer2 by Sage-Bionetworks.

the class AuthenticationController method oauthSignIn.

@PostMapping("/v3/auth/oauth/signIn")
public JsonNode oauthSignIn() {
    OAuthAuthorizationToken token = parseJson(OAuthAuthorizationToken.class);
    App app = appService.getApp(token.getAppId());
    CriteriaContext context = getCriteriaContext(app.getIdentifier());
    UserSession session = null;
    try {
        session = authenticationService.oauthSignIn(context, token);
    } catch (ConsentRequiredException e) {
        session = e.getUserSession();
        throw e;
    } finally {
        updateRequestInfoFromSession(session);
    }
    return UserSessionInfo.toJSON(session);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) ConsentRequiredException(org.sagebionetworks.bridge.exceptions.ConsentRequiredException) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) OAuthAuthorizationToken(org.sagebionetworks.bridge.models.oauth.OAuthAuthorizationToken) CriteriaContext(org.sagebionetworks.bridge.models.CriteriaContext) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 3 with CriteriaContext

use of org.sagebionetworks.bridge.models.CriteriaContext in project BridgeServer2 by Sage-Bionetworks.

the class BaseController method getLanguages.

/**
 * Once we acquire a language for a user, we save it and use that language going forward. Changing their
 * language in the host operating system will not change the language they are using (since changing the
 * language might change their consent state). If they change their language by updating their UserProfile,
 * then they may have to reconsent in the new language they are using for the app. Any warnings to
 * that effect will need to be included in the application.
 */
List<String> getLanguages(UserSession session) {
    StudyParticipant participant = session.getParticipant();
    if (!participant.getLanguages().isEmpty()) {
        return participant.getLanguages();
    }
    RequestContext reqContext = RequestContext.get();
    List<String> languages = reqContext.getCallerLanguages();
    if (!languages.isEmpty()) {
        AccountId accountId = AccountId.forHealthCode(session.getAppId(), session.getHealthCode());
        accountService.editAccount(accountId, account -> account.setLanguages(languages));
        CriteriaContext newContext = new CriteriaContext.Builder().withLanguages(languages).withClientInfo(reqContext.getCallerClientInfo()).withHealthCode(session.getHealthCode()).withUserId(session.getId()).withUserDataGroups(session.getParticipant().getDataGroups()).withUserStudyIds(session.getParticipant().getStudyIds()).withAppId(session.getAppId()).build();
        sessionUpdateService.updateLanguage(session, newContext);
    }
    return languages;
}
Also used : AccountId(org.sagebionetworks.bridge.models.accounts.AccountId) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) RequestContext(org.sagebionetworks.bridge.RequestContext) CriteriaContext(org.sagebionetworks.bridge.models.CriteriaContext)

Example 4 with CriteriaContext

use of org.sagebionetworks.bridge.models.CriteriaContext in project BridgeServer2 by Sage-Bionetworks.

the class AuthenticationServiceTest method getSessionFromAccountWithoutReauthentication.

@Test
public void getSessionFromAccountWithoutReauthentication() {
    // Create inputs.
    App app = App.create();
    app.setReauthenticationEnabled(false);
    setIpAddress(IP_ADDRESS);
    CriteriaContext context = new CriteriaContext.Builder().withAppId(TEST_APP_ID).build();
    Account account = Account.create();
    // Mock pre-reqs.
    when(participantService.getParticipant(any(), any(Account.class), anyBoolean())).thenReturn(PARTICIPANT);
    when(config.getEnvironment()).thenReturn(Environment.LOCAL);
    when(consentService.getConsentStatuses(any(), any())).thenReturn(CONSENTED_STATUS_MAP);
    // Execute and validate.
    UserSession session = service.getSessionFromAccount(app, context, account);
    assertNull(session.getReauthToken());
    verify(service, never()).generateReauthToken();
    verify(accountSecretDao, never()).createSecret(any(), any(), any());
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Account(org.sagebionetworks.bridge.models.accounts.Account) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) CriteriaContext(org.sagebionetworks.bridge.models.CriteriaContext) Test(org.testng.annotations.Test)

Example 5 with CriteriaContext

use of org.sagebionetworks.bridge.models.CriteriaContext in project BridgeServer2 by Sage-Bionetworks.

the class AuthenticationServiceTest method getSessionFromAccount.

// Most of the other behaviors are tested in other methods. This test specifically tests the session created has
// the correct attributes.
@Test
public void getSessionFromAccount() {
    // Create inputs.
    App app = App.create();
    app.setIdentifier(TEST_APP_ID);
    app.setReauthenticationEnabled(true);
    setIpAddress(IP_ADDRESS);
    CriteriaContext context = new CriteriaContext.Builder().withAppId(TEST_APP_ID).build();
    Account account = Account.create();
    account.setId(TEST_USER_ID);
    StudyParticipant participant = new StudyParticipant.Builder().copyOf(PARTICIPANT).withOrgMembership(TEST_ORG_ID).build();
    // Mock pre-reqs.
    when(participantService.getParticipant(any(), any(Account.class), anyBoolean())).thenReturn(participant);
    when(config.getEnvironment()).thenReturn(Environment.LOCAL);
    when(consentService.getConsentStatuses(any(), any())).thenReturn(CONSENTED_STATUS_MAP);
    when(service.generateReauthToken()).thenReturn(REAUTH_TOKEN);
    when(sponsorService.getSponsoredStudyIds(TEST_APP_ID, TEST_ORG_ID)).thenReturn(USER_STUDY_IDS);
    // Execute and validate.
    UserSession session = service.getSessionFromAccount(app, context, account);
    assertSame(session.getParticipant(), participant);
    assertNotNull(session.getSessionToken());
    assertNotNull(session.getInternalSessionToken());
    assertTrue(session.isAuthenticated());
    assertEquals(session.getEnvironment(), Environment.LOCAL);
    assertEquals(session.getIpAddress(), IP_ADDRESS);
    assertEquals(session.getAppId(), TEST_APP_ID);
    assertEquals(session.getReauthToken(), REAUTH_TOKEN);
    assertEquals(session.getConsentStatuses(), CONSENTED_STATUS_MAP);
    verify(accountSecretDao).createSecret(AccountSecretType.REAUTH, TEST_USER_ID, REAUTH_TOKEN);
    RequestContext retValue = RequestContext.updateFromSession(session, sponsorService);
    assertEquals(retValue.getCallerAppId(), TEST_APP_ID);
    assertEquals(retValue.getOrgSponsoredStudies(), USER_STUDY_IDS);
    assertEquals(retValue.getCallerUserId(), TEST_USER_ID);
    assertEquals(retValue.getCallerOrgMembership(), TEST_ORG_ID);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Account(org.sagebionetworks.bridge.models.accounts.Account) UserSession(org.sagebionetworks.bridge.models.accounts.UserSession) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) RequestContext(org.sagebionetworks.bridge.RequestContext) CriteriaContext(org.sagebionetworks.bridge.models.CriteriaContext) Test(org.testng.annotations.Test)

Aggregations

CriteriaContext (org.sagebionetworks.bridge.models.CriteriaContext)69 Test (org.testng.annotations.Test)44 UserSession (org.sagebionetworks.bridge.models.accounts.UserSession)31 StudyParticipant (org.sagebionetworks.bridge.models.accounts.StudyParticipant)27 App (org.sagebionetworks.bridge.models.apps.App)23 PostMapping (org.springframework.web.bind.annotation.PostMapping)14 AppConfig (org.sagebionetworks.bridge.models.appconfig.AppConfig)12 RequestContext (org.sagebionetworks.bridge.RequestContext)11 SignIn (org.sagebionetworks.bridge.models.accounts.SignIn)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 ConsentRequiredException (org.sagebionetworks.bridge.exceptions.ConsentRequiredException)7 Account (org.sagebionetworks.bridge.models.accounts.Account)6 ConsentStatus (org.sagebionetworks.bridge.models.accounts.ConsentStatus)6 Subpopulation (org.sagebionetworks.bridge.models.subpopulations.Subpopulation)5 DynamoSubpopulation (org.sagebionetworks.bridge.dynamodb.DynamoSubpopulation)4 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)4 EntityNotFoundException (org.sagebionetworks.bridge.exceptions.EntityNotFoundException)4 RequestInfo (org.sagebionetworks.bridge.models.RequestInfo)4 Enrollment (org.sagebionetworks.bridge.models.studies.Enrollment)4 SubpopulationGuid (org.sagebionetworks.bridge.models.subpopulations.SubpopulationGuid)4