Skip to content

Commit

Permalink
simplify UserInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ibodrov committed Jan 2, 2025
1 parent e9f5baf commit c26455b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* =====
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.server.process.pipelines.processors.signing.Signing;
import com.walmartlabs.concord.server.user.UserManager;
Expand All @@ -31,7 +32,7 @@
public class CurrentUserInfoProcessor extends UserInfoProcessor {

@Inject
public CurrentUserInfoProcessor(UserManager userManager, Signing signing) {
super(Constants.Request.CURRENT_USER_KEY, userManager, signing);
public CurrentUserInfoProcessor(UserManager userManager, Signing signing, ObjectMapper objectMapper) {
super(Constants.Request.CURRENT_USER_KEY, userManager, signing, objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* =====
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.server.process.pipelines.processors.signing.Signing;
import com.walmartlabs.concord.server.user.UserManager;
Expand All @@ -31,7 +32,7 @@
public class InitiatorUserInfoProcessor extends UserInfoProcessor {

@Inject
public InitiatorUserInfoProcessor(UserManager userManager, Signing signing) {
super(Constants.Request.INITIATOR_KEY, userManager, signing);
public InitiatorUserInfoProcessor(UserManager userManager, Signing signing, ObjectMapper objectMapper) {
super(Constants.Request.INITIATOR_KEY, userManager, signing, objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
* =====
*/

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.walmartlabs.concord.server.process.Payload;
import com.walmartlabs.concord.server.process.pipelines.processors.signing.Signing;
import com.walmartlabs.concord.server.sdk.ConcordApplicationException;
import com.walmartlabs.concord.server.user.UserInfoProvider.BaseUserInfo;
import com.walmartlabs.concord.server.user.UserManager;
import org.immutables.value.Value;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
* Collects and stores the current user's data.
Expand All @@ -40,19 +43,28 @@ public abstract class UserInfoProcessor implements PayloadProcessor {
private final String key;
private final UserManager userManager;
private final Signing signing;
private final ObjectMapper objectMapper;

public UserInfoProcessor(String key,
UserManager userManager,
Signing signing,
ObjectMapper objectMapper) {

public UserInfoProcessor(String key, UserManager userManager, Signing signing) {
this.key = key;
this.userManager = userManager;
this.signing = signing;
this.key = requireNonNull(key);
this.userManager = requireNonNull(userManager);
this.signing = requireNonNull(signing);
this.objectMapper = requireNonNull(objectMapper);
}

@Override
public Payload process(Chain chain, Payload payload) {
BaseUserInfo info = userManager.getCurrentUserInfo();
var info = userManager.getCurrentUserInfo();

var result = objectMapper.convertValue(info, ObjectNode.class);
if (signing.isEnabled()) {
info = sign(info);
Optional.ofNullable(info.username())
.map(this::sign)
.ifPresent(signature -> result.set("usernameSignature", signature));
}

Map<String, BaseUserInfo> m = new HashMap<>();
Expand All @@ -63,28 +75,11 @@ public Payload process(Chain chain, Payload payload) {
return chain.process(payload);
}

private BaseUserInfo sign(BaseUserInfo i) {
if (i == null || i.username() == null) {
return i;
}

private TextNode sign(String username) {
try {
String s = signing.sign(i.username());
return SignedUserInfo.from(i).usernameSignature(s).build();
return TextNode.valueOf(signing.sign(username));
} catch (Exception e) {
throw new ConcordApplicationException("Error while singing process data: " + e.getMessage(), e);
}
}

@Value.Immutable
@JsonSerialize(as = ImmutableSignedUserInfo.class)
@JsonDeserialize(as = ImmutableSignedUserInfo.class)
public interface SignedUserInfo extends BaseUserInfo {

String usernameSignature();

public static ImmutableSignedUserInfo.Builder from(BaseUserInfo i) {
return ImmutableSignedUserInfo.builder().from(i);
}
}
}

0 comments on commit c26455b

Please sign in to comment.