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 java.net.URI;
022
023import net.minidev.json.JSONObject;
024
025import com.nimbusds.oauth2.sdk.ParseException;
026import com.nimbusds.oauth2.sdk.ProtectedResourceRequest;
027import com.nimbusds.oauth2.sdk.http.HTTPRequest;
028import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
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, section 12.3.
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                
063                if (authRequestID == null) {
064                        throw new IllegalArgumentException("The auth_req_id must not be null");
065                }
066                this.authRequestID = authRequestID;
067        }
068        
069        
070        /**
071         * Checks if the callback indicates success.
072         *
073         * @return {@code true} if the callback indicates success, else
074         *         {@code false}.
075         */
076        public abstract boolean indicatesSuccess();
077        
078        
079        /**
080         * Returns the CIBA request ID.
081         *
082         * @return The CIBA request ID.
083         */
084        public AuthRequestID getAuthRequestID() {
085                
086                return authRequestID;
087        }
088        
089        
090        /**
091         * Casts this CIBA push callback to token delivery.
092         *
093         * @return The CIBA token push delivery.
094         */
095        public CIBATokenDelivery toTokenDelivery() {
096                
097                return (CIBATokenDelivery) this;
098        }
099        
100        
101        /**
102         * Casts this CIBA push callback to an error delivery.
103         *
104         * @return The CIBA error push delivery.
105         */
106        public CIBAErrorDelivery toErrorDelivery() {
107                
108                return (CIBAErrorDelivery) this;
109        }
110        
111        
112        /**
113         * Parses a CIBA push callback from the specified HTTP request.
114         *
115         * @param httpRequest The HTTP request. Must not be {@code null}.
116         *
117         * @return The CIBA token or error push delivery.
118         *
119         * @throws ParseException If the HTTP request couldn't be parsed to a
120         *                        CIBA push callback.
121         */
122        public static CIBAPushCallback parse(final HTTPRequest httpRequest)
123                throws ParseException {
124                
125                JSONObject jsonObject = httpRequest.getQueryAsJSONObject();
126                
127                if (jsonObject.containsKey("error")) {
128                        return CIBAErrorDelivery.parse(httpRequest);
129                } else {
130                        return CIBATokenDelivery.parse(httpRequest);
131                }
132        }
133}