001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/ds/resource/legal-notices/cddl.txt
011     * or http://www.opensource.org/licenses/cddl1.php.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/ds/resource/legal-notices/cddl.txt.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2013 UnboundID Corp.
026     */
027    package com.unboundid.directory.sdk.broker.types;
028    
029    import com.unboundid.util.Extensible;
030    
031    
032    /**
033     * This class provides a type of {@link Exception} which is used to indicate
034     * an error when interfacing with an OAuth 2 extension.
035     */
036    @Extensible()
037    public class OAuthException extends Exception
038    {
039      private static final long serialVersionUID = 3679401569779402582L;
040    
041      /**
042       * The access_denied OAuth 2 error code. Used when the resource
043       * owner or authorization server denied the request.
044       */
045      public static final String ACCESS_DENIED = "access_denied";
046    
047      /**
048       * The consent_required OAuth 2 error code. Used when the
049       * Authorization Server requires End-User consent.
050       */
051      public static final String CONSENT_REQUIRED = "consent_required";
052    
053      /**
054       * The invalid_client OAuth 2 error code. Used when client authentication
055       * failed (e.g., unknown client, no client authentication included, or
056       * unsupported authentication method).
057       */
058      public static final String INVALID_CLIENT = "invalid_client";
059    
060      /**
061       * The invalid_grant OAuth 2 error code. Used when the provided
062       * authorization grant (e.g., authorization code, resource owner
063       * credentials) or refresh token is invalid, expired, revoked, does
064       * not match the redirection URI used in the authorization request,
065       * or was issued to another client.
066       */
067      public static final String INVALID_GRANT = "invalid_grant";
068    
069      /**
070       * The invalid_request OAuth 2 error code. Used when the request is
071       * missing a required parameter, includes an unsupported parameter
072       * value (other than grant type), repeats a parameter, includes
073       * multiple credentials, utilizes more than one mechanism for
074       * authenticating the client, or is otherwise malformed.
075       */
076      public static final String INVALID_REQUEST = "invalid_request";
077    
078      /**
079       * The invalid_scope OAuth 2 error code. Used when the requested
080       * scope is invalid, unknown, or malformed
081       */
082      public static final String INVALID_SCOPE = "invalid_scope";
083    
084      /**
085       * The invalid_token OAuth 2 error code. Used when the provided
086       * access or refresh token value is invalid.
087       */
088      public static final String INVALID_TOKEN = "invalid_token";
089    
090      /**
091       * The login_required OAuth 2 error code. Used when the Authorization
092       * Server requires End-User authentication.
093       */
094      public static final String LOGIN_REQUIRED = "login_required";
095    
096      /**
097       * The server_error OAuth 2 error code. Used when the authorization server
098       * encountered an unexpected condition that prevented it from fulfilling
099       * the request.
100       */
101      public static final String SERVER_ERROR = "server_error";
102    
103      /**
104       * The temporarily_unavailable OAuth 2 error code. Used when the
105       * authorization server is currently unable to handle the request due to
106       * a temporary overloading or maintenance of the server.
107       */
108      public static final String TEMPORARILY_UNAVAILABLE =
109          "temporarily_unavailable";
110    
111      /**
112       * The unauthorized_client OAuth 2 error code. Used when the authenticated
113       * client is not authorized to use this authorization grant type.
114       */
115            public static final String UNAUTHORIZED_CLIENT = "unauthorized_client";
116    
117      /**
118       * The unsupported_grant_type OAuth 2 error code. Used when the
119       * authorization grant type is not supported by the authorization
120       * server.
121       */
122            public static final String UNSUPPORTED_GRANT_TYPE =
123           "unsupported_grant_type";
124    
125      /**
126       * The unsupported_response_type OAuth 2 error code. Used when the
127       * authorization server does not support obtaining an authorization
128       * code using this method.
129       */
130            public static final String UNSUPPORTED_RESPONSE_TYPE =
131           "unsupported_response_type";
132    
133      private final String oauth2ErrorCode;
134      private final String errorUri;
135    
136      /**
137       * Creates a new OAuthException with the provided information.
138       *
139       * @param  oauth2ErrorCode   The OAuth 2 error code that should be returned
140       *                           to the client application.
141       */
142      public OAuthException(final String oauth2ErrorCode)
143      {
144        this(oauth2ErrorCode, null, null, null);
145      }
146    
147    
148    
149      /**
150       * Creates a new OAuthException with the provided information.
151       *
152       * @param  oauth2ErrorCode   The OAuth 2 error code that should be returned
153       *                           to the client application.
154       * @param  message           The message that explains the problem that
155       *                           occurred.
156       */
157      public OAuthException(final String oauth2ErrorCode, final String message)
158      {
159        this(oauth2ErrorCode, message, null, null);
160      }
161    
162    
163    
164      /**
165       * Creates a new OAuthException with the provided information.
166       *
167       * @param  oauth2ErrorCode   The OAuth 2 error code that should be returned
168       *                           to the client application.
169       * @param  cause             The underlying cause that triggered this
170       *                           exception.
171       */
172      public OAuthException(final String oauth2ErrorCode, final Throwable cause)
173      {
174        this(oauth2ErrorCode, null, null, cause);
175      }
176    
177    
178    
179      /**
180       * Creates a new OAuthException with the provided information.
181       *
182       * @param  oauth2ErrorCode   The OAuth 2 error code that should be returned
183       *                           to the client application.
184       * @param  message           The message that explains the problem that
185       *                           occurred.
186       * @param  errorUri          A URI identifying a web page with information
187       *                           about the error.
188       * @param  cause             The underlying cause that triggered this
189       *                           exception.
190       */
191      public OAuthException(final String oauth2ErrorCode,
192                            final String message,
193                            final String errorUri,
194                            final Throwable cause)
195      {
196        super(message, cause);
197        this.oauth2ErrorCode = oauth2ErrorCode;
198        this.errorUri = errorUri;
199        if(oauth2ErrorCode == null)
200        {
201          throw new NullPointerException("The OAuth2ErrorCode cannot be null.");
202        }
203      }
204    
205      /**
206       * Retrieves the OAuth 2 error code that should be returned to the client
207       * application.
208       *
209       * @return The OAuth 2 error code that should be returned to the client
210       *         application.
211       */
212      public String getOauth2ErrorCode()
213      {
214        return oauth2ErrorCode;
215      }
216    
217      /**
218       * Retrieves the URI identifying a web page with information about the error
219       * or {@code null} if not available.
220       *
221       * @return The URI identifying a web page with information about the error or
222       * {@code null} if not available.
223       */
224      public String getErrorUri()
225      {
226        return errorUri;
227      }
228    }