Skip to content

Commit 835a326

Browse files
committed
allow polling of device codes, fixed UI for device code input
1 parent 1d7fba5 commit 835a326

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,16 @@ public interface DeviceCodeService {
4848
* @param client
4949
* @return
5050
*/
51-
public DeviceCode consumeDeviceCode(String deviceCode, ClientDetails client);
51+
public DeviceCode findDeviceCode(String deviceCode, ClientDetails client);
5252

53+
54+
/**
55+
*
56+
* @param deviceCode
57+
* @param client
58+
*/
59+
public void clearDeviceCode(String deviceCode, ClientDetails client);
60+
5361
/**
5462
* @param deviceCode
5563
* @param userCode

openid-connect-server-webapp/src/main/webapp/WEB-INF/views/approveDevice.jsp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</h1>
3838

3939
<form name="confirmationForm"
40-
action="${pageContext.request.contextPath.endsWith('/') ? pageContext.request.contextPath : pageContext.request.contextPath.concat('/') }device-user/approve" method="post">
40+
action="${pageContext.request.contextPath.endsWith('/') ? pageContext.request.contextPath : pageContext.request.contextPath.concat('/') }device/approve" method="post">
4141

4242
<div class="row">
4343
<div class="span5 offset1 well-small" style="text-align: left">
@@ -163,8 +163,9 @@
163163
</div>
164164
</c:if>
165165

166+
<ul>
166167
<c:forEach var="scope" items="${ scopes }">
167-
168+
<li>
168169
<c:if test="${ not empty scope.icon }">
169170
<i class="icon-${ fn:escapeXml(scope.icon) }"></i>
170171
</c:if>
@@ -199,9 +200,9 @@
199200

200201
</span>
201202
</c:if>
202-
203+
</li>
203204
</c:forEach>
204-
205+
</ul>
205206
</fieldset>
206207

207208
</div>

openid-connect-server-webapp/src/main/webapp/WEB-INF/views/requestUserCode.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
</c:if>
4040

4141

42-
<form action="${ config.issuer }${ config.issuer.endsWith('/') ? '' : '/' }device-user/verify" method="POST">
42+
<form action="${ config.issuer }${ config.issuer.endsWith('/') ? '' : '/' }device/verify" method="POST">
4343

4444
<div class="row-fluid">
4545
<div class="span12">

openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultDeviceCodeService.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,26 @@ public DeviceCode approveDeviceCode(DeviceCode dc, OAuth2Authentication auth) {
9090
* @see org.mitre.oauth2.service.DeviceCodeService#consumeDeviceCode(java.lang.String, org.springframework.security.oauth2.provider.ClientDetails)
9191
*/
9292
@Override
93-
public DeviceCode consumeDeviceCode(String deviceCode, ClientDetails client) {
93+
public DeviceCode findDeviceCode(String deviceCode, ClientDetails client) {
9494
DeviceCode found = repository.getByDeviceCode(deviceCode);
9595

96-
// make sure it's not used twice
97-
repository.remove(found);
98-
99-
if (found.getClientId().equals(client.getClientId())) {
100-
// make sure the client matches, if so, we're good
101-
return found;
96+
if (found != null) {
97+
if (found.getClientId().equals(client.getClientId())) {
98+
// make sure the client matches, if so, we're good
99+
return found;
100+
} else {
101+
// if the clients don't match, pretend the code wasn't found
102+
return null;
103+
}
102104
} else {
103-
// if the clients don't match, pretend the code wasn't found
105+
// didn't find the code, return null
104106
return null;
105107
}
106108

107109
}
108110

111+
112+
109113
/* (non-Javadoc)
110114
* @see org.mitre.oauth2.service.DeviceCodeService#clearExpiredDeviceCodes()
111115
*/
@@ -126,4 +130,18 @@ protected void doOperation(DeviceCode item) {
126130
}.execute();
127131
}
128132

133+
/* (non-Javadoc)
134+
* @see org.mitre.oauth2.service.DeviceCodeService#clearDeviceCode(java.lang.String, org.springframework.security.oauth2.provider.ClientDetails)
135+
*/
136+
@Override
137+
public void clearDeviceCode(String deviceCode, ClientDetails client) {
138+
DeviceCode found = findDeviceCode(deviceCode, client);
139+
140+
if (found != null) {
141+
// make sure it's not used twice
142+
repository.remove(found);
143+
}
144+
145+
}
146+
129147
}

openid-connect-server/src/main/java/org/mitre/oauth2/token/DeviceTokenGranter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,15 @@ protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, Tok
7070
String deviceCode = tokenRequest.getRequestParameters().get("device_code");
7171

7272
// look up the device code and consume it
73-
DeviceCode dc = deviceCodeService.consumeDeviceCode(deviceCode, client);
73+
DeviceCode dc = deviceCodeService.findDeviceCode(deviceCode, client);
7474

7575
if (dc != null) {
7676

7777
// make sure the code hasn't expired yet
7878
if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) {
79-
// TODO: return an error
79+
80+
deviceCodeService.clearDeviceCode(deviceCode, client);
81+
8082
throw new DeviceCodeExpiredException("Device code has expired " + deviceCode);
8183

8284
} else if (!dc.isApproved()) {
@@ -90,6 +92,8 @@ protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, Tok
9092

9193
OAuth2Authentication auth = new OAuth2Authentication(getRequestFactory().createOAuth2Request(client, tokenRequest), dc.getAuthenticationHolder().getUserAuth());
9294

95+
deviceCodeService.clearDeviceCode(deviceCode, client);
96+
9397
return auth;
9498
}
9599
} else {

0 commit comments

Comments
 (0)