001package com.nimbusds.openid.connect.provider.spi.grants;
002
003
004import net.minidev.json.JSONObject;
005
006import org.apache.commons.collections4.MapUtils;
007
008import com.nimbusds.oauth2.sdk.ParseException;
009import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
010
011
012/**
013 * Additional or preset OpenID Connect claims. These may be included in the ID
014 * token or in the UserInfo response.
015 */
016public final class PresetClaims {
017
018
019        /**
020         * Additional or preset claims to be included in the ID token,
021         * {@code null} if none.
022         */
023        private final JSONObject idTokenClaims;
024
025
026        /**
027         * Additional or preset claims to be included in the UserInfo response,
028         * {@code null} if none.
029         */
030        private final JSONObject userInfoClaims;
031
032
033        /**
034         * Creates a new empty preset claims instance.
035         */
036        public PresetClaims() {
037
038                this(null, null);
039        }
040
041
042        /**
043         * Creates a new preset claims instance.
044         *
045         * @param idTokenClaims  Additional or preset claims to be included in
046         *                       the ID token, {@code null} if none.
047         * @param userInfoClaims Additional or preset claims to be included in
048         *                       the UserInfo response, {@code null} if none.
049         */
050        public PresetClaims(final JSONObject idTokenClaims,
051                            final JSONObject userInfoClaims) {
052
053                this.idTokenClaims = idTokenClaims;
054                this.userInfoClaims = userInfoClaims;
055        }
056
057
058        /**
059         * Returns {@code true} if there are no preset claims specified.
060         *
061         * @return {@code true} if there are no preset claims specified, else
062         *         {@code false}.
063         */
064        public boolean isEmpty() {
065
066                return MapUtils.isEmpty(idTokenClaims) && MapUtils.isEmpty(userInfoClaims);
067        }
068
069
070        /**
071         * Returns the additional or preset claims to be included in the ID
072         * token.
073         *
074         * @return The preset ID token claims, {@code null} if none.
075         */
076        public JSONObject getPresetIDTokenClaims() {
077                return idTokenClaims;
078        }
079
080
081        /**
082         * Returns the additional or preset claims to be returned in the
083         * UserInfo response.
084         *
085         * @return The preset UserInfo claims, {@code null} if none.
086         */
087        public JSONObject getPresetUserInfoClaims() {
088                return userInfoClaims;
089        }
090
091
092        /**
093         * Returns a JSON object representation of this preset claims instance.
094         *
095         * @return The JSON object.
096         */
097        public JSONObject toJSONObject() {
098
099                JSONObject o = new JSONObject();
100
101                if (MapUtils.isNotEmpty(idTokenClaims)) {
102                        o.put("id_token", idTokenClaims);
103                }
104
105                if (MapUtils.isNotEmpty(userInfoClaims)) {
106                        o.put("userinfo", userInfoClaims);
107                }
108
109                return o;
110        }
111
112
113        @Override
114        public String toString() {
115
116                return toJSONObject().toString();
117        }
118
119
120        /**
121         * Parses a preset claims representation from the specified JSON
122         * object.
123         *
124         * @param o The JSON object. Must not be {@code null}.
125         *
126         * @return The preset claims.
127         *
128         * @throws ParseException If parsing failed.
129         */
130        public static PresetClaims parse(final JSONObject o)
131                throws ParseException {
132
133                JSONObject idTokenClaims = null;
134
135                if (o.containsKey("id_token")) {
136                        idTokenClaims = JSONObjectUtils.getJSONObject(o, "id_token");
137                }
138
139                JSONObject userInfoClaims = null;
140
141                if (o.containsKey("userinfo")) {
142                        userInfoClaims = JSONObjectUtils.getJSONObject(o, "userinfo");
143                }
144
145                return new PresetClaims(idTokenClaims, userInfoClaims);
146        }
147}