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