001    package com.nimbusds.openid.connect.sdk;
002    
003    
004    import net.jcip.annotations.Immutable;
005    
006    import net.minidev.json.JSONObject;
007    
008    import com.nimbusds.oauth2.sdk.ErrorObject;
009    import com.nimbusds.oauth2.sdk.ParseException;
010    import com.nimbusds.oauth2.sdk.TokenErrorResponse;
011    import com.nimbusds.oauth2.sdk.http.HTTPResponse;
012    
013    
014    /**
015     * OpenID Connect token error response. This class is immutable.
016     *
017     * <p>Standard token errors:
018     *
019     * <ul>
020     *     <li>{@link com.nimbusds.oauth2.sdk.OAuth2Error#INVALID_REQUEST}
021     *     <li>{@link com.nimbusds.oauth2.sdk.OAuth2Error#INVALID_CLIENT}
022     *     <li>{@link com.nimbusds.oauth2.sdk.OAuth2Error#INVALID_GRANT}
023     *     <li>{@link com.nimbusds.oauth2.sdk.OAuth2Error#UNAUTHORIZED_CLIENT}
024     *     <li>{@link com.nimbusds.oauth2.sdk.OAuth2Error#UNSUPPORTED_GRANT_TYPE}
025     *     <li>{@link com.nimbusds.oauth2.sdk.OAuth2Error#INVALID_SCOPE}
026     * </ul>
027     *
028     * <p>Example HTTP response:
029     *
030     * <pre>
031     * HTTP/1.1 400 Bad Request
032     * Content-Type: application/json
033     * Cache-Control: no-store
034     * Pragma: no-cache
035     * 
036     * {
037     *  "error": "invalid_request"
038     * }
039     * </pre>
040     *
041     * <p>Related specifications:
042     *
043     * <ul>
044     *     <li>OpenID Connect Messages 1.0, section 2.1.3.
045     *     <li>OpenID Connect Standard 1.0, section 2.3.5.2.
046     * </ul>
047     *
048     * @author Vladimir Dzhuvinov
049     */
050    @Immutable
051    public class OIDCTokenErrorResponse 
052            extends TokenErrorResponse
053            implements OIDCTokenResponse {
054    
055    
056            /**
057             * Creates a new OpenID Connect token error response. No OAuth 2.0 
058             * error is specified.
059             */
060            private OIDCTokenErrorResponse() {
061    
062                    super();
063            }
064    
065    
066            /**
067             * Creates a new OpenID Connect token error response.
068             *
069             * @param error The error. Should match one of the 
070             *              {@link #getStandardErrors standard errors} for a token 
071             *              error response. Must not be {@code null}.
072             */
073            public OIDCTokenErrorResponse(final ErrorObject error) {
074            
075                    super(error);
076            }
077    
078    
079            /**
080             * Parses an OpenID Connect token error response from the specified 
081             * JSON object.
082             *
083             * @param jsonObject The JSON object to parse. Its status code must not
084             *                   be 200 (OK). Must not be {@code null}.
085             *
086             * @throws ParseException If the JSON object couldn't be parsed to an 
087             *                        OpenID Connect token error response.
088             */
089            public static OIDCTokenErrorResponse parse(final JSONObject jsonObject)
090                    throws ParseException {
091    
092                    ErrorObject error = TokenErrorResponse.parse(jsonObject).getErrorObject();
093    
094                    if (error != null) {
095    
096                            return new OIDCTokenErrorResponse(error);
097    
098                    } else {
099    
100                            return new OIDCTokenErrorResponse();
101                    }
102            }
103    
104    
105            /**
106             * Parses an OpenID Connect token error response from the specified 
107             * HTTP response.
108             *
109             * @param httpResponse The HTTP response to parse. Its status code must
110             *                     not be 200 (OK). Must not be {@code null}.
111             *
112             * @throws ParseException If the JSON object couldn't be parsed to an 
113             *                        OpenID Connect token error response.
114             */
115            public static OIDCTokenErrorResponse parse(final HTTPResponse httpResponse)
116                    throws ParseException {
117    
118                    ErrorObject error = TokenErrorResponse.parse(httpResponse).getErrorObject();
119    
120                    if (error != null) {
121    
122                            return new OIDCTokenErrorResponse(error);
123    
124                    } else {
125                            
126                            return new OIDCTokenErrorResponse();
127                    }
128            }
129    }