Skip to content

Commit d317cf5

Browse files
committed
added exception handling to device code creation step
1 parent cc0622e commit d317cf5

File tree

3 files changed

+76
-14
lines changed

3 files changed

+76
-14
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*******************************************************************************
2+
* Copyright 2017 The MITRE Corporation
3+
* and the MIT Internet Trust Consortium
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*******************************************************************************/
17+
18+
package org.mitre.oauth2.exception;
19+
20+
/**
21+
* @author jricher
22+
*
23+
*/
24+
public class DeviceCodeCreationException extends Exception {
25+
26+
private static final long serialVersionUID = 8078568710169208466L;
27+
28+
private String error;
29+
30+
public DeviceCodeCreationException(String error, String message) {
31+
super(message);
32+
this.error = error;
33+
}
34+
35+
/**
36+
* @return the error
37+
*/
38+
public String getError() {
39+
return error;
40+
}
41+
42+
/**
43+
* @param error the error to set
44+
*/
45+
public void setError(String error) {
46+
this.error = error;
47+
}
48+
49+
50+
51+
}

openid-connect-common/src/main/java/org/mitre/oauth2/service/DeviceCodeService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222

23+
import org.mitre.oauth2.exception.DeviceCodeCreationException;
2324
import org.mitre.oauth2.model.ClientDetailsEntity;
2425
import org.mitre.oauth2.model.DeviceCode;
2526
import org.springframework.security.oauth2.provider.ClientDetails;
@@ -66,7 +67,7 @@ public interface DeviceCodeService {
6667
* @param parameters
6768
* @return
6869
*/
69-
public DeviceCode createNewDeviceCode(Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters);
70+
public DeviceCode createNewDeviceCode(Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters) throws DeviceCodeCreationException;
7071

7172

7273
public void clearExpiredDeviceCodes();

openid-connect-server/src/main/java/org/mitre/oauth2/web/DeviceEndpoint.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import javax.servlet.http.HttpSession;
2929

30+
import org.mitre.oauth2.exception.DeviceCodeCreationException;
3031
import org.mitre.oauth2.model.ClientDetailsEntity;
3132
import org.mitre.oauth2.model.DeviceCode;
3233
import org.mitre.oauth2.model.SystemScope;
@@ -132,21 +133,30 @@ public String requestDeviceCode(@RequestParam("client_id") String clientId, @Req
132133

133134
// if we got here the request is legit
134135

135-
DeviceCode dc = deviceCodeService.createNewDeviceCode(requestedScopes, client, parameters);
136-
137-
Map<String, Object> response = new HashMap<>();
138-
response.put("device_code", dc.getDeviceCode());
139-
response.put("user_code", dc.getUserCode());
140-
response.put("verification_uri", config.getIssuer() + USER_URL);
141-
if (client.getDeviceCodeValiditySeconds() != null) {
142-
response.put("expires_in", client.getDeviceCodeValiditySeconds());
136+
try {
137+
DeviceCode dc = deviceCodeService.createNewDeviceCode(requestedScopes, client, parameters);
138+
139+
Map<String, Object> response = new HashMap<>();
140+
response.put("device_code", dc.getDeviceCode());
141+
response.put("user_code", dc.getUserCode());
142+
response.put("verification_uri", config.getIssuer() + USER_URL);
143+
if (client.getDeviceCodeValiditySeconds() != null) {
144+
response.put("expires_in", client.getDeviceCodeValiditySeconds());
145+
}
146+
147+
model.put(JsonEntityView.ENTITY, response);
148+
149+
150+
return JsonEntityView.VIEWNAME;
151+
} catch (DeviceCodeCreationException dcce) {
152+
153+
model.put(HttpCodeView.CODE, HttpStatus.BAD_REQUEST);
154+
model.put(JsonErrorView.ERROR, dcce.getError());
155+
model.put(JsonErrorView.ERROR_MESSAGE, dcce.getMessage());
156+
157+
return JsonErrorView.VIEWNAME;
143158
}
144159

145-
model.put(JsonEntityView.ENTITY, response);
146-
147-
148-
return JsonEntityView.VIEWNAME;
149-
150160
}
151161

152162
@PreAuthorize("hasRole('ROLE_USER')")

0 commit comments

Comments
 (0)