Search in sources :

Example 6 with JSONObject

use of org.springframework.security.oauth2.common.json.JSONObject in project OpenClinica by OpenClinica.

the class RandomizeService method getRandomizationCode.

// Rest Call to OCUI to get Randomization
public String getRandomizationCode(EventCRFBean eventCrfBean, List<StratificationFactorBean> stratificationFactorBeans, RuleSetBean ruleSet) throws JSONException {
    StudySubjectDAO ssdao = new StudySubjectDAO<>(ds);
    StudySubjectBean ssBean = (StudySubjectBean) ssdao.findByPK(eventCrfBean.getStudySubjectId());
    // study subject oid
    String identifier = ssBean.getOid();
    StudyDAO sdao = new StudyDAO<>(ds);
    StudyBean sBean = (StudyBean) sdao.findByPK(ssBean.getStudyId());
    // site or study oid
    String siteIdentifier = sBean.getOid();
    // site or study name
    String name = sBean.getName();
    UserAccountDAO udao = new UserAccountDAO(ds);
    int userId = 0;
    if (eventCrfBean.getUpdaterId() == 0) {
        userId = eventCrfBean.getOwnerId();
    } else {
        userId = eventCrfBean.getUpdaterId();
    }
    UserAccountBean uBean = (UserAccountBean) udao.findByPK(userId);
    String user = uBean.getName();
    // sBean should be parent study
    // put randomization object in cache
    StudyBean study = getParentStudy(sBean.getOid());
    SeRandomizationDTO randomization = null;
    try {
        randomization = getCachedRandomizationDTOObject(study.getOid(), false);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String randomiseUrl = randomization.getUrl();
    String username = randomization.getUsername();
    String password = randomization.getPassword();
    String timezone = "America/New_York";
    // String randomiseUrl = "https://evaluation.sealedenvelope.com/redpill/seti2";
    // String username = "oc";
    // String password = "secret";
    HttpHeaders headers = createHeaders(username, password);
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    // retrieve json object if Randomization exist ,otherwise return a null object
    JSONObject jsonRandObject = retrieveARandomisation(randomiseUrl, ssBean, headers);
    if (jsonRandObject != null) {
        return (String) jsonRandObject.get("code");
    } else {
        // if Site identifier exists ,then update otherwise create new Site identifier
        addOrUpdateASite(randomiseUrl, sBean, headers, timezone);
        // send for Randomization
        JSONObject jsonRandomisedObject = randomiseSubject(randomiseUrl, ssBean, sBean, headers, user, stratificationFactorBeans, eventCrfBean, ruleSet);
        if (jsonRandomisedObject != null)
            return (String) jsonRandomisedObject.get("code");
        else
            return "";
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) JSONObject(org.springframework.security.oauth2.common.json.JSONObject) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) SeRandomizationDTO(org.akaza.openclinica.service.pmanage.SeRandomizationDTO) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) UserAccountDAO(org.akaza.openclinica.dao.login.UserAccountDAO) JSONException(org.springframework.security.oauth2.common.json.JSONException)

Example 7 with JSONObject

use of org.springframework.security.oauth2.common.json.JSONObject in project spring-security by spring-projects.

the class JwtIssuerReactiveAuthenticationManagerResolverTests method resolveWhenUsingTrustedIssuerThenReturnsAuthenticationManager.

@Test
public void resolveWhenUsingTrustedIssuerThenReturnsAuthenticationManager() throws Exception {
    try (MockWebServer server = new MockWebServer()) {
        String issuer = server.url("").toString();
        server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json").setBody(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)));
        server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json").setBody(JWK_SET));
        server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json").setBody(JWK_SET));
        JWSObject jws = new JWSObject(new JWSHeader(JWSAlgorithm.RS256), new Payload(new JSONObject(Collections.singletonMap(JwtClaimNames.ISS, issuer))));
        jws.sign(new RSASSASigner(TestKeys.DEFAULT_PRIVATE_KEY));
        JwtIssuerReactiveAuthenticationManagerResolver authenticationManagerResolver = new JwtIssuerReactiveAuthenticationManagerResolver(issuer);
        ReactiveAuthenticationManager authenticationManager = authenticationManagerResolver.resolve(null).block();
        assertThat(authenticationManager).isNotNull();
        BearerTokenAuthenticationToken token = withBearerToken(jws.serialize());
        Authentication authentication = authenticationManager.authenticate(token).block();
        assertThat(authentication).isNotNull();
        assertThat(authentication.isAuthenticated()).isTrue();
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ReactiveAuthenticationManager(org.springframework.security.authentication.ReactiveAuthenticationManager) JSONObject(net.minidev.json.JSONObject) Authentication(org.springframework.security.core.Authentication) MockWebServer(okhttp3.mockwebserver.MockWebServer) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) Payload(com.nimbusds.jose.Payload) JWSObject(com.nimbusds.jose.JWSObject) BearerTokenAuthenticationToken(org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken) JWSHeader(com.nimbusds.jose.JWSHeader) Test(org.junit.jupiter.api.Test)

Example 8 with JSONObject

use of org.springframework.security.oauth2.common.json.JSONObject in project spring-security by spring-projects.

the class NimbusOpaqueTokenIntrospectorTests method introspectWhenActiveTokenThenParsesValuesInResponse.

@Test
public void introspectWhenActiveTokenThenParsesValuesInResponse() {
    Map<String, Object> introspectedValues = new HashMap<>();
    introspectedValues.put(OAuth2TokenIntrospectionClaimNames.ACTIVE, true);
    introspectedValues.put(OAuth2TokenIntrospectionClaimNames.AUD, Arrays.asList("aud"));
    introspectedValues.put(OAuth2TokenIntrospectionClaimNames.NBF, 29348723984L);
    RestOperations restOperations = mock(RestOperations.class);
    OpaqueTokenIntrospector introspectionClient = new NimbusOpaqueTokenIntrospector(INTROSPECTION_URL, restOperations);
    given(restOperations.exchange(any(RequestEntity.class), eq(String.class))).willReturn(response(new JSONObject(introspectedValues).toJSONString()));
    OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token");
    // @formatter:off
    assertThat(authority.getAttributes()).isNotNull().containsEntry(OAuth2TokenIntrospectionClaimNames.ACTIVE, true).containsEntry(OAuth2TokenIntrospectionClaimNames.AUD, Arrays.asList("aud")).containsEntry(OAuth2TokenIntrospectionClaimNames.NBF, Instant.ofEpochSecond(29348723984L)).doesNotContainKey(OAuth2TokenIntrospectionClaimNames.CLIENT_ID).doesNotContainKey(OAuth2TokenIntrospectionClaimNames.SCOPE);
// @formatter:on
}
Also used : JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) JSONObject(net.minidev.json.JSONObject) RestOperations(org.springframework.web.client.RestOperations) RequestEntity(org.springframework.http.RequestEntity) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with JSONObject

use of org.springframework.security.oauth2.common.json.JSONObject in project spring-security by spring-projects.

the class BearerTokenAuthenticationTests method constructorWhenDefaultParametersThenSetsPrincipalToAttributesCopy.

// gh-6843
@Test
public void constructorWhenDefaultParametersThenSetsPrincipalToAttributesCopy() {
    JSONObject attributes = new JSONObject();
    attributes.put("active", true);
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(attributes, null);
    BearerTokenAuthentication token = new BearerTokenAuthentication(principal, this.token, null);
    assertThat(token.getPrincipal()).isNotSameAs(attributes);
    assertThat(token.getTokenAttributes()).isNotSameAs(attributes);
}
Also used : JSONObject(net.minidev.json.JSONObject) OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) Test(org.junit.jupiter.api.Test)

Example 10 with JSONObject

use of org.springframework.security.oauth2.common.json.JSONObject in project spring-security by spring-projects.

the class BearerTokenAuthenticationTests method toStringWhenAttributesContainsURLThenDoesNotFail.

// gh-6843
@Test
public void toStringWhenAttributesContainsURLThenDoesNotFail() throws Exception {
    JSONObject attributes = new JSONObject(Collections.singletonMap("iss", new URL("https://idp.example.com")));
    OAuth2AuthenticatedPrincipal principal = new DefaultOAuth2AuthenticatedPrincipal(attributes, null);
    BearerTokenAuthentication token = new BearerTokenAuthentication(principal, this.token, null);
    token.toString();
}
Also used : JSONObject(net.minidev.json.JSONObject) OAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) DefaultOAuth2AuthenticatedPrincipal(org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal) URL(java.net.URL) Test(org.junit.jupiter.api.Test)

Aggregations

JSONObject (net.minidev.json.JSONObject)5 Test (org.junit.jupiter.api.Test)5 OAuth2AuthenticatedPrincipal (org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal)4 JSONException (org.springframework.security.oauth2.common.json.JSONException)3 JSONObject (org.springframework.security.oauth2.common.json.JSONObject)3 JSONObject (com.alibaba.fastjson.JSONObject)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 HttpEntity (org.springframework.http.HttpEntity)2 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)2 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)2 DefaultOAuth2AuthenticatedPrincipal (org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal)2 RestTemplate (org.springframework.web.client.RestTemplate)2 JWSHeader (com.nimbusds.jose.JWSHeader)1 JWSObject (com.nimbusds.jose.JWSObject)1 Payload (com.nimbusds.jose.Payload)1 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)1 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1