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;
022import java.util.Collections;
023import java.util.HashSet;
024import java.util.Set;
025
026import net.jcip.annotations.Immutable;
027import net.minidev.json.JSONObject;
028
029import com.nimbusds.common.contenttype.ContentType;
030import com.nimbusds.oauth2.sdk.ErrorObject;
031import com.nimbusds.oauth2.sdk.OAuth2Error;
032import com.nimbusds.oauth2.sdk.ParseException;
033import com.nimbusds.oauth2.sdk.http.HTTPRequest;
034import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
035import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
036
037
038/**
039 * CIBA error push delivery to the client notification endpoint.
040 *
041 * <p>Related specifications:
042 *
043 * <ul>
044 *      <li>OpenID Connect CIBA Flow - Core 1.0, section 12.
045 * </ul>
046 */
047@Immutable
048public class CIBAErrorDelivery extends CIBAPushCallback {
049        
050        
051        /**
052         * The standard OAuth 2.0 errors for a CIBA error delivery.
053         */
054        private static final Set<ErrorObject> STANDARD_ERRORS;
055        
056        static {
057                Set<ErrorObject> errors = new HashSet<>();
058                errors.add(OAuth2Error.ACCESS_DENIED);
059                errors.add(CIBAError.EXPIRED_TOKEN);
060                errors.add(CIBAError.TRANSACTION_FAILED);
061                STANDARD_ERRORS = Collections.unmodifiableSet(errors);
062        }
063        
064        
065        /**
066         * Gets the standard OAuth 2.0 errors for a CIBA error delivery.
067         *
068         * @return The standard errors, as a read-only set.
069         */
070        public static Set<ErrorObject> getStandardErrors() {
071                
072                return STANDARD_ERRORS;
073        }
074        
075        
076        /**
077         * The error object.
078         */
079        private final ErrorObject errorObject;
080        
081        
082        /**
083         * Creates a new CIBA error push delivery.
084         *
085         * @param endpoint      The client notification endpoint. Must not be
086         *                      {@code null}.
087         * @param accessToken   The client notification token. Must not be
088         *                      {@code null}.
089         * @param authRequestID The CIBA request ID. Must not be {@code null}.
090         * @param errorObject   The error object. Must not be {@code null}.
091         */
092        public CIBAErrorDelivery(final URI endpoint,
093                                 final BearerAccessToken accessToken,
094                                 final AuthRequestID authRequestID,
095                                 final ErrorObject errorObject) {
096                
097                super(endpoint, accessToken, authRequestID);
098                
099                if (endpoint == null) {
100                        throw new IllegalArgumentException("The error object must not be null");
101                }
102                this.errorObject = errorObject;
103        }
104        
105        
106        @Override
107        public boolean indicatesSuccess() {
108                
109                return false;
110        }
111        
112        
113        /**
114         * Returns the error object.
115         *
116         * @return The error object.
117         */
118        public ErrorObject getErrorObject() {
119                
120                return errorObject;
121        }
122        
123        
124        @Override
125        public HTTPRequest toHTTPRequest() {
126                
127                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.POST, getEndpointURI());
128                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
129                httpRequest.setEntityContentType(ContentType.APPLICATION_JSON);
130                JSONObject jsonObject = new JSONObject();
131                jsonObject.put("auth_req_id", getAuthRequestID().getValue());
132                jsonObject.putAll(getErrorObject().toJSONObject());
133                httpRequest.setQuery(jsonObject.toJSONString());
134                return httpRequest;
135        }
136        
137        
138        /**
139         * Parses a CIBA error push delivery from the specified HTTP request.
140         *
141         * @param httpRequest The HTTP request. Must not be {@code null}.
142         *
143         * @return The CIBA error push delivery.
144         *
145         * @throws ParseException If parsing failed.
146         */
147        public static CIBAErrorDelivery parse(final HTTPRequest httpRequest)
148                throws ParseException {
149                
150                URI uri = httpRequest.getURI();
151                httpRequest.ensureMethod(HTTPRequest.Method.POST);
152                httpRequest.ensureEntityContentType(ContentType.APPLICATION_JSON);
153                
154                BearerAccessToken clientNotificationToken = BearerAccessToken.parse(httpRequest);
155                
156                AuthRequestID authRequestID = new AuthRequestID(
157                        JSONObjectUtils.getString(
158                                httpRequest.getQueryAsJSONObject(),
159                                "auth_req_id")
160                );
161                
162                ErrorObject errorObject = ErrorObject.parse(httpRequest.getQueryAsJSONObject());
163                
164                return new CIBAErrorDelivery(uri, clientNotificationToken, authRequestID, errorObject);
165        }
166}