001package ca.cdr.api.model.json;
002
003/*-
004 * #%L
005 * Smile CDR - CDR
006 * %%
007 * Copyright (C) 2016 - 2025 Smile CDR, Inc.
008 * %%
009 * All rights reserved.
010 * #L%
011 */
012
013import ca.uhn.fhir.util.JsonUtil;
014import com.fasterxml.jackson.annotation.JsonProperty;
015import com.fasterxml.jackson.annotation.JsonPropertyOrder;
016import io.swagger.v3.oas.annotations.media.Schema;
017import jakarta.validation.constraints.NotBlank;
018import org.springframework.security.oauth2.provider.ClientDetails;
019
020import java.io.Serial;
021import java.util.HashMap;
022import java.util.Map;
023import java.util.regex.Pattern;
024
025@Schema(name = "OAuth2Client", description = "Represents an OpenID Connect client")
026@JsonPropertyOrder(
027                value = {
028                        OAuth2WritableClientDetailsJson.PID,
029                        OAuth2ClientDetailsJson.NODE_ID,
030                        OAuth2ClientDetailsJson.MODULE_ID,
031                        OAuth2WritableClientDetailsJson.CLIENT_ID,
032                        OAuth2WritableClientDetailsJson.CLIENT_NAME,
033                        OAuth2WritableClientDetailsJson.ENABLED
034                },
035                alphabetic = true)
036public class OAuth2ClientDetailsJson extends OAuth2WritableClientDetailsJson
037                implements ClientDetails, IModelJson, IOAuth2ClientDetails, IHasUserData {
038
039        public static final String MODULEID_PATTERN_CHARS = "[a-zA-Z0-9._-]";
040        public static final int CLIENT_ID_MAX_LENGTH = 200;
041        public static final String CLIENT_ID_PATTERN = MODULEID_PATTERN_CHARS + "{1," + CLIENT_ID_MAX_LENGTH + "}";
042        public static final Pattern CLIENT_ID_PATTERN_P = Pattern.compile(CLIENT_ID_PATTERN);
043        public static final String MODULE_ID = "moduleId";
044        public static final String NODE_ID = "nodeId";
045
046        @Serial
047        private static final long serialVersionUID = 1L;
048
049        @NotBlank
050        @JsonProperty(MODULE_ID)
051        @Schema(description = "The Module ID that this client is registered against")
052        private String myModuleId;
053
054        @NotBlank
055        @JsonProperty(NODE_ID)
056        @Schema(description = "The Node ID that this client is registered against")
057        private String myNodeId;
058
059        @Schema(description = "The user data for this session.")
060        @JsonProperty("userData")
061        private Map<String, Object> myUserData;
062
063        /**
064         * Constructor
065         */
066        public OAuth2ClientDetailsJson() {
067                super();
068        }
069
070        /**
071         * Copy constructor
072         */
073        public OAuth2ClientDetailsJson(OAuth2ClientDetailsJson theClientDetailsJson) {
074                super(theClientDetailsJson);
075                myModuleId = theClientDetailsJson.myModuleId;
076                myNodeId = theClientDetailsJson.myNodeId;
077        }
078
079        @Override
080        public String getModuleId() {
081                return myModuleId;
082        }
083
084        public void setModuleId(String theModuleId) {
085                myModuleId = theModuleId;
086        }
087
088        @Override
089        public String getNodeId() {
090                return myNodeId;
091        }
092
093        public void setNodeId(String theNodeId) {
094                myNodeId = theNodeId;
095        }
096
097        @Override
098        public OAuth2ClientDetailsJson setPid(Long myPid) {
099                super.setPid(myPid);
100                return this;
101        }
102
103        @Override
104        public Map<String, Object> getClientPopulatedUserData(boolean theCreateIfNull) {
105                if (theCreateIfNull && myUserData == null) {
106                        myUserData = new HashMap<>();
107                }
108                return myUserData;
109        }
110
111        public String toJsonString() {
112                return JsonUtil.serialize(this);
113        }
114}