This is an automated email from the ASF dual-hosted git repository. ilgrosso pushed a commit to branch 4_0_X in repository https://gitbox.apache.org/repos/asf/syncope.git
The following commit(s) were added to refs/heads/4_0_X by this push: new 4265c0d620 [SYNCOPE-1883] Storing username along with GoogleMfaAuthAccount (#1081) 4265c0d620 is described below commit 4265c0d6209538cf2bdeb01217a4d4933a0aec28 Author: Francesco Chicchiriccò <ilgro...@users.noreply.github.com> AuthorDate: Thu May 15 12:27:55 2025 +0200 [SYNCOPE-1883] Storing username along with GoogleMfaAuthAccount (#1081) --- .../console/panels/search/SearchClausePanel.java | 24 +++- .../client/enduser/panels/any/PlainAttrs.java | 1 - .../common/lib/wa/GoogleMfaAuthAccount.java | 122 +++++++++++++-------- .../syncope/common/lib/wa/GoogleMfaAuthToken.java | 24 ++-- .../service/wa/GoogleMfaAuthAccountService.java | 42 ++++--- .../core/logic/wa/GoogleMfaAuthAccountLogic.java | 11 +- .../wa/GoogleMfaAuthAccountServiceImpl.java | 10 +- .../persistence/jpa/inner/AuthProfileTest.java | 5 +- .../persistence/neo4j/inner/AuthProfileTest.java | 5 +- .../fit/core/wa/GoogleMfaAuthAccountITCase.java | 25 +++-- .../gauth/WAGoogleMfaAuthCredentialRepository.java | 74 ++++++------- .../gauth/WAGoogleMfaAuthTokenRepository.java | 23 ++-- 12 files changed, 202 insertions(+), 164 deletions(-) diff --git a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/search/SearchClausePanel.java b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/search/SearchClausePanel.java index 96262d306c..fba724f091 100644 --- a/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/search/SearchClausePanel.java +++ b/client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/search/SearchClausePanel.java @@ -44,6 +44,7 @@ import org.apache.syncope.client.console.wicket.ajax.form.IndicatorAjaxEventBeha import org.apache.syncope.client.lib.SyncopeClient; import org.apache.syncope.client.ui.commons.Constants; import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior; +import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDateFieldPanel; import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDateTimeFieldPanel; import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel; import org.apache.syncope.client.ui.commons.markup.html.form.AjaxNumberFieldPanel; @@ -947,10 +948,7 @@ public class SearchClausePanel extends FieldPanel<SearchClause> { ? DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT : FastDateFormat.getInstance(plainSchema.getConversionPattern()); - result = new AjaxDateTimeFieldPanel( - "value", - "value", - new PropertyModel<>(searchClause, "value") { + PropertyModel<Date> dateModel = new PropertyModel<>(searchClause, "value") { private static final long serialVersionUID = -3743432456095828573L; @@ -970,7 +968,23 @@ public class SearchClausePanel extends FieldPanel<SearchClause> { public void setObject(final Date object) { Optional.ofNullable(object).ifPresent(date -> searchClause.setValue(formatter.format(date))); } - }, DateFormatUtils.ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT); + }; + + if (plainSchema.getConversionPattern() == null + || StringUtils.containsIgnoreCase(plainSchema.getConversionPattern(), "H")) { + + result = new AjaxDateTimeFieldPanel( + "value", + "value", + dateModel, + formatter); + } else { + result = new AjaxDateFieldPanel( + "value", + "value", + dateModel, + formatter); + } break; case Enum: diff --git a/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/panels/any/PlainAttrs.java b/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/panels/any/PlainAttrs.java index 45ef210a09..6e60bee248 100644 --- a/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/panels/any/PlainAttrs.java +++ b/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/panels/any/PlainAttrs.java @@ -18,7 +18,6 @@ */ package org.apache.syncope.client.enduser.panels.any; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; diff --git a/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthAccount.java b/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthAccount.java index fc01b3d5e4..82ca852d51 100644 --- a/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthAccount.java +++ b/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthAccount.java @@ -18,7 +18,7 @@ */ package org.apache.syncope.common.lib.wa; -import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.builder.EqualsBuilder; @@ -34,13 +34,18 @@ public class GoogleMfaAuthAccount implements BaseBean { private final GoogleMfaAuthAccount instance = new GoogleMfaAuthAccount(); - public GoogleMfaAuthAccount.Builder registrationDate(final OffsetDateTime date) { - instance.setRegistrationDate(date); + public GoogleMfaAuthAccount.Builder id(final Long id) { + instance.setId(id); return this; } - public GoogleMfaAuthAccount.Builder scratchCodes(final List<Integer> codes) { - instance.setScratchCodes(codes); + public GoogleMfaAuthAccount.Builder name(final String name) { + instance.setName(name); + return this; + } + + public GoogleMfaAuthAccount.Builder username(final String username) { + instance.setUsername(username); return this; } @@ -54,13 +59,18 @@ public class GoogleMfaAuthAccount implements BaseBean { return this; } - public GoogleMfaAuthAccount.Builder id(final Long id) { - instance.setId(id); + public GoogleMfaAuthAccount.Builder scratchCodes(final List<Integer> codes) { + instance.setScratchCodes(codes); return this; } - public GoogleMfaAuthAccount.Builder name(final String name) { - instance.setName(name); + public GoogleMfaAuthAccount.Builder registrationDate(final ZonedDateTime date) { + instance.setRegistrationDate(date); + return this; + } + + public GoogleMfaAuthAccount.Builder source(final String source) { + instance.setSource(source); return this; } @@ -69,17 +79,29 @@ public class GoogleMfaAuthAccount implements BaseBean { } } - private String secretKey; + private long id; private String name; - private int validationCode; + private String username; - private long id; + private String secretKey; + + private int validationCode; private List<Integer> scratchCodes = new ArrayList<>(0); - private OffsetDateTime registrationDate; + private ZonedDateTime registrationDate; + + private String source; + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } public String getName() { return name; @@ -89,12 +111,12 @@ public class GoogleMfaAuthAccount implements BaseBean { this.name = name; } - public long getId() { - return id; + public String getUsername() { + return username; } - public void setId(final long id) { - this.id = id; + public void setUsername(final String username) { + this.username = username; } public String getSecretKey() { @@ -121,24 +143,34 @@ public class GoogleMfaAuthAccount implements BaseBean { this.scratchCodes = scratchCodes; } - public OffsetDateTime getRegistrationDate() { + public ZonedDateTime getRegistrationDate() { return registrationDate; } - public void setRegistrationDate(final OffsetDateTime registrationDate) { + public void setRegistrationDate(final ZonedDateTime registrationDate) { this.registrationDate = registrationDate; } + public String getSource() { + return source; + } + + public void setSource(final String source) { + this.source = source; + } + @Override public int hashCode() { - return new HashCodeBuilder() - .append(secretKey) - .append(name) - .append(id) - .append(scratchCodes) - .append(validationCode) - .append(registrationDate) - .toHashCode(); + return new HashCodeBuilder(). + append(id). + append(name). + append(username). + append(secretKey). + append(validationCode). + append(scratchCodes). + append(registrationDate). + append(source). + build(); } @Override @@ -153,25 +185,29 @@ public class GoogleMfaAuthAccount implements BaseBean { return false; } GoogleMfaAuthAccount other = (GoogleMfaAuthAccount) obj; - return new EqualsBuilder() - .append(this.secretKey, other.secretKey) - .append(this.name, other.name) - .append(this.id, other.id) - .append(this.scratchCodes, other.scratchCodes) - .append(this.registrationDate, other.registrationDate) - .append(this.validationCode, other.validationCode) - .isEquals(); + return new EqualsBuilder(). + append(id, other.id). + append(name, other.name). + append(username, other.username). + append(secretKey, other.secretKey). + append(validationCode, other.validationCode). + append(scratchCodes, other.scratchCodes). + append(registrationDate, other.registrationDate). + append(source, other.source). + build(); } @Override public String toString() { - return new ToStringBuilder(this) - .append("name", name) - .append("secretKey", secretKey) - .append("id", id) - .append("scratchCodes", scratchCodes) - .append("registrationDate", registrationDate) - .append("validationCode", validationCode) - .toString(); + return new ToStringBuilder(this). + append(id). + append(name). + append(username). + append(secretKey). + append(validationCode). + append(scratchCodes). + append(registrationDate). + append(source). + build(); } } diff --git a/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthToken.java b/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthToken.java index 470103a598..9e4d266349 100644 --- a/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthToken.java +++ b/common/am/lib/src/main/java/org/apache/syncope/common/lib/wa/GoogleMfaAuthToken.java @@ -69,10 +69,10 @@ public class GoogleMfaAuthToken implements BaseBean { @Override public int hashCode() { - return new HashCodeBuilder() - .append(otp) - .append(issueDate) - .toHashCode(); + return new HashCodeBuilder(). + append(otp). + append(issueDate). + build(); } @Override @@ -87,17 +87,17 @@ public class GoogleMfaAuthToken implements BaseBean { return false; } GoogleMfaAuthToken other = (GoogleMfaAuthToken) obj; - return new EqualsBuilder() - .append(this.otp, other.otp) - .append(this.issueDate, other.issueDate) - .isEquals(); + return new EqualsBuilder(). + append(otp, other.otp). + append(issueDate, other.issueDate). + build(); } @Override public String toString() { - return new ToStringBuilder(this) - .append("token", otp) - .append("issueDate", issueDate) - .toString(); + return new ToStringBuilder(this). + append("token", otp). + append("issueDate", issueDate). + build(); } } diff --git a/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/wa/GoogleMfaAuthAccountService.java b/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/wa/GoogleMfaAuthAccountService.java index b003a4992e..8b3b751950 100644 --- a/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/wa/GoogleMfaAuthAccountService.java +++ b/common/am/rest-api/src/main/java/org/apache/syncope/common/rest/api/service/wa/GoogleMfaAuthAccountService.java @@ -40,54 +40,50 @@ import org.apache.syncope.common.rest.api.service.JAXRSService; @SecurityRequirements({ @SecurityRequirement(name = "BasicAuthentication"), @SecurityRequirement(name = "Bearer") }) -@Path("wa/gauth") +@Path("wa/gauth/accts") public interface GoogleMfaAuthAccountService extends JAXRSService { - @DELETE + @GET @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - @Path("accts/{owner}") - void delete(@NotNull @PathParam("owner") String owner); + PagedResult<GoogleMfaAuthAccount> list(); - @DELETE + @GET @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - @Path("accts/devices/{id}") - void delete(@NotNull @PathParam("id") long id); + @Path("{owner}") + PagedResult<GoogleMfaAuthAccount> read(@NotNull @PathParam("owner") String owner); - @DELETE + @GET + @Path("devices/{id}") @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - @Path("accts") - void deleteAll(); + GoogleMfaAuthAccount read(@NotNull @PathParam("id") long id); @POST - @Path("accts/{owner}") @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - void create(@NotNull @PathParam("owner") String owner, @NotNull GoogleMfaAuthAccount acct); + void create(@NotNull GoogleMfaAuthAccount acct); @PUT - @Path("accts/{owner}") @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - void update(@NotNull @PathParam("owner") String owner, @NotNull GoogleMfaAuthAccount acct); + void update(@NotNull GoogleMfaAuthAccount acct); - @GET + @DELETE @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - @Path("accts/{owner}") - PagedResult<GoogleMfaAuthAccount> read(@NotNull @PathParam("owner") String owner); + @Path("{owner}") + void delete(@NotNull @PathParam("owner") String owner); - @GET - @Path("accts/id/{id}") + @DELETE @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - GoogleMfaAuthAccount read(@NotNull @PathParam("id") long id); + @Path("devices/{id}") + void delete(@NotNull @PathParam("id") long id); - @GET - @Path("accts") + @DELETE @Consumes({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, RESTHeaders.APPLICATION_YAML, MediaType.APPLICATION_XML }) - PagedResult<GoogleMfaAuthAccount> list(); + void deleteAll(); } diff --git a/core/am/logic/src/main/java/org/apache/syncope/core/logic/wa/GoogleMfaAuthAccountLogic.java b/core/am/logic/src/main/java/org/apache/syncope/core/logic/wa/GoogleMfaAuthAccountLogic.java index ae59dfd8df..2877126edd 100644 --- a/core/am/logic/src/main/java/org/apache/syncope/core/logic/wa/GoogleMfaAuthAccountLogic.java +++ b/core/am/logic/src/main/java/org/apache/syncope/core/logic/wa/GoogleMfaAuthAccountLogic.java @@ -91,8 +91,8 @@ public class GoogleMfaAuthAccountLogic extends AbstractAuthProfileLogic { } @PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')") - public void create(final String owner, final GoogleMfaAuthAccount account) { - AuthProfile profile = authProfile(owner); + public void create(final GoogleMfaAuthAccount account) { + AuthProfile profile = authProfile(account.getUsername()); List<GoogleMfaAuthAccount> accounts = profile.getGoogleMfaAuthAccounts(); accounts.add(account); @@ -101,9 +101,10 @@ public class GoogleMfaAuthAccountLogic extends AbstractAuthProfileLogic { } @PreAuthorize("hasRole('" + IdRepoEntitlement.ANONYMOUS + "')") - public void update(final String owner, final GoogleMfaAuthAccount account) { - AuthProfile authProfile = authProfileDAO.findByOwner(owner). - orElseThrow(() -> new NotFoundException("Could not find account for Owner " + owner)); + public void update(final GoogleMfaAuthAccount account) { + AuthProfile authProfile = authProfileDAO.findByOwner(account.getUsername()). + orElseThrow(() -> new NotFoundException("Could not find account for Owner " + account.getUsername())); + List<GoogleMfaAuthAccount> accounts = authProfile.getGoogleMfaAuthAccounts(); if (accounts.removeIf(acct -> acct.getId() == account.getId())) { accounts.add(account); diff --git a/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/wa/GoogleMfaAuthAccountServiceImpl.java b/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/wa/GoogleMfaAuthAccountServiceImpl.java index 8e9b967469..4324c40d13 100644 --- a/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/wa/GoogleMfaAuthAccountServiceImpl.java +++ b/core/am/rest-cxf/src/main/java/org/apache/syncope/core/rest/cxf/service/wa/GoogleMfaAuthAccountServiceImpl.java @@ -49,16 +49,16 @@ public class GoogleMfaAuthAccountServiceImpl extends AbstractService implements } @Override - public void create(final String owner, final GoogleMfaAuthAccount acct) { - logic.create(owner, acct); + public void create(final GoogleMfaAuthAccount acct) { + logic.create(acct); } @Override - public void update(final String owner, final GoogleMfaAuthAccount acct) { - logic.update(owner, acct); + public void update(final GoogleMfaAuthAccount acct) { + logic.update(acct); } - private PagedResult<GoogleMfaAuthAccount> build(final List<GoogleMfaAuthAccount> read) { + protected PagedResult<GoogleMfaAuthAccount> build(final List<GoogleMfaAuthAccount> read) { PagedResult<GoogleMfaAuthAccount> result = new PagedResult<>(); result.setPage(1); result.setSize(read.size()); diff --git a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthProfileTest.java b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthProfileTest.java index 4a930eda6d..104d916e53 100644 --- a/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthProfileTest.java +++ b/core/persistence-jpa/src/test/java/org/apache/syncope/core/persistence/jpa/inner/AuthProfileTest.java @@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.time.LocalDateTime; -import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.List; import java.util.Optional; import java.util.stream.IntStream; @@ -182,11 +182,12 @@ public class AuthProfileTest extends AbstractTest { AuthProfile profile = entityFactory.newEntity(AuthProfile.class); profile.setOwner(owner); GoogleMfaAuthAccount account = new GoogleMfaAuthAccount.Builder() - .registrationDate(OffsetDateTime.now()) + .registrationDate(ZonedDateTime.now()) .scratchCodes(List.of(1, 2, 3, 4, 5)) .secretKey(SecureRandomUtils.generateRandomUUID().toString()) .validationCode(123456) .name(SecureRandomUtils.generateRandomUUID().toString()) + .username(owner) .build(); profile.setGoogleMfaAuthAccounts(List.of(account)); return authProfileDAO.save(profile); diff --git a/core/persistence-neo4j/src/test/java/org/apache/syncope/core/persistence/neo4j/inner/AuthProfileTest.java b/core/persistence-neo4j/src/test/java/org/apache/syncope/core/persistence/neo4j/inner/AuthProfileTest.java index afb48b26f8..659a6cd81e 100644 --- a/core/persistence-neo4j/src/test/java/org/apache/syncope/core/persistence/neo4j/inner/AuthProfileTest.java +++ b/core/persistence-neo4j/src/test/java/org/apache/syncope/core/persistence/neo4j/inner/AuthProfileTest.java @@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.time.LocalDateTime; -import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.List; import java.util.Optional; import java.util.stream.IntStream; @@ -186,11 +186,12 @@ public class AuthProfileTest extends AbstractTest { AuthProfile profile = entityFactory.newEntity(AuthProfile.class); profile.setOwner(owner); GoogleMfaAuthAccount account = new GoogleMfaAuthAccount.Builder() - .registrationDate(OffsetDateTime.now()) + .registrationDate(ZonedDateTime.now()) .scratchCodes(List.of(1, 2, 3, 4, 5)) .secretKey(SecureRandomUtils.generateRandomUUID().toString()) .validationCode(123456) .name(SecureRandomUtils.generateRandomUUID().toString()) + .username(owner) .build(); profile.setGoogleMfaAuthAccounts(List.of(account)); return authProfileDAO.save(profile); diff --git a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/wa/GoogleMfaAuthAccountITCase.java b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/wa/GoogleMfaAuthAccountITCase.java index d054b56281..26373b9c5e 100644 --- a/fit/core-reference/src/test/java/org/apache/syncope/fit/core/wa/GoogleMfaAuthAccountITCase.java +++ b/fit/core-reference/src/test/java/org/apache/syncope/fit/core/wa/GoogleMfaAuthAccountITCase.java @@ -23,7 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.List; import java.util.UUID; import org.apache.syncope.common.lib.SyncopeClientException; @@ -36,9 +36,10 @@ import org.junit.jupiter.api.Test; public class GoogleMfaAuthAccountITCase extends AbstractITCase { - private static GoogleMfaAuthAccount createGoogleMfaAuthAccount() { + private static GoogleMfaAuthAccount createGoogleMfaAuthAccount(final String username) { return new GoogleMfaAuthAccount.Builder() - .registrationDate(OffsetDateTime.now()) + .username(username) + .registrationDate(ZonedDateTime.now()) .scratchCodes(List.of(1, 2, 3, 4, 5)) .secretKey(SecureRandomUtils.generateRandomUUID().toString()) .validationCode(123456) @@ -53,15 +54,15 @@ public class GoogleMfaAuthAccountITCase extends AbstractITCase { @Test public void create() { - GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(); - assertDoesNotThrow(() -> GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(UUID.randomUUID().toString(), acct)); + GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(UUID.randomUUID().toString()); + assertDoesNotThrow(() -> GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(acct)); } @Test public void count() { String owner = UUID.randomUUID().toString(); - GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(); - GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(owner, acct); + GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(owner); + GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(acct); PagedResult<GoogleMfaAuthAccount> list = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.list(); assertFalse(list.getResult().isEmpty()); assertEquals(1, list.getTotalCount()); @@ -74,8 +75,8 @@ public class GoogleMfaAuthAccountITCase extends AbstractITCase { @Test public void delete() { String owner = UUID.randomUUID().toString(); - GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(); - GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(owner, acct); + GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(owner); + GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(acct); GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.delete(owner); assertThrows(SyncopeClientException.class, () -> GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(owner)); } @@ -83,12 +84,12 @@ public class GoogleMfaAuthAccountITCase extends AbstractITCase { @Test public void update() { String owner = UUID.randomUUID().toString(); - GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(); - GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(owner, acct); + GoogleMfaAuthAccount acct = createGoogleMfaAuthAccount(owner); + GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.create(acct); acct = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(acct.getId()); acct.setSecretKey("NewSecret"); acct.setScratchCodes(List.of(9, 8, 7, 6, 5)); - GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.update(owner, acct); + GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.update(acct); assertEquals(1, GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.list().getTotalCount()); acct = GOOGLE_MFA_AUTH_ACCOUNT_SERVICE.read(owner).getResult().getFirst(); assertEquals(acct.getSecretKey(), acct.getSecretKey()); diff --git a/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthCredentialRepository.java b/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthCredentialRepository.java index fbfcb77ae7..288d1cffc0 100644 --- a/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthCredentialRepository.java +++ b/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthCredentialRepository.java @@ -19,7 +19,7 @@ package org.apache.syncope.wa.starter.gauth; import com.warrenstrange.googleauth.IGoogleAuthenticator; -import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; @@ -48,37 +48,23 @@ public class WAGoogleMfaAuthCredentialRepository extends BaseGoogleAuthenticator this.waRestClient = waRestClient; } - protected GoogleMfaAuthAccount mapGoogleMfaAuthAccount(final OneTimeTokenAccount otta) { - return new GoogleMfaAuthAccount.Builder(). - registrationDate(OffsetDateTime.now()). - scratchCodes(otta.getScratchCodes().stream().map(Number::intValue).toList()). - validationCode(otta.getValidationCode()). - secretKey(otta.getSecretKey()). - id(otta.getId()). - build(); - } - protected GoogleAuthenticatorAccount mapGoogleMfaAuthAccount(final GoogleMfaAuthAccount gmfaa) { return GoogleAuthenticatorAccount.builder(). + id(gmfaa.getId()). + name(gmfaa.getName()). + username(gmfaa.getUsername()). secretKey(gmfaa.getSecretKey()). validationCode(gmfaa.getValidationCode()). scratchCodes(gmfaa.getScratchCodes().stream().map(Number::intValue).collect(Collectors.toList())). - name(gmfaa.getName()). - id(gmfaa.getId()). + registrationDate(gmfaa.getRegistrationDate()). + source(gmfaa.getSource()). build(); } - protected GoogleMfaAuthAccountService service() { - return waRestClient.getService(GoogleMfaAuthAccountService.class); - } - @Override public OneTimeTokenAccount get(final long id) { try { - GoogleMfaAuthAccount account = service().read(id); - if (account != null) { - return mapGoogleMfaAuthAccount(account); - } + return mapGoogleMfaAuthAccount(waRestClient.getService(GoogleMfaAuthAccountService.class).read(id)); } catch (SyncopeClientException e) { if (e.getType() == ClientExceptionType.NotFound) { LOG.info("Could not locate account for id {}", id); @@ -92,7 +78,7 @@ public class WAGoogleMfaAuthCredentialRepository extends BaseGoogleAuthenticator @Override public OneTimeTokenAccount get(final String username, final long id) { try { - return service().read(username). + return waRestClient.getService(GoogleMfaAuthAccountService.class).read(username). getResult().stream(). filter(account -> account.getId() == id). map(this::mapGoogleMfaAuthAccount). @@ -111,7 +97,7 @@ public class WAGoogleMfaAuthCredentialRepository extends BaseGoogleAuthenticator @Override public Collection<? extends OneTimeTokenAccount> get(final String username) { try { - return service().read(username). + return waRestClient.getService(GoogleMfaAuthAccountService.class).read(username). getResult().stream(). map(this::mapGoogleMfaAuthAccount). toList(); @@ -127,42 +113,48 @@ public class WAGoogleMfaAuthCredentialRepository extends BaseGoogleAuthenticator @Override public Collection<? extends OneTimeTokenAccount> load() { - return service().list(). + return waRestClient.getService(GoogleMfaAuthAccountService.class).list(). getResult().stream(). map(this::mapGoogleMfaAuthAccount). toList(); } - @Override - public OneTimeTokenAccount save(final OneTimeTokenAccount otta) { - GoogleMfaAuthAccount account = new GoogleMfaAuthAccount.Builder(). - registrationDate(OffsetDateTime.now()). - scratchCodes(otta.getScratchCodes().stream().map(Number::intValue).toList()). - validationCode(otta.getValidationCode()). - secretKey(otta.getSecretKey()). - name(otta.getName()). + protected GoogleMfaAuthAccount mapOneTimeTokenAccount(final OneTimeTokenAccount otta) { + return new GoogleMfaAuthAccount.Builder(). id(otta.getId()). + name(otta.getName()). + username(otta.getUsername()). + secretKey(otta.getSecretKey()). + validationCode(otta.getValidationCode()). + scratchCodes(otta.getScratchCodes().stream().map(Number::intValue).toList()). + registrationDate(ZonedDateTime.now()). + source(otta.getSource()). build(); - service().create(otta.getUsername(), account); - return mapGoogleMfaAuthAccount(account); + } + + @Override + public OneTimeTokenAccount save(final OneTimeTokenAccount otta) { + GoogleMfaAuthAccount account = mapOneTimeTokenAccount(otta); + waRestClient.getService(GoogleMfaAuthAccountService.class).create(account); + return otta; } @Override public OneTimeTokenAccount update(final OneTimeTokenAccount tokenAccount) { - GoogleMfaAuthAccount acct = mapGoogleMfaAuthAccount(tokenAccount); - service().update(tokenAccount.getUsername(), acct); + GoogleMfaAuthAccount acct = mapOneTimeTokenAccount(tokenAccount); + waRestClient.getService(GoogleMfaAuthAccountService.class).update(acct); return tokenAccount; } @Override public void deleteAll() { - service().deleteAll(); + waRestClient.getService(GoogleMfaAuthAccountService.class).deleteAll(); } @Override public void delete(final String username) { try { - service().delete(username); + waRestClient.getService(GoogleMfaAuthAccountService.class).delete(username); } catch (SyncopeClientException e) { if (e.getType() == ClientExceptionType.NotFound) { LOG.info("Could not locate account for owner {}", username); @@ -174,18 +166,18 @@ public class WAGoogleMfaAuthCredentialRepository extends BaseGoogleAuthenticator @Override public void delete(final long id) { - service().delete(id); + waRestClient.getService(GoogleMfaAuthAccountService.class).delete(id); } @Override public long count() { - return service().list().getTotalCount(); + return waRestClient.getService(GoogleMfaAuthAccountService.class).list().getTotalCount(); } @Override public long count(final String username) { try { - return service().read(username).getTotalCount(); + return waRestClient.getService(GoogleMfaAuthAccountService.class).read(username).getTotalCount(); } catch (SyncopeClientException e) { if (e.getType() == ClientExceptionType.NotFound) { LOG.info("Could not locate account for owner {}", username); diff --git a/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthTokenRepository.java b/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthTokenRepository.java index 6148dc0ee0..d495531b4d 100644 --- a/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthTokenRepository.java +++ b/wa/starter/src/main/java/org/apache/syncope/wa/starter/gauth/WAGoogleMfaAuthTokenRepository.java @@ -40,13 +40,10 @@ public class WAGoogleMfaAuthTokenRepository extends BaseOneTimeTokenRepository<G this.expireTokensInSeconds = expireTokensInSeconds; } - protected GoogleMfaAuthTokenService service() { - return waRestClient.getService(GoogleMfaAuthTokenService.class); - } - @Override protected void cleanInternal() { - service().delete(LocalDateTime.now().minusSeconds(expireTokensInSeconds)); + waRestClient.getService(GoogleMfaAuthTokenService.class). + delete(LocalDateTime.now().minusSeconds(expireTokensInSeconds)); } @Override @@ -55,14 +52,14 @@ public class WAGoogleMfaAuthTokenRepository extends BaseOneTimeTokenRepository<G token(token.getToken()). issueDate(token.getIssuedDateTime()). build(); - service().store(token.getUserId(), tokenTO); + waRestClient.getService(GoogleMfaAuthTokenService.class).store(token.getUserId(), tokenTO); return token; } @Override public GoogleAuthenticatorToken get(final String username, final Integer otp) { try { - GoogleMfaAuthToken tokenTO = service().read(username, otp); + GoogleMfaAuthToken tokenTO = waRestClient.getService(GoogleMfaAuthTokenService.class).read(username, otp); GoogleAuthenticatorToken token = new GoogleAuthenticatorToken(tokenTO.getOtp(), username); token.setIssuedDateTime(tokenTO.getIssueDate()); return token; @@ -74,31 +71,31 @@ public class WAGoogleMfaAuthTokenRepository extends BaseOneTimeTokenRepository<G @Override public void remove(final String username, final Integer otp) { - service().delete(username, otp); + waRestClient.getService(GoogleMfaAuthTokenService.class).delete(username, otp); } @Override public void remove(final String username) { - service().delete(username); + waRestClient.getService(GoogleMfaAuthTokenService.class).delete(username); } @Override public void remove(final Integer otp) { - service().delete(otp); + waRestClient.getService(GoogleMfaAuthTokenService.class).delete(otp); } @Override public void removeAll() { - service().delete((LocalDateTime) null); + waRestClient.getService(GoogleMfaAuthTokenService.class).delete((LocalDateTime) null); } @Override public long count(final String username) { - return service().read(username).getTotalCount(); + return waRestClient.getService(GoogleMfaAuthTokenService.class).read(username).getTotalCount(); } @Override public long count() { - return service().list().getTotalCount(); + return waRestClient.getService(GoogleMfaAuthTokenService.class).list().getTotalCount(); } }