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.oauth2.sdk.device;
019
020
021import net.minidev.json.JSONObject;
022
023import com.nimbusds.oauth2.sdk.ParseException;
024import com.nimbusds.oauth2.sdk.Response;
025import com.nimbusds.oauth2.sdk.http.HTTPResponse;
026
027
028/**
029 * Token endpoint response. This is the base abstract class for device
030 * authorization success and error responses.
031 *
032 * <p>Related specifications:
033 *
034 * <ul>
035 *     <li>OAuth 2.0 Device Authorization Grant (RFC 8628)
036 * </ul>
037 */
038public abstract class DeviceAuthorizationResponse implements Response {
039
040        
041        /**
042         * Casts this response to an authorization success response.
043         *
044         * @return The authorization success response.
045         */
046        public DeviceAuthorizationSuccessResponse toSuccessResponse() {
047
048                return (DeviceAuthorizationSuccessResponse) this;
049        }
050
051
052        /**
053         * Casts this response to a device authorization error response.
054         *
055         * @return The device authorization error response.
056         */
057        public DeviceAuthorizationErrorResponse toErrorResponse() {
058
059                return (DeviceAuthorizationErrorResponse) this;
060        }
061
062
063        /**
064         * Parses a device authorization response from the specified JSON
065         * object.
066         *
067         * @param jsonObject The JSON object to parse. Must not be
068         *                   {@code null}.
069         *
070         * @return The device authorization success or error response.
071         *
072         * @throws ParseException If the JSON object couldn't be parsed to a
073         *                        device authorization response.
074         */
075        public static DeviceAuthorizationResponse parse(final JSONObject jsonObject) throws ParseException {
076
077                if (jsonObject.containsKey("device_code"))
078                        return DeviceAuthorizationSuccessResponse.parse(jsonObject);
079                else
080                        return DeviceAuthorizationErrorResponse.parse(jsonObject);
081        }
082
083
084        /**
085         * Parses a device authorization response from the specified HTTP
086         * response.
087         *
088         * @param httpResponse The HTTP response. Must not be {@code null}.
089         *
090         * @return The device authorization success or error response.
091         *
092         * @throws ParseException If the HTTP response couldn't be parsed to a
093         *                        device authorization response.
094         */
095        public static DeviceAuthorizationResponse parse(final HTTPResponse httpResponse) throws ParseException {
096
097                if (httpResponse.getStatusCode() == HTTPResponse.SC_OK)
098                        return DeviceAuthorizationSuccessResponse.parse(httpResponse);
099                else
100                        return DeviceAuthorizationErrorResponse.parse(httpResponse);
101        }
102}