001package com.nimbusds.openid.connect.provider.spi.reg;
002
003
004import org.checkerframework.checker.nullness.qual.Nullable;
005
006import com.nimbusds.oauth2.sdk.ErrorObject;
007import com.nimbusds.oauth2.sdk.client.RegistrationError;
008
009
010/**
011 * Invalid client registration exception.
012 */
013public class InvalidRegistrationException extends Exception {
014
015
016        /**
017         * The error object.
018         */
019        private final ErrorObject errorObject;
020        
021        
022        /**
023         * Creates a new invalid client registration exception with a general
024         * {@code invalid_client_metadata} error code and description that
025         * doesn't specify the cause.
026         *
027         * <p>This will result in the following error response:
028         *
029         * <pre>
030         * HTTP/1.1 400 Bad Request
031         * Content-Type: application/json
032         * Cache-Control: no-store
033         * Pragma: no-cache
034         *
035         * {
036         *   "error"             : "invalid_client_metadata",
037         *   "error_description" : "Invalid client metadata field"
038         * }
039         * </pre>
040         */
041        public InvalidRegistrationException() {
042                
043                this(RegistrationError.INVALID_CLIENT_METADATA);
044        }
045
046
047        /**
048         * Creates a new invalid client registration exception with the
049         * specified error code and description.
050         *
051         * <p>The error code should be one of the following:
052         *
053         * <ul>
054         *     <li>{@link RegistrationError#INVALID_REDIRECT_URI invalid_redirect_uri}</li>
055         *     <li>{@link RegistrationError#INVALID_CLIENT_METADATA invalid_client_metadata}</li>
056         *     <li>{@link RegistrationError#INVALID_SOFTWARE_STATEMENT invalid_software_statement}</li>
057         *     <li>{@link RegistrationError#UNAPPROVED_SOFTWARE_STATEMENT unapproved_software_statement}</li>
058         * </ul>
059         *
060         * <p>To construct an exception for a general
061         * {@code invalid_client_metadata} error with a description:
062         *
063         * <pre>
064         * new InvalidRegistrationException(RegistrationError.INVALID_CLIENT_METADATA
065         *      .setDescription("The policy_uri must be on a redirect_uris domain"));
066         * </pre>
067         *
068         * <p>This will result in the following error response:
069         *
070         * <pre>
071         * HTTP/1.1 400 Bad Request
072         * Content-Type: application/json
073         * Cache-Control: no-store
074         * Pragma: no-cache
075         *
076         * {
077         *   "error"             : "invalid_client_metadata",
078         *   "error_description" : "The policy_uri must be on a redirect_uris domain"
079         * }
080         * </pre>
081         *
082         * @param errorObject The associated error object. If {@code null} will
083         *                    be set to {@code invalid_client_metadata}.
084         */
085        public InvalidRegistrationException(final @Nullable ErrorObject errorObject) {
086
087                if (errorObject == null)
088                        this.errorObject = RegistrationError.INVALID_CLIENT_METADATA;
089                else
090                        this.errorObject = errorObject;
091        }
092
093
094        /**
095         * Creates a new invalid client registration exception, with the error
096         * code set to {@code invalid_client_metadata} and a description
097         * specifying the name of the invalid field and cause.
098         *
099         * <p>Example:
100         *
101         * <pre>
102         * new InvalidRegistrationException("policy_uri", "Must be on a redirect_uris domain");
103         * </pre>
104         *
105         * <p>This will result in the following error response:
106         *
107         * <pre>
108         * HTTP/1.1 400 Bad Request
109         * Content-Type: application/json
110         * Cache-Control: no-store
111         * Pragma: no-cache
112         *
113         * {
114         *   "error"             : "invalid_client_metadata",
115         *   "error_description" : "Invalid client metadata field policy_uri: Must be on a redirect_uris domain"
116         * }
117         * </pre>
118         *
119         * @param field The name of the invalid client metadata field. Must not
120         *              be {@code null}.
121         * @param cause The cause, {@code null} if not specified.
122         */
123        public InvalidRegistrationException(final String field, final @Nullable String cause) {
124
125                errorObject = RegistrationError.INVALID_CLIENT_METADATA
126                        .setDescription("Invalid client metadata field " + field +
127                                ((cause != null) ? ": " + cause : ""));
128        }
129
130
131        /**
132         * Returns the associated error object.
133         *
134         * @return The associated error object.
135         */
136        public ErrorObject getErrorObject() {
137
138                return errorObject;
139        }
140}