Search in sources :

Example 16 with JSONObject

use of com.nimbusds.jose.shaded.json.JSONObject in project platform by elveahuang.

the class JwtUtils method createJwsToken.

// -----------------------------------------------------------------------------------------------------------------
// JWS
// -----------------------------------------------------------------------------------------------------------------
public static String createJwsToken(byte[] secretKey, Map<String, Object> payloadMap) throws JOSEException {
    JWSHeader header = new JWSHeader(DEFAULT_JWT_ALGORITHM);
    JWSSigner signer = new MACSigner(secretKey);
    Payload payload = new Payload(new JSONObject(payloadMap));
    JWSObject object = new JWSObject(header, payload);
    object.sign(signer);
    return object.serialize();
}
Also used : MACSigner(com.nimbusds.jose.crypto.MACSigner) JSONObject(com.nimbusds.jose.shaded.json.JSONObject)

Example 17 with JSONObject

use of com.nimbusds.jose.shaded.json.JSONObject in project kf-key-management by kids-first.

the class FenceResource method getAuthClient.

@GetMapping("/{fence}/authenticated")
public Mono<ResponseEntity<JSONObject>> getAuthClient(@PathVariable("fence") String fenceKey, JwtAuthenticationToken authentication) throws IllegalArgumentException {
    val userId = authentication.getTokenAttributes().get("sub").toString();
    val fence = fenceService.getFence(fenceKey);
    val defaultResponse = new JSONObject();
    defaultResponse.put("authenticated", false);
    Mono<Optional<Long>> refreshExpiration = secretService.getSecret(fence.keyRefreshToken(), userId).filter(Secret::notExpired).map(Secret::getExpiration).map(Optional::of).defaultIfEmpty(Optional.empty());
    Mono<Optional<Long>> accessExpiration = secretService.getSecret(fence.keyAccessToken(), userId).filter(Secret::notExpired).map(Secret::getExpiration).map(Optional::of).defaultIfEmpty(Optional.empty());
    Mono<Long> expiration = Mono.zip(refreshExpiration, accessExpiration).flatMap(t -> {
        val refreshOpt = t.getT1();
        val accessOpt = t.getT2();
        if (refreshOpt.isPresent() && accessOpt.isPresent()) {
            val exp = accessOpt.get().compareTo(refreshOpt.get()) > 0 ? accessOpt : refreshOpt;
            return Mono.just(exp.get());
        } else
            return refreshOpt.map(Mono::just).orElseGet(() -> accessOpt.map(Mono::just).orElseGet(Mono::empty));
    });
    return expiration.map(e -> {
        val body = new JSONObject();
        body.put("authenticated", true);
        body.put("expiration", e);
        return ResponseEntity.ok(body);
    }).defaultIfEmpty(ResponseEntity.ok(defaultResponse));
}
Also used : lombok.val(lombok.val) Secret(io.kidsfirst.core.model.Secret) JwtAuthenticationToken(org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) JSONObject(com.nimbusds.jose.shaded.json.JSONObject) URIBuilder(org.apache.http.client.utils.URIBuilder) URISyntaxException(java.net.URISyntaxException) SecretService(io.kidsfirst.core.service.SecretService) lombok.val(lombok.val) Mono(reactor.core.publisher.Mono) Secret(io.kidsfirst.core.model.Secret) FenceService(io.kidsfirst.core.service.FenceService) Flux(reactor.core.publisher.Flux) Slf4j(lombok.extern.slf4j.Slf4j) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) JSONObject(com.nimbusds.jose.shaded.json.JSONObject) Optional(java.util.Optional) Mono(reactor.core.publisher.Mono)

Example 18 with JSONObject

use of com.nimbusds.jose.shaded.json.JSONObject in project kf-key-management by kids-first.

the class CavaticaTests method testCavaticaPostWithoutToken.

@Test
void testCavaticaPostWithoutToken() {
    JSONObject content = new JSONObject();
    content.put("path", "/user");
    content.put("method", "GET");
    JSONObject body = new JSONObject();
    body.put("key1", "value1");
    body.put("key2", "value2");
    content.put("body", body);
    webClient.post().uri(cavaticaURI).bodyValue(content.toJSONString()).accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isUnauthorized();
}
Also used : JSONObject(com.nimbusds.jose.shaded.json.JSONObject) Test(org.junit.jupiter.api.Test)

Example 19 with JSONObject

use of com.nimbusds.jose.shaded.json.JSONObject in project kf-key-management by kids-first.

the class CavaticaTests method testCavaticaUnsupported.

@Test
void testCavaticaUnsupported() {
    JSONObject content = new JSONObject();
    content.put("path", "/user");
    content.put("method", "UNSUPORTED");
    cavaticaWM.stubFor(get("/user").willReturn(ok(cavaticaResponseBody)));
    webClient.post().uri("/cavatica").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + defaultAccessToken).bodyValue(content.toJSONString()).exchange().expectStatus().isEqualTo(400);
}
Also used : JSONObject(com.nimbusds.jose.shaded.json.JSONObject) Test(org.junit.jupiter.api.Test)

Example 20 with JSONObject

use of com.nimbusds.jose.shaded.json.JSONObject in project kf-key-management by kids-first.

the class CavaticaTests method testCavaticaPostWithBody.

@Test
void testCavaticaPostWithBody() {
    JSONObject content = new JSONObject();
    content.put("path", "/user");
    content.put("method", "GET");
    JSONObject body = new JSONObject();
    body.put("key1", "value1");
    body.put("key2", "value2");
    content.put("body", body);
    cavaticaWM.stubFor(get("/user").willReturn(ok(cavaticaResponseBody)));
    webClient.post().uri("/cavatica").contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + defaultAccessToken).bodyValue(content.toJSONString()).exchange().expectStatus().isOk().expectBody().json(cavaticaResponseBody);
}
Also used : JSONObject(com.nimbusds.jose.shaded.json.JSONObject) Test(org.junit.jupiter.api.Test)

Aggregations

JSONObject (com.nimbusds.jose.shaded.json.JSONObject)52 lombok.val (lombok.val)22 Test (org.junit.jupiter.api.Test)21 Secret (io.kidsfirst.core.model.Secret)10 JSONArray (com.nimbusds.jose.shaded.json.JSONArray)9 Map (java.util.Map)5 Slf4j (lombok.extern.slf4j.Slf4j)5 JWSObject (com.nimbusds.jose.JWSObject)4 FenceService (io.kidsfirst.core.service.FenceService)4 SecretService (io.kidsfirst.core.service.SecretService)4 ResponseEntity (org.springframework.http.ResponseEntity)4 JwtAuthenticationToken (org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken)4 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)4 IOException (java.io.IOException)3 ECPublicKey (java.security.interfaces.ECPublicKey)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 JWTClaimsSet (com.nimbusds.jwt.JWTClaimsSet)2 URI (java.net.URI)2