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.oauth2.sdk.cnf;
019
020
021import java.util.Map;
022
023import net.minidev.json.JSONObject;
024
025import com.nimbusds.jwt.JWTClaimsSet;
026import com.nimbusds.oauth2.sdk.ParseException;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028
029
030/**
031 * Abstract confirmation.
032 */
033public abstract class AbstractConfirmation {
034        
035        
036        
037        /**
038         * Returns this confirmation as a JWT claim.
039         *
040         * <p>Example:
041         *
042         * <pre>
043         * "cnf" : { "x5t#S256" : "bwcK0esc3ACC3DB2Y5_lESsXE8o9ltc05O89jdN-dg2" }
044         * </pre>
045         *
046         * @return The JWT claim name / value.
047         */
048        public abstract Map.Entry<String,JSONObject> toJWTClaim();
049        
050        
051        
052        /**
053         * Returns this X.509 certificate SHA-256 confirmation as a JSON
054         * object.
055         *
056         * <p>Example:
057         *
058         * <pre>
059         * {
060         *   "cnf" : { "x5t#S256" : "bwcK0esc3ACC3DB2Y5_lESsXE8o9ltc05O89jdN-dg2" }
061         * }
062         * </pre>
063         *
064         * @return The JSON object.
065         */
066        public JSONObject toJSONObject() {
067                
068                JSONObject jsonObject = new JSONObject();
069                Map.Entry<String, JSONObject> cnfClaim = toJWTClaim();
070                jsonObject.put(cnfClaim.getKey(), cnfClaim.getValue());
071                return jsonObject;
072        }
073        
074        
075        /**
076         * Merges this X.509 certificate SHA-256 confirmation into the
077         * specified JSON object. Any existing {@code cnf} JSON object values
078         * will be preserved.
079         *
080         * <p>Example:
081         *
082         * <pre>
083         * {
084         *   "cnf" : { "x5t#S256" : "bwcK0esc3ACC3DB2Y5_lESsXE8o9ltc05O89jdN-dg2" }
085         * }
086         * </pre>
087         */
088        public void mergeInto(final JSONObject jsonObject) {
089                
090                JSONObject cnf = new JSONObject();
091                if (jsonObject.get("cnf") != null) {
092                        try {
093                                cnf = JSONObjectUtils.getJSONObject(jsonObject, "cnf");
094                        } catch (ParseException e) {
095                                // ignore
096                        }
097                }
098                Map.Entry<String, JSONObject> en = toJWTClaim();
099                cnf.putAll(en.getValue());
100                jsonObject.put("cnf", cnf);
101        }
102        
103        
104        /**
105         * Applies this confirmation to the specified JWT claims set.
106         *
107         * @param jwtClaimsSet The JWT claims set.
108         *
109         * @return The modified JWT claims set.
110         */
111        public JWTClaimsSet applyTo(final JWTClaimsSet jwtClaimsSet) {
112                
113                Map.Entry<String, JSONObject> cnfClaim = toJWTClaim();
114                
115                return new JWTClaimsSet.Builder(jwtClaimsSet)
116                        .claim(cnfClaim.getKey(), cnfClaim.getValue())
117                        .build();
118        }
119        
120        
121        @Override
122        public String toString() {
123                return toJSONObject().toJSONString();
124        }
125        
126        
127        /**
128         * Parses a confirmation JSON object from the specified JWT claims set.
129         *
130         * @param jwtClaimsSet The JWT claims set.
131         *
132         * @return The confirmation JSON object, {@code null} if none.
133         */
134        protected static JSONObject parseConfirmationJSONObject(final JWTClaimsSet jwtClaimsSet) {
135                
136                Map<String, Object> jsonObjectClaim;
137                try {
138                        jsonObjectClaim = jwtClaimsSet.getJSONObjectClaim("cnf");
139                } catch (java.text.ParseException e) {
140                        return null;
141                }
142                
143                if (jsonObjectClaim == null) {
144                        return null;
145                }
146                
147                return new JSONObject(jsonObjectClaim);
148        }
149}