001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2021, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.openid.connect.sdk.assurance;
019
020
021import java.util.Objects;
022
023import net.jcip.annotations.Immutable;
024import net.minidev.json.JSONObject;
025
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028import com.nimbusds.oauth2.sdk.util.StringUtils;
029
030
031/**
032 * Identity assurance process.
033 */
034@Immutable
035public final class IdentityAssuranceProcess {
036        
037        
038        /**
039         * The policy.
040         */
041        private final Policy policy;
042        
043        
044        /**
045         * The procedure.
046         */
047        private final Procedure procedure;
048        
049        
050        /**
051         * The status.
052         */
053        private final Status status;
054        
055        
056        /**
057         * Creates a new identity assurance process. At least one assurance
058         * process element must be specified.
059         *
060         * @param policy    The policy, {@code null} if not specified.
061         * @param procedure The procedure, {@code null} if not specified.
062         * @param status    The status, {@code null} if not specified.
063         */
064        public IdentityAssuranceProcess(final Policy policy,
065                                        final Procedure procedure,
066                                        final Status status) {
067                
068                if (policy == null && procedure == null && status == null) {
069                        throw new IllegalArgumentException("At least one assurance process element must be specified");
070                }
071                
072                this.policy = policy;
073                this.procedure = procedure;
074                this.status = status;
075        }
076        
077        
078        /**
079         * Returns the policy.
080         *
081         * @return The policy, {@code null} if not specified.
082         */
083        public Policy getPolicy() {
084                return policy;
085        }
086        
087        
088        /**
089         * Returns the procedure.
090         *
091         * @return The procedure, {@code null} if not specified.
092         */
093        public Procedure getProcedure() {
094                return procedure;
095        }
096        
097        
098        /**
099         * Returns the status.
100         *
101         * @return The status, {@code null} if not specified.
102         */
103        public Status getStatus() {
104                return status;
105        }
106        
107        
108        @Override
109        public boolean equals(Object o) {
110                if (this == o) return true;
111                if (!(o instanceof IdentityAssuranceProcess)) return false;
112                IdentityAssuranceProcess that = (IdentityAssuranceProcess) o;
113                return Objects.equals(getPolicy(), that.getPolicy()) && Objects.equals(getProcedure(), that.getProcedure()) && Objects.equals(getStatus(), that.getStatus());
114        }
115        
116        
117        @Override
118        public int hashCode() {
119                return Objects.hash(getPolicy(), getProcedure(), getStatus());
120        }
121        
122        
123        /**
124         * Returns a JSON object representation of this identity assurance
125         * process.
126         *
127         * @return The JSON object.
128         */
129        public JSONObject toJSONObject() {
130                
131                JSONObject o = new JSONObject();
132                if (policy != null) {
133                        o.put("policy", policy.getValue());
134                }
135                if (procedure != null) {
136                        o.put("procedure", procedure.getValue());
137                }
138                if (status != null) {
139                        o.put("status", status.getValue());
140                }
141                return o;
142        }
143        
144        
145        /**
146         * Parses an identity assurance process from the specified JSON object.
147         *
148         * @param jsonObject The JSON object. Must not be {@code null}.
149         *
150         * @return The identity assurance process.
151         *
152         * @throws ParseException If parsing failed.
153         */
154        public static IdentityAssuranceProcess parse(final JSONObject jsonObject)
155                throws ParseException {
156                
157                Policy policy = null;
158                String value = JSONObjectUtils.getString(jsonObject, "policy", null);
159                if (StringUtils.isNotBlank(value)) {
160                        policy = new Policy(value);
161                }
162                
163                Procedure procedure = null;
164                value = JSONObjectUtils.getString(jsonObject, "procedure", null);
165                if (StringUtils.isNotBlank(value)) {
166                        procedure = new Procedure(value);
167                }
168                
169                Status status = null;
170                value = JSONObjectUtils.getString(jsonObject, "status", null);
171                if (StringUtils.isNotBlank(value)) {
172                        status = new Status(value);
173                }
174                
175                try {
176                        return new IdentityAssuranceProcess(policy, procedure, status);
177                } catch (IllegalArgumentException e) {
178                        throw new ParseException(e.getMessage());
179                }
180        }
181}