added exception handling to device code creation step

pull/972/merge
Justin Richer 2017-04-12 15:59:17 -04:00
parent cc0622edd0
commit d317cf5024
3 changed files with 76 additions and 14 deletions

View File

@ -0,0 +1,51 @@
/*******************************************************************************
* Copyright 2017 The MITRE Corporation
* and the MIT Internet Trust Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package org.mitre.oauth2.exception;
/**
* @author jricher
*
*/
public class DeviceCodeCreationException extends Exception {
private static final long serialVersionUID = 8078568710169208466L;
private String error;
public DeviceCodeCreationException(String error, String message) {
super(message);
this.error = error;
}
/**
* @return the error
*/
public String getError() {
return error;
}
/**
* @param error the error to set
*/
public void setError(String error) {
this.error = error;
}
}

View File

@ -20,6 +20,7 @@ package org.mitre.oauth2.service;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.mitre.oauth2.exception.DeviceCodeCreationException;
import org.mitre.oauth2.model.ClientDetailsEntity; import org.mitre.oauth2.model.ClientDetailsEntity;
import org.mitre.oauth2.model.DeviceCode; import org.mitre.oauth2.model.DeviceCode;
import org.springframework.security.oauth2.provider.ClientDetails; import org.springframework.security.oauth2.provider.ClientDetails;
@ -66,7 +67,7 @@ public interface DeviceCodeService {
* @param parameters * @param parameters
* @return * @return
*/ */
public DeviceCode createNewDeviceCode(Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters); public DeviceCode createNewDeviceCode(Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters) throws DeviceCodeCreationException;
public void clearExpiredDeviceCodes(); public void clearExpiredDeviceCodes();

View File

@ -27,6 +27,7 @@ import java.util.UUID;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.mitre.oauth2.exception.DeviceCodeCreationException;
import org.mitre.oauth2.model.ClientDetailsEntity; import org.mitre.oauth2.model.ClientDetailsEntity;
import org.mitre.oauth2.model.DeviceCode; import org.mitre.oauth2.model.DeviceCode;
import org.mitre.oauth2.model.SystemScope; import org.mitre.oauth2.model.SystemScope;
@ -132,6 +133,7 @@ public class DeviceEndpoint {
// if we got here the request is legit // if we got here the request is legit
try {
DeviceCode dc = deviceCodeService.createNewDeviceCode(requestedScopes, client, parameters); DeviceCode dc = deviceCodeService.createNewDeviceCode(requestedScopes, client, parameters);
Map<String, Object> response = new HashMap<>(); Map<String, Object> response = new HashMap<>();
@ -146,6 +148,14 @@ public class DeviceEndpoint {
return JsonEntityView.VIEWNAME; return JsonEntityView.VIEWNAME;
} catch (DeviceCodeCreationException dcce) {
model.put(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
model.put(JsonErrorView.ERROR, dcce.getError());
model.put(JsonErrorView.ERROR_MESSAGE, dcce.getMessage());
return JsonErrorView.VIEWNAME;
}
} }