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 Core 1.0, sections 3.1.3.3 and 3.1.3.4.
019 * </ul>
020 */
021public class OIDCTokenResponseParser { 
022
023
024        /**
025         * Parses an OpenID Connect token response or token error response from
026         * the specified JSON object.
027         *
028         * @param jsonObject The JSON object to parse. Must not be 
029         *                   {@code null}.
030         *
031         * @return The OpenID Connect token response or token error response.
032         *
033         * @throws ParseException If the JSON object couldn't be parsed to a
034         *                        token response.
035         */
036        public static TokenResponse parse(final JSONObject jsonObject)
037                throws ParseException {
038
039                if (jsonObject.containsKey("error"))
040                        return TokenErrorResponse.parse(jsonObject);
041                else
042                        return OIDCTokenResponse.parse(jsonObject);
043        }
044
045
046        /**
047         * Parses an OpenID Connect token response or token error response from
048         * the specified HTTP response.
049         *
050         * @param httpResponse The HTTP response. Must not be {@code null}.
051         *
052         * @return The OpenID Connect token response or token error response.
053         *
054         * @throws ParseException If the HTTP response couldn't be parsed to a
055         *                        token response.
056         */
057        public static TokenResponse parse(final HTTPResponse httpResponse)
058                throws ParseException {
059                
060                if (httpResponse.getStatusCode() == HTTPResponse.SC_OK)
061                        return OIDCTokenResponse.parse(httpResponse);
062                else
063                        return TokenErrorResponse.parse(httpResponse);
064        }
065
066
067        /**
068         * Prevents public instantiation.
069         */
070        private OIDCTokenResponseParser() { }
071}