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> 033 * Related specifications: 034 * 035 * <ul> 036 * <li>OAuth 2.0 Device Authorization Grant (draft-ietf-oauth-device-flow-15) 037 * </ul> 038 */ 039public abstract class DeviceAuthorizationResponse implements Response { 040 041 042 /** 043 * Casts this response to an authorization success response. 044 * 045 * @return The authorization success response. 046 */ 047 public DeviceAuthorizationSuccessResponse toSuccessResponse() { 048 049 return (DeviceAuthorizationSuccessResponse) this; 050 } 051 052 053 /** 054 * Casts this response to a device authorization error response. 055 * 056 * @return The device authorization error response. 057 */ 058 public DeviceAuthorizationErrorResponse toErrorResponse() { 059 060 return (DeviceAuthorizationErrorResponse) this; 061 } 062 063 064 /** 065 * Parses a device authorization response from the specified JSON 066 * object. 067 * 068 * @param jsonObject The JSON object to parse. Must not be 069 * {@code null}. 070 * 071 * @return The device authorization success or error response. 072 * 073 * @throws ParseException If the JSON object couldn't be parsed to a 074 * device authorization response. 075 */ 076 public static DeviceAuthorizationResponse parse(final JSONObject jsonObject) throws ParseException { 077 078 if (jsonObject.containsKey("device_code")) 079 return DeviceAuthorizationSuccessResponse.parse(jsonObject); 080 else 081 return DeviceAuthorizationErrorResponse.parse(jsonObject); 082 } 083 084 085 /** 086 * Parses a device authorization response from the specified HTTP 087 * response. 088 * 089 * @param httpResponse The HTTP response. Must not be {@code null}. 090 * 091 * @return The device authorization sucess or error response. 092 * 093 * @throws ParseException If the HTTP response couldn't be parsed to a 094 * device authorization response. 095 */ 096 public static DeviceAuthorizationResponse parse(final HTTPResponse httpResponse) throws ParseException { 097 098 if (httpResponse.getStatusCode() == HTTPResponse.SC_OK) 099 return DeviceAuthorizationSuccessResponse.parse(httpResponse); 100 else 101 return DeviceAuthorizationErrorResponse.parse(httpResponse); 102 } 103}