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