All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nimbusds.oauth2.sdk.TokenErrorResponse Maven / Gradle / Ivy

Go to download

OAuth 2.0 SDK with OpenID Connection extensions for developing client and server applications.

There is a newer version: 11.20.1
Show newest version
/*
 * oauth2-oidc-sdk
 *
 * Copyright 2012-2016, Connect2id Ltd and contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.nimbusds.oauth2.sdk;


import com.nimbusds.common.contenttype.ContentType;
import com.nimbusds.oauth2.sdk.http.HTTPResponse;
import net.jcip.annotations.Immutable;
import net.minidev.json.JSONObject;

import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;


/**
 * OAuth 2.0 Token error response.
 *
 * 

Standard token errors: * *

    *
  • {@link OAuth2Error#INVALID_REQUEST} *
  • {@link OAuth2Error#INVALID_CLIENT} *
  • {@link OAuth2Error#INVALID_GRANT} *
  • {@link OAuth2Error#UNAUTHORIZED_CLIENT} *
  • {@link OAuth2Error#UNSUPPORTED_GRANT_TYPE} *
  • {@link OAuth2Error#INVALID_SCOPE} *
* *

Example HTTP response: * *

 * HTTP/1.1 400 Bad Request
 * Content-Type: application/json
 * Cache-Control: no-store
 * Pragma: no-cache
 * 
 * {
 *  "error": "invalid_request"
 * }
 * 
* *

Related specifications: * *

    *
  • OAuth 2.0 (RFC 6749) *
*/ @Immutable public class TokenErrorResponse extends TokenResponse implements ErrorResponse { /** * The standard OAuth 2.0 errors for an Access Token error response. */ private static final Set STANDARD_ERRORS; static { Set errors = new HashSet<>(); errors.add(OAuth2Error.INVALID_REQUEST); errors.add(OAuth2Error.INVALID_CLIENT); errors.add(OAuth2Error.INVALID_GRANT); errors.add(OAuth2Error.UNAUTHORIZED_CLIENT); errors.add(OAuth2Error.UNSUPPORTED_GRANT_TYPE); errors.add(OAuth2Error.INVALID_SCOPE); STANDARD_ERRORS = Collections.unmodifiableSet(errors); } /** * Gets the standard OAuth 2.0 errors for an Access Token error * response. * * @return The standard errors, as a read-only set. */ public static Set getStandardErrors() { return STANDARD_ERRORS; } /** * The error. */ private final ErrorObject error; /** * Creates a new OAuth 2.0 Access Token error response. No OAuth 2.0 * error is specified. */ protected TokenErrorResponse() { error = null; } /** * Creates a new OAuth 2.0 Access Token error response. * * @param error The error. Should match one of the * {@link #getStandardErrors standard errors} for a token * error response. Must not be {@code null}. */ public TokenErrorResponse(final ErrorObject error) { this.error = Objects.requireNonNull(error); } @Override public boolean indicatesSuccess() { return false; } @Override public ErrorObject getErrorObject() { return error; } /** * Returns the JSON object for this token error response. * * @return The JSON object for this token error response. */ public JSONObject toJSONObject() { JSONObject o = new JSONObject(); // No error? if (error == null) return o; o.putAll(error.toJSONObject()); return o; } @Override public HTTPResponse toHTTPResponse() { int statusCode = (error != null && error.getHTTPStatusCode() > 0) ? error.getHTTPStatusCode() : HTTPResponse.SC_BAD_REQUEST; HTTPResponse httpResponse = new HTTPResponse(statusCode); if (error == null) return httpResponse; httpResponse.setEntityContentType(ContentType.APPLICATION_JSON); httpResponse.setCacheControl("no-store"); httpResponse.setPragma("no-cache"); httpResponse.setBody(toJSONObject().toString()); return httpResponse; } /** * Parses an OAuth 2.0 Token Error response from the specified JSON * object. * * @param jsonObject The JSON object to parse. Its status code must not * be 200 (OK). Must not be {@code null}. * * @return The token error response. * * @throws ParseException If the JSON object couldn't be parsed to an * OAuth 2.0 Token Error response. */ public static TokenErrorResponse parse(final JSONObject jsonObject) throws ParseException { // No error code? if (! jsonObject.containsKey("error")) return new TokenErrorResponse(); ErrorObject error = ErrorObject.parse(jsonObject).setHTTPStatusCode(HTTPResponse.SC_BAD_REQUEST); return new TokenErrorResponse(error); } /** * Parses an OAuth 2.0 Token Error response from the specified HTTP * response. * * @param httpResponse The HTTP response to parse. Its status code must * not be 200 (OK). Must not be {@code null}. * * @return The token error response. * * @throws ParseException If the HTTP response couldn't be parsed to an * OAuth 2.0 Token Error response. */ public static TokenErrorResponse parse(final HTTPResponse httpResponse) throws ParseException { httpResponse.ensureStatusCodeNotOK(); return new TokenErrorResponse(ErrorObject.parse(httpResponse)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy