internalized random string generation for device codes

pull/972/merge
Justin Richer 8 years ago
parent 52829d4adb
commit cc0622edd0

@ -66,7 +66,7 @@ public interface DeviceCodeService {
* @param parameters * @param parameters
* @return * @return
*/ */
public DeviceCode createNewDeviceCode(String deviceCode, String userCode, Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters); public DeviceCode createNewDeviceCode(Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters);
public void clearExpiredDeviceCodes(); public void clearExpiredDeviceCodes();

@ -21,6 +21,7 @@ import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID;
import org.mitre.data.AbstractPageOperationTemplate; import org.mitre.data.AbstractPageOperationTemplate;
import org.mitre.oauth2.model.AuthenticationHolderEntity; import org.mitre.oauth2.model.AuthenticationHolderEntity;
@ -29,6 +30,7 @@ import org.mitre.oauth2.model.DeviceCode;
import org.mitre.oauth2.repository.impl.DeviceCodeRepository; import org.mitre.oauth2.repository.impl.DeviceCodeRepository;
import org.mitre.oauth2.service.DeviceCodeService; import org.mitre.oauth2.service.DeviceCodeService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.util.RandomValueStringGenerator;
import org.springframework.security.oauth2.provider.ClientDetails; import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -44,11 +46,19 @@ public class DefaultDeviceCodeService implements DeviceCodeService {
@Autowired @Autowired
private DeviceCodeRepository repository; private DeviceCodeRepository repository;
private RandomValueStringGenerator randomGenerator = new RandomValueStringGenerator();
/* (non-Javadoc) /* (non-Javadoc)
* @see org.mitre.oauth2.service.DeviceCodeService#save(org.mitre.oauth2.model.DeviceCode) * @see org.mitre.oauth2.service.DeviceCodeService#save(org.mitre.oauth2.model.DeviceCode)
*/ */
@Override @Override
public DeviceCode createNewDeviceCode(String deviceCode, String userCode, Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters) { public DeviceCode createNewDeviceCode(Set<String> requestedScopes, ClientDetailsEntity client, Map<String, String> parameters) {
// create a device code, should be big and random
String deviceCode = UUID.randomUUID().toString();
// create a user code, should be random but small and typable
String userCode = randomGenerator.generate();
DeviceCode dc = new DeviceCode(deviceCode, userCode, requestedScopes, client.getClientId(), parameters); DeviceCode dc = new DeviceCode(deviceCode, userCode, requestedScopes, client.getClientId(), parameters);

@ -91,8 +91,6 @@ public class DeviceEndpoint {
@Autowired @Autowired
private OAuth2RequestFactory oAuth2RequestFactory; private OAuth2RequestFactory oAuth2RequestFactory;
private RandomValueStringGenerator randomGenerator = new RandomValueStringGenerator();
@RequestMapping(value = "/" + URL, method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @RequestMapping(value = "/" + URL, method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public String requestDeviceCode(@RequestParam("client_id") String clientId, @RequestParam(name="scope", required=false) String scope, Map<String, String> parameters, ModelMap model) { public String requestDeviceCode(@RequestParam("client_id") String clientId, @RequestParam(name="scope", required=false) String scope, Map<String, String> parameters, ModelMap model) {
@ -134,17 +132,11 @@ public class DeviceEndpoint {
// if we got here the request is legit // if we got here the request is legit
// create a device code, should be big and random DeviceCode dc = deviceCodeService.createNewDeviceCode(requestedScopes, client, parameters);
String deviceCode = UUID.randomUUID().toString();
// create a user code, should be random but small and typable
String userCode = randomGenerator.generate();
deviceCodeService.createNewDeviceCode(deviceCode, userCode, requestedScopes, client, parameters);
Map<String, Object> response = new HashMap<>(); Map<String, Object> response = new HashMap<>();
response.put("device_code", deviceCode); response.put("device_code", dc.getDeviceCode());
response.put("user_code", userCode); response.put("user_code", dc.getUserCode());
response.put("verification_uri", config.getIssuer() + USER_URL); response.put("verification_uri", config.getIssuer() + USER_URL);
if (client.getDeviceCodeValiditySeconds() != null) { if (client.getDeviceCodeValiditySeconds() != null) {
response.put("expires_in", client.getDeviceCodeValiditySeconds()); response.put("expires_in", client.getDeviceCodeValiditySeconds());

Loading…
Cancel
Save