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.common.contenttype.ContentType;
022import com.nimbusds.oauth2.sdk.ErrorObject;
023import com.nimbusds.oauth2.sdk.OAuth2Error;
024import com.nimbusds.oauth2.sdk.ParseException;
025import com.nimbusds.oauth2.sdk.http.HTTPRequest;
026import com.nimbusds.oauth2.sdk.token.BearerAccessToken;
027import com.nimbusds.oauth2.sdk.util.JSONObjectUtils;
028import net.jcip.annotations.Immutable;
029import net.minidev.json.JSONObject;
030
031import java.net.URI;
032import java.util.Collections;
033import java.util.HashSet;
034import java.util.Objects;
035import java.util.Set;
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
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                this.errorObject = Objects.requireNonNull(errorObject);
099        }
100        
101        
102        @Override
103        public boolean indicatesSuccess() {
104                
105                return false;
106        }
107        
108        
109        /**
110         * Returns the error object.
111         *
112         * @return The error object.
113         */
114        public ErrorObject getErrorObject() {
115                
116                return errorObject;
117        }
118        
119        
120        @Override
121        public HTTPRequest toHTTPRequest() {
122                
123                HTTPRequest httpRequest = new HTTPRequest(HTTPRequest.Method.POST, getEndpointURI());
124                httpRequest.setAuthorization(getAccessToken().toAuthorizationHeader());
125                httpRequest.setEntityContentType(ContentType.APPLICATION_JSON);
126                JSONObject jsonObject = new JSONObject();
127                jsonObject.put("auth_req_id", getAuthRequestID().getValue());
128                jsonObject.putAll(getErrorObject().toJSONObject());
129                httpRequest.setBody(jsonObject.toJSONString());
130                return httpRequest;
131        }
132        
133        
134        /**
135         * Parses a CIBA error push delivery from the specified HTTP request.
136         *
137         * @param httpRequest The HTTP request. Must not be {@code null}.
138         *
139         * @return The CIBA error push delivery.
140         *
141         * @throws ParseException If parsing failed.
142         */
143        public static CIBAErrorDelivery parse(final HTTPRequest httpRequest)
144                throws ParseException {
145                
146                URI uri = httpRequest.getURI();
147                httpRequest.ensureMethod(HTTPRequest.Method.POST);
148                httpRequest.ensureEntityContentType(ContentType.APPLICATION_JSON);
149                
150                BearerAccessToken clientNotificationToken = BearerAccessToken.parse(httpRequest);
151                
152                AuthRequestID authRequestID = new AuthRequestID(
153                        JSONObjectUtils.getNonBlankString(
154                                httpRequest.getBodyAsJSONObject(),
155                                "auth_req_id")
156                );
157                
158                ErrorObject errorObject = ErrorObject.parse(httpRequest.getQueryAsJSONObject());
159                
160                return new CIBAErrorDelivery(uri, clientNotificationToken, authRequestID, errorObject);
161        }
162}