001package com.nimbusds.openid.connect.sdk; 002 003 004import net.minidev.json.JSONObject; 005 006import com.nimbusds.oauth2.sdk.ParseException; 007import com.nimbusds.oauth2.sdk.TokenErrorResponse; 008import com.nimbusds.oauth2.sdk.TokenResponse; 009import com.nimbusds.oauth2.sdk.http.HTTPResponse; 010 011 012/** 013 * Parser of OpenID Connect token endpoint response messages. 014 * 015 * <p>Related specifications: 016 * 017 * <ul> 018 * <li>OpenID Connect Messages 1.0, sections 2.2.3 and 2.2.4. 019 * </ul> 020 * 021 * @author Vladimir Dzhuvinov 022 */ 023public class OIDCTokenResponseParser { 024 025 026 /** 027 * Parses an OpenID Connect access token response or token error 028 * response from the specified JSON object. 029 * 030 * @param jsonObject The JSON object to parse. Must not be 031 * {@code null}. 032 * 033 * @return The OpenID Connect access token response or token error 034 * response. 035 * 036 * @throws ParseException If the JSON object couldn't be parsed to a 037 * token response. 038 */ 039 public static TokenResponse parse(final JSONObject jsonObject) 040 throws ParseException { 041 042 if (jsonObject.containsKey("error")) 043 return TokenErrorResponse.parse(jsonObject); 044 else 045 return OIDCAccessTokenResponse.parse(jsonObject); 046 } 047 048 049 /** 050 * Parses an OpenID Connect access token response or token error 051 * response from the specified HTTP response. 052 * 053 * @param httpResponse The HTTP response. Must not be {@code null}. 054 * 055 * @return The OpenID Connect access token response or token error 056 * response. 057 * 058 * @throws ParseException If the HTTP response couldn't be parsed to a 059 * token response. 060 */ 061 public static TokenResponse parse(final HTTPResponse httpResponse) 062 throws ParseException { 063 064 if (httpResponse.getStatusCode() == HTTPResponse.SC_OK) 065 return OIDCAccessTokenResponse.parse(httpResponse); 066 else 067 return TokenErrorResponse.parse(httpResponse); 068 } 069 070 071 /** 072 * Prevents public instantiation. 073 */ 074 private OIDCTokenResponseParser() { } 075}