001package com.nimbusds.openid.connect.provider.spi.grants;
002
003
004import net.minidev.json.JSONObject;
005
006import com.nimbusds.oauth2.sdk.ParseException;
007import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
008import com.nimbusds.oauth2.sdk.util.MapUtils;
009
010
011/**
012 * Additional or preset OpenID Connect claims. These may be included in the ID
013 * token or in the UserInfo response.
014 */
015public final class PresetClaims {
016
017
018        /**
019         * Additional or preset claims to be included in the ID token,
020         * {@code null} if none.
021         */
022        private final JSONObject idTokenClaims;
023
024
025        /**
026         * Additional or preset claims to be included in the UserInfo response,
027         * {@code null} if none.
028         */
029        private final JSONObject userInfoClaims;
030
031
032        /**
033         * Creates a new empty preset claims instance.
034         */
035        public PresetClaims() {
036
037                this(null, null);
038        }
039
040
041        /**
042         * Creates a new preset claims instance.
043         *
044         * @param idTokenClaims  Additional or preset claims to be included in
045         *                       the ID token, {@code null} if none.
046         * @param userInfoClaims Additional or preset claims to be included in
047         *                       the UserInfo response, {@code null} if none.
048         */
049        public PresetClaims(final JSONObject idTokenClaims,
050                            final JSONObject userInfoClaims) {
051
052                this.idTokenClaims = idTokenClaims;
053                this.userInfoClaims = userInfoClaims;
054        }
055
056
057        /**
058         * Returns {@code true} if there are no preset claims specified.
059         *
060         * @return {@code true} if there are no preset claims specified, else
061         *         {@code false}.
062         */
063        public boolean isEmpty() {
064
065                return
066                        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}