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