001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2016, 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.openid.connect.sdk; 019 020 021import net.minidev.json.JSONObject; 022 023import com.nimbusds.oauth2.sdk.ParseException; 024import com.nimbusds.oauth2.sdk.TokenErrorResponse; 025import com.nimbusds.oauth2.sdk.TokenResponse; 026import com.nimbusds.oauth2.sdk.http.HTTPResponse; 027 028 029/** 030 * Parser of OpenID Connect token endpoint response messages. 031 * 032 * <p>Related specifications: 033 * 034 * <ul> 035 * <li>OpenID Connect Core 1.0, sections 3.1.3.3 and 3.1.3.4. 036 * </ul> 037 */ 038public class OIDCTokenResponseParser { 039 040 041 /** 042 * Parses an OpenID Connect token response or token error response from 043 * the specified JSON object. 044 * 045 * @param jsonObject The JSON object to parse. Must not be 046 * {@code null}. 047 * 048 * @return The OpenID Connect token response or token error response. 049 * 050 * @throws ParseException If the JSON object couldn't be parsed to a 051 * token response. 052 */ 053 public static TokenResponse parse(final JSONObject jsonObject) 054 throws ParseException { 055 056 if (jsonObject.containsKey("error")) 057 return TokenErrorResponse.parse(jsonObject); 058 else 059 return OIDCTokenResponse.parse(jsonObject); 060 } 061 062 063 /** 064 * Parses an OpenID Connect token response or token error response from 065 * the specified HTTP response. 066 * 067 * @param httpResponse The HTTP response. Must not be {@code null}. 068 * 069 * @return The OpenID Connect token response or token error response. 070 * 071 * @throws ParseException If the HTTP response couldn't be parsed to a 072 * token response. 073 */ 074 public static TokenResponse parse(final HTTPResponse httpResponse) 075 throws ParseException { 076 077 if (httpResponse.getStatusCode() == HTTPResponse.SC_OK) 078 return OIDCTokenResponse.parse(httpResponse); 079 else 080 return TokenErrorResponse.parse(httpResponse); 081 } 082 083 084 /** 085 * Prevents public instantiation. 086 */ 087 private OIDCTokenResponseParser() { } 088}