001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2021, 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
020
021import com.nimbusds.oauth2.sdk.ParseException;
022import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
023import com.nimbusds.oauth2.sdk.http.HTTPRequest;
024import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
025import net.minidev.json.JSONObject;
026
027import java.net.URI;
028import java.util.Objects;
029
030
031/**
032 * CIBA push callback to the client notification endpoint.
033 *
034 * <p>Related specifications:
035 *
036 * <ul>
037 *      <li>OpenID Connect CIBA Flow - Core 1.0
038 * </ul>
039 */
040public abstract class CIBAPushCallback extends ProtectedResourceRequest {
041        
042        
043        /**
044         * The CIBA request ID.
045         */
046        private final AuthRequestID authRequestID;
047        
048        
049        /**
050         * Creates a new CIBA push callback.
051         *
052         * @param endpoint      The client notification endpoint. Must not be
053         *                      {@code null}.
054         * @param accessToken   The client notification token. Must not be
055         *                      {@code null}.
056         * @param authRequestID The CIBA request ID. Must not be {@code null}.
057         */
058        public CIBAPushCallback(final URI endpoint,
059                                final BearerAccessToken accessToken,
060                                final AuthRequestID authRequestID) {
061                super(endpoint, accessToken);
062                this.authRequestID = Objects.requireNonNull(authRequestID);
063        }
064        
065        
066        /**
067         * Checks if the callback indicates success.
068         *
069         * @return {@code true} if the callback indicates success, else
070         *         {@code false}.
071         */
072        public abstract boolean indicatesSuccess();
073        
074        
075        /**
076         * Returns the CIBA request ID.
077         *
078         * @return The CIBA request ID.
079         */
080        public AuthRequestID getAuthRequestID() {
081                
082                return authRequestID;
083        }
084        
085        
086        /**
087         * Casts this CIBA push callback to token delivery.
088         *
089         * @return The CIBA token push delivery.
090         */
091        public CIBATokenDelivery toTokenDelivery() {
092                
093                return (CIBATokenDelivery) this;
094        }
095        
096        
097        /**
098         * Casts this CIBA push callback to an error delivery.
099         *
100         * @return The CIBA error push delivery.
101         */
102        public CIBAErrorDelivery toErrorDelivery() {
103                
104                return (CIBAErrorDelivery) this;
105        }
106        
107        
108        /**
109         * Parses a CIBA push callback from the specified HTTP request.
110         *
111         * @param httpRequest The HTTP request. Must not be {@code null}.
112         *
113         * @return The CIBA token or error push delivery.
114         *
115         * @throws ParseException If the HTTP request couldn't be parsed to a
116         *                        CIBA push callback.
117         */
118        public static CIBAPushCallback parse(final HTTPRequest httpRequest)
119                throws ParseException {
120                
121                JSONObject jsonObject = httpRequest.getBodyAsJSONObject();
122                
123                if (jsonObject.containsKey("error")) {
124                        return CIBAErrorDelivery.parse(httpRequest);
125                } else {
126                        return CIBATokenDelivery.parse(httpRequest);
127                }
128        }
129}