Search in sources :

Example 1 with Fido2DeviceResource

use of io.jans.scim.model.scim2.fido.Fido2DeviceResource in project jans by JanssenProject.

the class BulkWebService method execute.

private Pair<Response, String> execute(Verb verb, BaseScimWebService ws, String data, String fragment) {
    Response response = null;
    String idCreated = null;
    try {
        if (ws == userWS)
            switch(verb) {
                case PUT:
                    UserResource user = mapper.readValue(data, UserResource.class);
                    response = userWS.updateUser(user, fragment, "id", null);
                    break;
                case DELETE:
                    response = userWS.deleteUser(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = userWS.patchUser(pr, fragment, "id", null);
                    break;
                case POST:
                    user = mapper.readValue(data, UserResource.class);
                    response = userWS.createUser(user, "id", null);
                    if (CREATED.getStatusCode() == response.getStatus()) {
                        user = mapper.readValue(response.getEntity().toString(), UserResource.class);
                        idCreated = user.getId();
                    }
                    break;
            }
        else if (ws == groupWS)
            switch(verb) {
                case PUT:
                    GroupResource group = mapper.readValue(data, GroupResource.class);
                    response = groupWS.updateGroup(group, fragment, "id", null);
                    break;
                case DELETE:
                    response = groupWS.deleteGroup(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = groupWS.patchGroup(pr, fragment, "id", null);
                    break;
                case POST:
                    group = mapper.readValue(data, GroupResource.class);
                    response = groupWS.createGroup(group, "id", null);
                    if (CREATED.getStatusCode() == response.getStatus()) {
                        group = mapper.readValue(response.getEntity().toString(), GroupResource.class);
                        idCreated = group.getId();
                    }
                    break;
            }
        else if (ws == fidoDeviceWS)
            switch(verb) {
                case PUT:
                    FidoDeviceResource dev = mapper.readValue(data, FidoDeviceResource.class);
                    response = fidoDeviceWS.updateDevice(dev, fragment, "id", null);
                    break;
                case DELETE:
                    response = fidoDeviceWS.deleteDevice(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = fidoDeviceWS.patchDevice(pr, fragment, "id", null);
                    break;
                case POST:
                    response = fidoDeviceWS.createDevice();
                    break;
            }
        else if (ws == fido2DeviceWS)
            switch(verb) {
                case PUT:
                    Fido2DeviceResource dev = mapper.readValue(data, Fido2DeviceResource.class);
                    response = fido2DeviceWS.updateF2Device(dev, fragment, "id", null);
                    break;
                case DELETE:
                    response = fido2DeviceWS.deleteF2Device(fragment);
                    break;
                case PATCH:
                    PatchRequest pr = mapper.readValue(data, PatchRequest.class);
                    response = fido2DeviceWS.patchF2Device(pr, fragment, "id", null);
                    break;
                case POST:
                    response = fido2DeviceWS.createDevice();
                    break;
            }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return new Pair<>(response, idCreated);
}
Also used : BulkResponse(io.jans.scim.model.scim2.bulk.BulkResponse) Response(javax.ws.rs.core.Response) Fido2DeviceResource(io.jans.scim.model.scim2.fido.Fido2DeviceResource) FidoDeviceResource(io.jans.scim.model.scim2.fido.FidoDeviceResource) UserResource(io.jans.scim.model.scim2.user.UserResource) PatchRequest(io.jans.scim.model.scim2.patch.PatchRequest) GroupResource(io.jans.scim.model.scim2.group.GroupResource) Pair(io.jans.util.Pair)

Example 2 with Fido2DeviceResource

use of io.jans.scim.model.scim2.fido.Fido2DeviceResource in project jans by JanssenProject.

the class Fido2DeviceWebService method updateF2Device.

@Path("{id}")
@PUT
@Consumes({ MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ MEDIA_TYPE_SCIM_JSON + UTF8_CHARSET_FRAGMENT, MediaType.APPLICATION_JSON + UTF8_CHARSET_FRAGMENT })
@HeaderParam("Accept")
@DefaultValue(MEDIA_TYPE_SCIM_JSON)
@ProtectedApi(scopes = { "https://jans.io/scim/fido2.write" })
@RefAdjusted
public Response updateF2Device(Fido2DeviceResource fidoDeviceResource, @PathParam("id") String id, @QueryParam(QUERY_PARAM_ATTRIBUTES) String attrsList, @QueryParam(QUERY_PARAM_EXCLUDED_ATTRS) String excludedAttrsList) {
    Response response;
    try {
        log.debug("Executing web service method. updateDevice");
        // remove externalId, no place to store it in LDAP
        fidoDeviceResource.setExternalId(null);
        if (fidoDeviceResource.getId() != null && !fidoDeviceResource.getId().equals(id))
            throw new SCIMException("Parameter id does not match id attribute of Device");
        String userId = fidoDeviceResource.getUserId();
        GluuFido2Device device = fidoDeviceService.getFido2DeviceById(userId, id);
        if (device == null)
            return notFoundResponse(id, fido2ResourceType);
        response = externalConstraintsService.applyEntityCheck(device, fidoDeviceResource, httpHeaders, uriInfo, HttpMethod.PUT, fido2ResourceType);
        if (response != null)
            return response;
        executeValidation(fidoDeviceResource, true);
        Fido2DeviceResource updatedResource = new Fido2DeviceResource();
        transferAttributesToFido2Resource(device, updatedResource, endpointUrl, userId);
        updatedResource.getMeta().setLastModified(DateUtil.millisToISOString(System.currentTimeMillis()));
        updatedResource = (Fido2DeviceResource) ScimResourceUtil.transferToResourceReplace(fidoDeviceResource, updatedResource, extService.getResourceExtensions(updatedResource.getClass()));
        transferAttributesToDevice(updatedResource, device);
        fidoDeviceService.updateFido2Device(device);
        String json = resourceSerializer.serialize(updatedResource, attrsList, excludedAttrsList);
        response = Response.ok(new URI(updatedResource.getMeta().getLocation())).entity(json).build();
    } catch (SCIMException e) {
        log.error("Validation check error: {}", e.getMessage());
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.INVALID_VALUE, e.getMessage());
    } catch (InvalidAttributeValueException e) {
        log.error(e.getMessage());
        response = getErrorResponse(Response.Status.BAD_REQUEST, ErrorScimType.MUTABILITY, e.getMessage());
    } catch (Exception e) {
        log.error("Failure at updateDevice method", e);
        response = getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, "Unexpected error: " + e.getMessage());
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) Fido2DeviceResource(io.jans.scim.model.scim2.fido.Fido2DeviceResource) SCIMException(io.jans.scim.model.exception.SCIMException) URI(java.net.URI) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) GluuFido2Device(io.jans.scim.model.GluuFido2Device) URISyntaxException(java.net.URISyntaxException) SCIMException(io.jans.scim.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) RefAdjusted(io.jans.scim.service.scim2.interceptor.RefAdjusted) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ProtectedApi(io.jans.scim.service.filter.ProtectedApi) PUT(javax.ws.rs.PUT)

Example 3 with Fido2DeviceResource

use of io.jans.scim.model.scim2.fido.Fido2DeviceResource in project jans by JanssenProject.

the class Fido2DeviceTest method search.

@Test
public void search() {
    logger.debug("Searching all fido 2 devices");
    Response response = client.searchF2Devices(null, "id pr", null, null, null, null, null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    ListResponse listResponse = response.readEntity(ListResponse.class);
    // Work upon the first device of the list only
    device = (Fido2DeviceResource) listResponse.getResources().get(0);
    assertNotNull(device);
    logger.debug("First device {} picked", device.getId());
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) ListResponse(io.jans.scim.model.scim2.ListResponse) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 4 with Fido2DeviceResource

use of io.jans.scim.model.scim2.fido.Fido2DeviceResource in project jans by JanssenProject.

the class Fido2DeviceTest method updateWithObject.

@Test(dependsOnMethods = "updateWithJson")
public void updateWithObject() throws Exception {
    logger.debug("Updating device to original attributes");
    Response response = client.updateF2Device(device, device.getId(), null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    Fido2DeviceResource updated = response.readEntity(fido2Class);
    // Naively compare (property-to-property) the original and new object. It's feasible since all of them are strings
    for (String path : IntrospectUtil.allAttrs.get(fido2Class)) {
        String val = BeanUtils.getProperty(device, path);
        // Exclude metas since they diverge and skip if original attribute was null (when passing null for an update, server ignores)
        if (!path.startsWith("meta") && val != null) {
            assertEquals(BeanUtils.getProperty(updated, path), val);
        }
    }
    // Update an immutable attribute
    updated.setCounter(Integer.MIN_VALUE);
    response = client.updateF2Device(updated, updated.getId(), null, null);
    assertEquals(response.getStatus(), BAD_REQUEST.getStatusCode());
}
Also used : Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) Fido2DeviceResource(io.jans.scim.model.scim2.fido.Fido2DeviceResource) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Example 5 with Fido2DeviceResource

use of io.jans.scim.model.scim2.fido.Fido2DeviceResource in project jans by JanssenProject.

the class Fido2DeviceTest method updateWithJson.

@Test(dependsOnMethods = "retrieve")
public void updateWithJson() throws Exception {
    // shallow clone device
    Fido2DeviceResource clone = (Fido2DeviceResource) BeanUtils.cloneBean(device);
    String name = "The quick brown fox jumps over the lazy dog";
    clone.setDisplayName(name);
    String json = mapper.writeValueAsString(clone);
    logger.debug("Updating device with json");
    Response response = client.updateF2Device(json, device.getId(), null, null);
    assertEquals(response.getStatus(), OK.getStatusCode());
    Fido2DeviceResource updated = response.readEntity(fido2Class);
    assertNotEquals(updated.getDisplayName(), device.getDisplayName());
    assertEquals(updated.getDisplayName(), name);
}
Also used : Fido2DeviceResource(io.jans.scim.model.scim2.fido.Fido2DeviceResource) Response(javax.ws.rs.core.Response) ListResponse(io.jans.scim.model.scim2.ListResponse) Test(org.testng.annotations.Test) BaseTest(io.jans.scim2.client.BaseTest)

Aggregations

Fido2DeviceResource (io.jans.scim.model.scim2.fido.Fido2DeviceResource)7 Response (javax.ws.rs.core.Response)7 ListResponse (io.jans.scim.model.scim2.ListResponse)4 BaseTest (io.jans.scim2.client.BaseTest)4 Test (org.testng.annotations.Test)4 GluuFido2Device (io.jans.scim.model.GluuFido2Device)3 SCIMException (io.jans.scim.model.exception.SCIMException)3 URISyntaxException (java.net.URISyntaxException)3 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)3 ProtectedApi (io.jans.scim.service.filter.ProtectedApi)2 RefAdjusted (io.jans.scim.service.scim2.interceptor.RefAdjusted)2 URI (java.net.URI)2 DefaultValue (javax.ws.rs.DefaultValue)2 HeaderParam (javax.ws.rs.HeaderParam)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 PagedResult (io.jans.orm.model.PagedResult)1 Filter (io.jans.orm.search.filter.Filter)1 BulkResponse (io.jans.scim.model.scim2.bulk.BulkResponse)1 FidoDeviceResource (io.jans.scim.model.scim2.fido.FidoDeviceResource)1