Search in sources :

Example 41 with JSONObject

use of net.minidev.json.JSONObject in project spring-security by spring-projects.

the class JwtIssuerReactiveAuthenticationManagerResolverTests method resolveWhednUsingTrustedIssuerThenReturnsAuthenticationManager.

// gh-10444
@Test
public void resolveWhednUsingTrustedIssuerThenReturnsAuthenticationManager() throws Exception {
    try (MockWebServer server = new MockWebServer()) {
        String issuer = server.url("").toString();
        // @formatter:off
        server.enqueue(new MockResponse().setResponseCode(500).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(String.format(DEFAULT_RESPONSE_TEMPLATE, issuer, issuer)));
        server.enqueue(new MockResponse().setResponseCode(200).setHeader("Content-Type", "application/json").setBody(JWK_SET));
        // @formatter:on
        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();
        Authentication token = withBearerToken(jws.serialize());
        assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> authenticationManager.authenticate(token).block());
        Authentication authentication = authenticationManager.authenticate(token).block();
        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) JWSHeader(com.nimbusds.jose.JWSHeader) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Example 42 with JSONObject

use of net.minidev.json.JSONObject in project spring-security by spring-projects.

the class NimbusReactiveOpaqueTokenIntrospectorTests method authenticateWhenActiveTokenThenParsesValuesInResponse.

@Test
public void authenticateWhenActiveTokenThenParsesValuesInResponse() {
    Map<String, Object> introspectedValues = new HashMap<>();
    introspectedValues.put(OAuth2TokenIntrospectionClaimNames.ACTIVE, true);
    introspectedValues.put(OAuth2TokenIntrospectionClaimNames.AUD, Arrays.asList("aud"));
    introspectedValues.put(OAuth2TokenIntrospectionClaimNames.NBF, 29348723984L);
    WebClient webClient = mockResponse(new JSONObject(introspectedValues).toJSONString());
    NimbusReactiveOpaqueTokenIntrospector introspectionClient = new NimbusReactiveOpaqueTokenIntrospector(INTROSPECTION_URL, webClient);
    OAuth2AuthenticatedPrincipal authority = introspectionClient.introspect("token").block();
    // @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) WebClient(org.springframework.web.reactive.function.client.WebClient) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 43 with JSONObject

use of net.minidev.json.JSONObject in project skype-bot by toomasr.

the class BitBucketHookHandler method handle.

@Override
public Map<String, String[]> handle(Map<String, String[]> parameters, String requestBody) {
    try {
        requestBody = URLDecoder.decode(requestBody, "UTF-8");
        requestBody = requestBody.replaceFirst("payload=", "");
        JSONObject object = (JSONObject) new JSONParser(JSONParser.MODE_PERMISSIVE).parse(requestBody);
        JSONObject repo = (JSONObject) object.get("repository");
        // String repoName = (String) repo.get("name");
        String absoluteUrl = (String) repo.get("absolute_url");
        String repoUrl = "https://bitbucket.org" + absoluteUrl;
        List<String> messages = new ArrayList<String>();
        JSONArray commits = (JSONArray) object.get("commits");
        for (int i = 0; i < commits.size(); i++) {
            JSONObject commit = (JSONObject) commits.get(i);
            String rawAuthor = (String) commit.get("raw_author");
            rawAuthor = rawAuthor.replaceFirst(" <.*>", "");
            String message = (String) commit.get("message");
            String branch = (String) commit.get("branch");
            messages.add(String.format("%s commited '%s' to %s (branch '%s')", rawAuthor, message, repoUrl, branch));
        }
        Map<String, String[]> replies = new HashMap<String, String[]>();
        replies.put(chatName, messages.toArray(new String[0]));
        return replies;
    } catch (Exception e) {
        throw new RuntimeException("Cannot parse json: " + requestBody, e);
    }
}
Also used : JSONObject(net.minidev.json.JSONObject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONParser(net.minidev.json.parser.JSONParser)

Example 44 with JSONObject

use of net.minidev.json.JSONObject in project skype-bot by toomasr.

the class Weather method getKelvinsTemp.

private Double getKelvinsTemp(String cityId) throws Exception, ParseException {
    String url = baseUrl.replaceFirst("CITY_ID", cityId);
    String json = URLConnectionReader.getText(url);
    JSONParser p = new JSONParser(JSONParser.MODE_PERMISSIVE);
    JSONObject o1 = (JSONObject) p.parse(json);
    o1 = (JSONObject) o1.get("main");
    Number kelvins = (Number) o1.get("temp");
    return kelvins.doubleValue();
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONParser(net.minidev.json.parser.JSONParser)

Example 45 with JSONObject

use of net.minidev.json.JSONObject in project tomee by apache.

the class TokenUtils method generateJWTString.

public static String generateJWTString(String jsonResource) throws Exception {
    byte[] byteBuffer = new byte[16384];
    currentThread().getContextClassLoader().getResource(jsonResource).openStream().read(byteBuffer);
    JSONParser parser = new JSONParser(DEFAULT_PERMISSIVE_MODE);
    JSONObject jwtJson = (JSONObject) parser.parse(byteBuffer);
    long currentTimeInSecs = (System.currentTimeMillis() / 1000);
    long expirationTime = currentTimeInSecs + 1000;
    jwtJson.put(Claims.iat.name(), currentTimeInSecs);
    jwtJson.put(Claims.auth_time.name(), currentTimeInSecs);
    jwtJson.put(Claims.exp.name(), expirationTime);
    SignedJWT signedJWT = new SignedJWT(new JWSHeader.Builder(RS256).keyID("/privateKey.pem").type(JWT).build(), parse(jwtJson));
    signedJWT.sign(new RSASSASigner(readPrivateKey("privateKey.pem")));
    return signedJWT.serialize();
}
Also used : JSONObject(net.minidev.json.JSONObject) RSASSASigner(com.nimbusds.jose.crypto.RSASSASigner) JSONParser(net.minidev.json.parser.JSONParser) SignedJWT(com.nimbusds.jwt.SignedJWT)

Aggregations

JSONObject (net.minidev.json.JSONObject)254 JSONArray (net.minidev.json.JSONArray)49 Test (org.junit.Test)39 Test (org.testng.annotations.Test)38 JSONParser (net.minidev.json.parser.JSONParser)23 HashMap (java.util.HashMap)22 MockFlowFile (org.apache.nifi.util.MockFlowFile)16 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)14 Test (org.junit.jupiter.api.Test)14 Map (java.util.Map)13 ParseException (net.minidev.json.parser.ParseException)13 JSONConverterException (org.btrplace.json.JSONConverterException)13 SignedJWT (com.nimbusds.jwt.SignedJWT)12 Node (org.btrplace.model.Node)12 VM (org.btrplace.model.VM)12 JWSHeader (com.nimbusds.jose.JWSHeader)10 RSASSASigner (com.nimbusds.jose.crypto.RSASSASigner)10 AuthenticationContext (org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext)10 OpenAPI (io.swagger.v3.oas.models.OpenAPI)9 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)9