Skip to content

Commit

Permalink
Fix infinite recursion on object serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
enricovianello committed Jan 4, 2025
1 parent 1ecf6d9 commit e0ef6d4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
*/
package it.infn.mw.iam.audit.events.account.multi_factor_authentication;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import it.infn.mw.iam.audit.events.account.AccountEvent;
import it.infn.mw.iam.audit.utils.IamTotpMfaSerializer;
import it.infn.mw.iam.persistence.model.IamAccount;
import it.infn.mw.iam.persistence.model.IamTotpMfa;

public class MultiFactorEvent extends AccountEvent {

private static final long serialVersionUID = 1L;

@JsonSerialize(using=IamTotpMfaSerializer.class)
private final IamTotpMfa totpMfa;

protected MultiFactorEvent(Object source, IamAccount account, IamTotpMfa totpMfa,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2021
*
* Licensed 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 it.infn.mw.iam.audit.utils;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import it.infn.mw.iam.persistence.model.IamTotpMfa;

public class IamTotpMfaSerializer extends JsonSerializer<IamTotpMfa> {

@Override
public void serialize(IamTotpMfa value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {

gen.writeStartObject();
gen.writeStringField("account", value.getAccount().getUsername());
gen.writeStringField("creationTime", value.getCreationTime().toString());
gen.writeStringField("lastUpdateTime", value.getLastUpdateTime().toString());
gen.writeStringField("active", String.valueOf(value.isActive()));
gen.writeEndObject();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Preconditions;

@Entity
Expand Down Expand Up @@ -91,7 +90,6 @@ public class IamAccount implements Serializable {

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "user_info_id")
@JsonIgnore
private IamUserInfo userInfo;

@Temporal(TemporalType.TIMESTAMP)
Expand All @@ -105,17 +103,14 @@ public class IamAccount implements Serializable {
private Set<IamAuthority> authorities = new HashSet<>();

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonIgnore
private Set<IamAccountGroupMembership> groups = new HashSet<>();

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.EAGER,
orphanRemoval = true)
@JsonIgnore
private Set<IamSamlId> samlIds = new HashSet<>();

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.EAGER,
orphanRemoval = true)
@JsonIgnore
private Set<IamOidcId> oidcIds = new HashSet<>();

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.EAGER,
Expand All @@ -124,7 +119,6 @@ public class IamAccount implements Serializable {

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.EAGER,
orphanRemoval = true)
@JsonIgnore
private Set<IamX509Certificate> x509Certificates = new HashSet<>();

@Column(name = "confirmation_key", unique = true, length = 36)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
Expand All @@ -38,7 +39,8 @@ public class IamTotpMfa implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne()
@OneToOne
@JoinColumn(name = "account_id")
private IamAccount account;

@Column(name = "secret", nullable = false)
Expand Down

0 comments on commit e0ef6d4

Please sign in to comment.