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.oauth2.sdk.ciba;
019
020import net.minidev.json.JSONObject;
021
022import com.nimbusds.oauth2.sdk.ParseException;
023import com.nimbusds.oauth2.sdk.Response;
024import com.nimbusds.oauth2.sdk.http.HTTPResponse;
025
026/**
027 * CIBA response from an OpenID provider / OAuth 2.0 authorisation server
028 * backend authentication endpoint.
029 *
030 * <p>Related specifications:
031 *
032 * <ul>
033 *      <li>OpenID Connect CIBA Flow - Core 1.0, section 7.3 and 13.
034 * </ul>
035 */
036public abstract class CIBAResponse implements Response {
037
038        
039        /**
040         * Casts this response to a successful CIBA request acknowledgement.
041         *
042         * @return The CIBA request acknowledgement.
043         */
044        public CIBARequestAcknowledgement toRequestAcknowledgement() {
045
046                return (CIBARequestAcknowledgement) this;
047        }
048        
049
050        /**
051         * Casts this response to a CIBA error response.
052         *
053         * @return The CIBA error response.
054         */
055        public CIBAErrorResponse toErrorResponse() {
056
057                return (CIBAErrorResponse) this;
058        }
059        
060
061        /**
062         * Parses a CIBA response from the specified JSON object.
063         *
064         * @param jsonObject The JSON object to parse. Must not be
065         *                   {@code null}.
066         *
067         * @return The CIBA response.
068         *
069         * @throws ParseException If parsing failed.
070         */
071        public static CIBAResponse parse(final JSONObject jsonObject)
072                throws ParseException {
073                
074                if (jsonObject.containsKey("auth_req_id")) {
075                        return CIBARequestAcknowledgement.parse(jsonObject);
076                } else {
077                        return CIBAErrorResponse.parse(jsonObject);
078                }
079        }
080
081        
082        /**
083         * Parses a CIBA response from the specified HTTP response.
084         *
085         * @param httpResponse The HTTP response. Must not be {@code null}.
086         *
087         * @return The CIBA response.
088         *
089         * @throws ParseException If parsing failed.
090         */
091        public static CIBAResponse parse(final HTTPResponse httpResponse)
092                throws ParseException {
093
094                if (httpResponse.getStatusCode() == HTTPResponse.SC_OK)
095                        return CIBARequestAcknowledgement.parse(httpResponse);
096                else
097                        return CIBAErrorResponse.parse(httpResponse);
098        }
099}