ilgrosso commented on a change in pull request #189: URL: https://github.com/apache/syncope/pull/189#discussion_r430993966
########## File path: common/am/lib/src/main/java/org/apache/syncope/common/lib/to/AuthProfileTO.java ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.common.lib.to; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.syncope.common.lib.BaseBean; +import org.apache.syncope.common.lib.types.GoogleMfaAuthToken; + +import javax.ws.rs.PathParam; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@XmlRootElement(name = "authProfile") +@XmlType +public class AuthProfileTO extends BaseBean implements EntityTO { + + private static final long serialVersionUID = -6543425997956703057L; + + private final List<GoogleMfaAuthToken> googleMfaAuthTokens = new ArrayList<>(); + + private String key; + + private String owner; + + @Override + public String getKey() { + return key; + } + + @PathParam("key") + @Override + public void setKey(final String key) { + this.key = key; + } + + public String getOwner() { + return owner; + } + + public void setOwner(final String owner) { + this.owner = owner; + } + + @XmlElementWrapper(name = "googleMfaAuthTokens") + @XmlElement(name = "googleMfaAuthTokens") + @JsonProperty("googleMfaAuthTokens") + public List<GoogleMfaAuthToken> getGoogleMfaAuthTokens() { + return googleMfaAuthTokens; + } + + public boolean add(final GoogleMfaAuthToken item) { Review comment: This method is not needed, please remove. ########## File path: common/am/lib/src/main/java/org/apache/syncope/common/lib/to/AuthProfileTO.java ########## @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.common.lib.to; + +import com.fasterxml.jackson.annotation.JsonProperty; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.syncope.common.lib.BaseBean; +import org.apache.syncope.common.lib.types.GoogleMfaAuthToken; + +import javax.ws.rs.PathParam; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +@XmlRootElement(name = "authProfile") +@XmlType +public class AuthProfileTO extends BaseBean implements EntityTO { + + private static final long serialVersionUID = -6543425997956703057L; + + private final List<GoogleMfaAuthToken> googleMfaAuthTokens = new ArrayList<>(); + + private String key; + + private String owner; + + @Override + public String getKey() { + return key; + } + + @PathParam("key") + @Override + public void setKey(final String key) { + this.key = key; + } + + public String getOwner() { + return owner; + } + + public void setOwner(final String owner) { + this.owner = owner; + } + + @XmlElementWrapper(name = "googleMfaAuthTokens") + @XmlElement(name = "googleMfaAuthTokens") + @JsonProperty("googleMfaAuthTokens") + public List<GoogleMfaAuthToken> getGoogleMfaAuthTokens() { + return googleMfaAuthTokens; + } + + public boolean add(final GoogleMfaAuthToken item) { + return Optional.ofNullable(item). + filter(itemTO -> googleMfaAuthTokens.contains(itemTO) || googleMfaAuthTokens.add(itemTO)). + isPresent(); + } + + public boolean remove(final GoogleMfaAuthToken item) { Review comment: This method is not needed, please remove. ########## File path: common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AuthProfileService.java ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.common.rest.api.service; + +import io.swagger.v3.oas.annotations.headers.Header; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.security.SecurityRequirements; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.apache.syncope.common.lib.to.AuthProfileTO; +import org.apache.syncope.common.rest.api.RESTHeaders; + +import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import java.util.List; + +/** + * REST operations for SAML 2.0 SP metadata. + */ +@Tag(name = "Auth Profiles") +@SecurityRequirements({ + @SecurityRequirement(name = "BasicAuthentication"), + @SecurityRequirement(name = "Bearer")}) +@Path("authProfiles") +public interface AuthProfileService extends JAXRSService { + + @GET + @Produces({MediaType.APPLICATION_JSON}) + List<AuthProfileTO> list(); + + @GET + @Path("owners/{owner}") + @Consumes({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + AuthProfileTO findByOwner(@NotNull @PathParam("owner") String owner); Review comment: As far as I can see from `JPAAuthProfile` there could be different profiles for a given `owner`: so, this method should return `List<AuthProfileTO>` ########## File path: core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/wa/GoogleMfaAuthTokenServiceImpl.java ########## @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.syncope.core.rest.cxf.service.wa; + +import org.apache.syncope.common.lib.SyncopeClientException; +import org.apache.syncope.common.lib.types.ClientExceptionType; +import org.apache.syncope.common.lib.types.GoogleMfaAuthToken; +import org.apache.syncope.common.rest.api.RESTHeaders; +import org.apache.syncope.common.rest.api.service.wa.GoogleMfaAuthTokenService; +import org.apache.syncope.core.logic.GoogleMfaAuthTokenLogic; +import org.apache.syncope.core.persistence.api.dao.NotFoundException; +import org.apache.syncope.core.rest.cxf.service.AbstractServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.ws.rs.core.Response; + +import java.net.URI; +import java.util.Date; +import java.util.List; + +@Service +public class GoogleMfaAuthTokenServiceImpl extends AbstractServiceImpl implements GoogleMfaAuthTokenService { + @Autowired + private GoogleMfaAuthTokenLogic logic; + + @Override + public Response deleteTokensByDate(final Date expirationDate) { + logic.delete(expirationDate); + return Response.noContent().build(); + } + + @Override + public Response deleteToken(final String owner, final Integer otp) { + logic.delete(owner, otp); + return Response.noContent().build(); + } + + @Override + public Response deleteTokensFor(final String owner) { + logic.delete(owner); + return Response.noContent().build(); + } + + @Override + public Response deleteToken(final Integer otp) { + logic.delete(otp); + return Response.noContent().build(); + } + + @Override + public Response deleteTokens() { + logic.deleteAll(); + return Response.noContent().build(); + } + + @Override + public Response save(final GoogleMfaAuthToken tokenTO) { + final GoogleMfaAuthToken token = logic.save(tokenTO); + URI location = uriInfo.getAbsolutePathBuilder().path(token.getKey()).build(); + return Response.created(location). + header(RESTHeaders.RESOURCE_KEY, token.getKey()). + build(); + } + + @Override + public GoogleMfaAuthToken findTokenFor(final String owner, final Integer otp) { + try { + return logic.read(owner, otp); Review comment: Normally, `NotFoundException` is managed at logic level - see many examples including https://github.com/apache/syncope/blob/master/core/idm/logic/src/main/java/org/apache/syncope/core/logic/ResourceLogic.java#L148-L150 ########## File path: common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/AuthProfileService.java ########## @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.syncope.common.rest.api.service; + +import io.swagger.v3.oas.annotations.headers.Header; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.security.SecurityRequirements; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.apache.syncope.common.lib.to.AuthProfileTO; +import org.apache.syncope.common.rest.api.RESTHeaders; + +import javax.validation.constraints.NotNull; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import java.util.List; + +/** + * REST operations for SAML 2.0 SP metadata. + */ +@Tag(name = "Auth Profiles") +@SecurityRequirements({ + @SecurityRequirement(name = "BasicAuthentication"), + @SecurityRequirement(name = "Bearer")}) +@Path("authProfiles") +public interface AuthProfileService extends JAXRSService { + + @GET + @Produces({MediaType.APPLICATION_JSON}) + List<AuthProfileTO> list(); + + @GET + @Path("owners/{owner}") + @Consumes({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + AuthProfileTO findByOwner(@NotNull @PathParam("owner") String owner); + + @GET + @Path("{key}") + @Consumes({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + AuthProfileTO findByKey(@NotNull @PathParam("key") String key); + + @DELETE + @Path("{key}") + @Consumes({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + Response deleteByKey(@NotNull @PathParam("key") String key); + + @DELETE + @Path("owners/{owner}") + @Consumes({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + @Produces({MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML}) + Response deleteByOwner(@NotNull @PathParam("owner") String owner); + + @ApiResponses({ + @ApiResponse(responseCode = "201", + description = "AuthProfile successfully created", headers = { + @Header(name = RESTHeaders.RESOURCE_KEY, schema = + @Schema(type = "string"), + description = "UUID generated for the entity created"), + @Header(name = HttpHeaders.LOCATION, schema = + @Schema(type = "string"), + description = "URL of the entity created") }), + @ApiResponse(responseCode = "409", + description = "AuthProfile already existing") }) + @POST + @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) + Response create(@NotNull AuthProfileTO authProfileTO); Review comment: I don't think we should expose a mean to administratively create an `AuthProfile`; this method should be removed, then. ########## File path: core/am/logic/src/main/java/org/apache/syncope/core/logic/AuthProfileLogic.java ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.syncope.core.logic; + +import org.apache.syncope.common.lib.to.AuthProfileTO; +import org.apache.syncope.common.lib.types.AMEntitlement; +import org.apache.syncope.core.persistence.api.dao.NotFoundException; +import org.apache.syncope.core.persistence.api.dao.auth.AuthProfileDAO; +import org.apache.syncope.core.persistence.api.entity.auth.AuthProfile; +import org.apache.syncope.core.provisioning.api.data.AuthProfileDataBinder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.stream.Collectors; + +@Component +@Transactional(rollbackFor = {Throwable.class}) +public class AuthProfileLogic { Review comment: Please have this class extend `AbstractTransactionalLogic<AuthProfileTO>` and remove `@Transactional` annotation. ########## File path: core/am/logic/src/main/java/org/apache/syncope/core/logic/GoogleMfaAuthTokenLogic.java ########## @@ -0,0 +1,178 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.syncope.core.logic; + +import org.apache.syncope.common.lib.types.AMEntitlement; +import org.apache.syncope.common.lib.types.GoogleMfaAuthToken; +import org.apache.syncope.common.lib.types.IdRepoEntitlement; +import org.apache.syncope.core.persistence.api.dao.auth.AuthProfileDAO; +import org.apache.syncope.core.persistence.api.entity.EntityFactory; +import org.apache.syncope.core.persistence.api.entity.auth.AuthProfile; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.UUID; +import java.util.function.Predicate; + +@Component +@Transactional(rollbackFor = { Throwable.class }) +public class GoogleMfaAuthTokenLogic { Review comment: Please have this class extend `AbstractTransactionalLogic<AuthProfileTO>` and remove `@Transactional` annotation. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org