001package com.nimbusds.openid.connect.provider.spi.grants;
002
003
004import java.util.List;
005
006import com.nimbusds.oauth2.sdk.ParseException;
007import com.nimbusds.oauth2.sdk.id.Audience;
008import com.nimbusds.oauth2.sdk.id.Subject;
009import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
010import net.jcip.annotations.Immutable;
011import net.minidev.json.JSONObject;
012
013
014/**
015 * Optional token specification.
016 */
017@Immutable
018public class OptionalTokenSpec extends TokenSpec {
019
020
021        /**
022         * Specifies if a token is to be issued. If {@code true} a token must
023         * be issued, {@code false} to prohibit issue.
024         */
025        private final boolean issue;
026
027
028        /**
029         * Creates a new optional token specification.
030         *
031         * @param issue               If {@code true} a token must be issued,
032         *                            {@code false} to prohibit issue.
033         * @param lifetime            The token lifetime, in seconds. Zero
034         *                            implies permanent or not specified (to
035         *                            apply the default configured token
036         *                            lifetime), depending on the token type.
037         *                            Must not be negative integer.
038         * @param audList             Explicit list of audiences for the token,
039         *                            {@code null} if not specified.
040         * @param impersonatedSubject The subject in impersonation and
041         *                            delegation cases, {@code null} if not
042         *                            applicable.
043         */
044        public OptionalTokenSpec(final boolean issue,
045                                 final long lifetime,
046                                 final List<Audience> audList,
047                                 final Subject impersonatedSubject) {
048
049                super(lifetime, audList, impersonatedSubject);
050
051                this.issue = issue;
052        }
053
054
055        /**
056         * Returns the token issue policy.
057         *
058         * @return {@code true} if a token must be issued, {@code false} to
059         *         prohibit issue.
060         */
061        public boolean issue() {
062
063                return issue;
064        }
065
066
067        @Override
068        public JSONObject toJSONObject() {
069
070                JSONObject o = super.toJSONObject();
071                o.put("issue", issue);
072                return o;
073        }
074
075
076        /**
077         * Parses an optional token specification from the specified JSON
078         * object.
079         *
080         * @param jsonObject The JSON object. Must not be {@code null}.
081         *
082         * @return The optional token specification.
083         *
084         * @throws ParseException If parsing failed.
085         */
086        public static OptionalTokenSpec parse(final JSONObject jsonObject)
087                throws ParseException {
088
089                TokenSpec tokenSpec = TokenSpec.parse(jsonObject);
090
091                boolean issue = false;
092
093                if (jsonObject.containsKey("issue")) {
094                        issue = JSONObjectUtils.getBoolean(jsonObject, "issue");
095                }
096
097                return new OptionalTokenSpec(issue, tokenSpec.getLifetime(), tokenSpec.getAudience(), tokenSpec.getImpersonatedSubject());
098        }
099}