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.oauth2.sdk.OAuth2Error;
024import com.nimbusds.openid.connect.sdk.AuthenticationRequest;
025
026
027/**
028 * Resolve exception.
029 */
030public class ResolveException extends GeneralException {
031        
032        
033        private static final long serialVersionUID = 1014725420232885505L;
034        
035        
036        /**
037         * Creates a new resolve exception.
038         *
039         * @param error       The associated OpenID Connect / OAuth 2.0 error.
040         *                    Must not be {@code null}.
041         * @param authRequest The associated OpenID Connect authentication
042         *                    request. Must not be {@code null}.
043         */
044        public ResolveException(final ErrorObject error, final AuthenticationRequest authRequest) {
045
046                super(error.getDescription(),
047                        error,
048                        authRequest.getClientID(),
049                        authRequest.getRedirectionURI(),
050                        authRequest.getResponseMode(),
051                        authRequest.getState(),
052                        null);
053        }
054
055
056        /**
057         * Creates a new resolve exception. The error code is set to
058         * {@link OAuth2Error#INVALID_REQUEST_URI} or
059         * {@link OAuth2Error#INVALID_REQUEST_OBJECT} depending on the request
060         * type.
061         *
062         * @param exMessage     The original exception message (to be logged).
063         *                      May be {@code null}.
064         * @param clientMessage The message to pass back to the client in the
065         *                      {@code error_description} of the error code,
066         *                      {@code null} to use the default one.
067         * @param authRequest   The associated OpenID Connect authentication
068         *                      request, used to determine the error object.
069         *                      Must not be {@code null}.
070         * @param cause         The exception cause, {@code null} if not
071         *                      specified.
072         */
073        public ResolveException(final String exMessage,
074                                final String clientMessage,
075                                final AuthenticationRequest authRequest,
076                                final Throwable cause) {
077
078                super(exMessage,
079                        resolveErrorObject(clientMessage, authRequest),
080                        authRequest.getClientID(),
081                        authRequest.getRedirectionURI(),
082                        authRequest.getResponseMode(),
083                        authRequest.getState(),
084                        cause);
085        }
086
087
088        /**
089         * Resolves the error object ({@code invalid_request_uri} or
090         * {@code invalid_request_object}) for the specified OpenID
091         * authentication request.
092         *
093         * @param clientMessage The message to pass back to the client in the
094         *                      {@code error_description} of the error code,
095         *                      {@code null} to use the default one.
096         * @param authRequest   The associated OpenID Connect authentication
097         *                      request, used to determine the error object.
098         *                      Must not be {@code null}.
099         *
100         * @return The error object.
101         */
102        private static ErrorObject resolveErrorObject(final String clientMessage,
103                                                      final AuthenticationRequest authRequest) {
104
105                ErrorObject errorObject;
106
107                if (authRequest.getRequestURI() != null) {
108                        errorObject = OAuth2Error.INVALID_REQUEST_URI;
109                } else {
110                        errorObject = OAuth2Error.INVALID_REQUEST_OBJECT;
111                }
112
113                if (clientMessage != null) {
114                        return errorObject.setDescription(clientMessage);
115                }
116
117                return errorObject;
118        }
119}