001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.openid.connect.sdk.op;
019
020
021import com.nimbusds.oauth2.sdk.ErrorObject;
022import com.nimbusds.oauth2.sdk.GeneralException;
023import com.nimbusds.openid.connect.sdk.AuthenticationRequest;
024import com.nimbusds.openid.connect.sdk.OIDCError;
025
026
027/**
028 * Resolve exception.
029 */
030public class ResolveException extends GeneralException {
031
032
033        /**
034         * Creates a new resolve exception.
035         *
036         * @param error       The associated OpenID Connect / OAuth 2.0 error.
037         *                    Must not be {@code null}.
038         * @param authRequest The associated OpenID Connect authentication
039         *                    request. Must not be {@code null}.
040         */
041        public ResolveException(final ErrorObject error, final AuthenticationRequest authRequest) {
042
043                super(error.getDescription(),
044                        error,
045                        authRequest.getClientID(),
046                        authRequest.getRedirectionURI(),
047                        authRequest.getResponseMode(),
048                        authRequest.getState(),
049                        null);
050        }
051
052
053        /**
054         * Creates a new resolve exception. The error code is set to
055         * {@link OIDCError#INVALID_REQUEST_URI} or
056         * {@link OIDCError#INVALID_REQUEST_OBJECT} depending on the request
057         * type.
058         *
059         * @param exMessage     The original exception message (to be logged).
060         *                      May be {@code null}.
061         * @param clientMessage The message to pass back to the client in the
062         *                      {@code error_description} of the error code,
063         *                      {@code null} to use the default one.
064         * @param authRequest   The associated OpenID Connect authentication
065         *                      request, used to determine the error object.
066         *                      Must not be {@code null}.
067         * @param cause         The exception cause, {@code null} if not
068         *                      specified.
069         */
070        public ResolveException(final String exMessage,
071                                final String clientMessage,
072                                final AuthenticationRequest authRequest,
073                                final Throwable cause) {
074
075                super(exMessage,
076                        resolveErrorObject(clientMessage, authRequest),
077                        authRequest.getClientID(),
078                        authRequest.getRedirectionURI(),
079                        authRequest.getResponseMode(),
080                        authRequest.getState(),
081                        cause);
082        }
083
084
085        /**
086         * Resolves the error object ({@code invalid_request_uri} or
087         * {@code invalid_request_object}) for the specified OpenID
088         * authentication request.
089         *
090         * @param clientMessage The message to pass back to the client in the
091         *                      {@code error_description} of the error code,
092         *                      {@code null} to use the default one.
093         * @param authRequest   The associated OpenID Connect authentication
094         *                      request, used to determine the error object.
095         *                      Must not be {@code null}.
096         *
097         * @return The error object.
098         */
099        private static ErrorObject resolveErrorObject(final String clientMessage,
100                                                      final AuthenticationRequest authRequest) {
101
102                ErrorObject errorObject;
103
104                if (authRequest.getRequestURI() != null) {
105                        errorObject = OIDCError.INVALID_REQUEST_URI;
106                } else {
107                        errorObject = OIDCError.INVALID_REQUEST_OBJECT;
108                }
109
110                if (clientMessage != null) {
111                        return errorObject.setDescription(clientMessage);
112                }
113
114                return errorObject;
115        }
116}