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();
}
}
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
}
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);
}
}
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();
}
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();
}
Aggregations