1616
1717package org .mitre .oauth2 .web ;
1818
19+ import java .net .URI ;
20+ import java .net .URISyntaxException ;
1921import java .util .Collection ;
2022import java .util .Date ;
2123import java .util .HashMap ;
2628
2729import javax .servlet .http .HttpSession ;
2830
31+ import org .apache .http .client .utils .URIBuilder ;
2932import org .mitre .oauth2 .exception .DeviceCodeCreationException ;
3033import org .mitre .oauth2 .model .ClientDetailsEntity ;
3134import org .mitre .oauth2 .model .DeviceCode ;
@@ -134,14 +137,22 @@ public String requestDeviceCode(@RequestParam("client_id") String clientId, @Req
134137
135138 try {
136139 DeviceCode dc = deviceCodeService .createNewDeviceCode (requestedScopes , client , parameters );
137-
140+
138141 Map <String , Object > response = new HashMap <>();
139142 response .put ("device_code" , dc .getDeviceCode ());
140143 response .put ("user_code" , dc .getUserCode ());
141144 response .put ("verification_uri" , config .getIssuer () + USER_URL );
142145 if (client .getDeviceCodeValiditySeconds () != null ) {
143146 response .put ("expires_in" , client .getDeviceCodeValiditySeconds ());
144147 }
148+
149+ if (config .isAllowCompleteDeviceCodeUri ()) {
150+ URI verificationUriComplete = new URIBuilder (config .getIssuer () + USER_URL )
151+ .addParameter ("user_code" , dc .getUserCode ())
152+ .build ();
153+
154+ response .put ("verification_uri_complete" , verificationUriComplete .toString ());
155+ }
145156
146157 model .put (JsonEntityView .ENTITY , response );
147158
@@ -154,18 +165,31 @@ public String requestDeviceCode(@RequestParam("client_id") String clientId, @Req
154165 model .put (JsonErrorView .ERROR_MESSAGE , dcce .getMessage ());
155166
156167 return JsonErrorView .VIEWNAME ;
168+ } catch (URISyntaxException use ) {
169+ logger .error ("unable to build verification_uri_complete due to wrong syntax of uri components" );
170+ model .put (HttpCodeView .CODE , HttpStatus .INTERNAL_SERVER_ERROR );
171+
172+ return HttpCodeView .VIEWNAME ;
157173 }
158174
159175 }
160176
161177 @ PreAuthorize ("hasRole('ROLE_USER')" )
162178 @ RequestMapping (value = "/" + USER_URL , method = RequestMethod .GET )
163- public String requestUserCode (ModelMap model ) {
179+ public String requestUserCode (@ RequestParam ( value = "user_code" , required = false ) String userCode , ModelMap model , HttpSession session ) {
164180
165- // print out a page that asks the user to enter their user code
166- // user must be logged in
181+ if (!config .isAllowCompleteDeviceCodeUri () || userCode == null ) {
182+ // if we don't allow the complete URI or we didn't get a user code on the way in,
183+ // print out a page that asks the user to enter their user code
184+ // user must be logged in
185+ return "requestUserCode" ;
186+ } else {
167187
168- return "requestUserCode" ;
188+ // complete verification uri was used, we received user code directly
189+ // skip requesting code page
190+ // user must be logged in
191+ return readUserCode (userCode , model , session );
192+ }
169193 }
170194
171195 @ PreAuthorize ("hasRole('ROLE_USER')" )
0 commit comments