001package com.nimbusds.openid.connect.sdk.op;
002
003
004import com.nimbusds.oauth2.sdk.ErrorObject;
005import com.nimbusds.oauth2.sdk.GeneralException;
006import com.nimbusds.openid.connect.sdk.AuthenticationRequest;
007import com.nimbusds.openid.connect.sdk.OIDCError;
008
009
010/**
011 * Resolve exception.
012 */
013public class ResolveException extends GeneralException {
014
015
016        /**
017         * Creates a new resolve exception.
018         *
019         * @param error       The associated OpenID Connect / OAuth 2.0 error.
020         *                    Must not be {@code null}.
021         * @param authRequest The associated OpenID Connect authentication
022         *                    request. Must not be {@code null}.
023         */
024        public ResolveException(final ErrorObject error, final AuthenticationRequest authRequest) {
025
026                super(error.getDescription(),
027                        error,
028                        authRequest.getClientID(),
029                        authRequest.getRedirectionURI(),
030                        authRequest.getResponseMode(),
031                        authRequest.getState(),
032                        null);
033        }
034
035
036        /**
037         * Creates a new resolve exception. The error code is set to
038         * {@link OIDCError#INVALID_REQUEST_URI} or
039         * {@link OIDCError#INVALID_REQUEST_OBJECT} depending on the request
040         * type.
041         *
042         * @param exMessage     The original exception message (to be logged).
043         *                      May be {@code null}.
044         * @param clientMessage The message to pass back to the client in the
045         *                      {@code error_description} of the error code,
046         *                      {@code null} to use the default one.
047         * @param authRequest   The associated OpenID Connect authentication
048         *                      request, used to determine the error object.
049         *                      Must not be {@code null}.
050         * @param cause         The exception cause, {@code null} if not
051         *                      specified.
052         */
053        public ResolveException(final String exMessage,
054                                final String clientMessage,
055                                final AuthenticationRequest authRequest,
056                                final Throwable cause) {
057
058                super(exMessage,
059                        resolveErrorObject(clientMessage, authRequest),
060                        authRequest.getClientID(),
061                        authRequest.getRedirectionURI(),
062                        authRequest.getResponseMode(),
063                        authRequest.getState(),
064                        cause);
065        }
066
067
068        /**
069         * Resolves the error object ({@code invalid_request_uri} or
070         * {@code invalid_request_object}) for the specified OpenID
071         * authentication request.
072         *
073         * @param clientMessage The message to pass back to the client in the
074         *                      {@code error_description} of the error code,
075         *                      {@code null} to use the default one.
076         * @param authRequest   The associated OpenID Connect authentication
077         *                      request, used to determine the error object.
078         *                      Must not be {@code null}.
079         *
080         * @return The error object.
081         */
082        private static ErrorObject resolveErrorObject(final String clientMessage,
083                                                      final AuthenticationRequest authRequest) {
084
085                ErrorObject errorObject;
086
087                if (authRequest.getRequestURI() != null) {
088                        errorObject = OIDCError.INVALID_REQUEST_URI;
089                } else {
090                        errorObject = OIDCError.INVALID_REQUEST_OBJECT;
091                }
092
093                if (clientMessage != null) {
094                        return errorObject.setDescription(clientMessage);
095                }
096
097                return errorObject;
098        }
099}