@Override 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, and always uppercase (lookup is case insensitive) String userCode = randomGenerator.generate().toUpperCase(); DeviceCode dc = new DeviceCode(deviceCode, userCode, requestedScopes, client.getClientId(), parameters); if (client.getDeviceCodeValiditySeconds() != null) { dc.setExpiration(new Date(System.currentTimeMillis() + client.getDeviceCodeValiditySeconds() * 1000L)); } dc.setApproved(false); return repository.save(dc); }
@Override protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) { String deviceCode = tokenRequest.getRequestParameters().get("device_code"); // look up the device code and consume it DeviceCode dc = deviceCodeService.findDeviceCode(deviceCode, client); if (dc != null) { // make sure the code hasn't expired yet if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) { deviceCodeService.clearDeviceCode(deviceCode, client); throw new DeviceCodeExpiredException("Device code has expired " + deviceCode); } else if (!dc.isApproved()) { // still waiting for approval throw new AuthorizationPendingException("Authorization pending for code " + deviceCode); } else { // inherit the (approved) scopes from the original request tokenRequest.setScope(dc.getScope()); OAuth2Authentication auth = new OAuth2Authentication(getRequestFactory().createOAuth2Request(client, tokenRequest), dc.getAuthenticationHolder().getUserAuth()); deviceCodeService.clearDeviceCode(deviceCode, client); return auth; } } else { throw new InvalidGrantException("Invalid device code: " + deviceCode); } }
if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) { model.addAttribute("error", "expiredUserCode"); return "requestUserCode"; if (dc.isApproved()) { model.addAttribute("error", "userCodeAlreadyApproved"); return "requestUserCode"; ClientDetailsEntity client = clientService.loadClientByClientId(dc.getClientId()); Set<SystemScope> scopes = scopeService.fromStrings(dc.getScope()); AuthorizationRequest authorizationRequest = oAuth2RequestFactory.createAuthorizationRequest(dc.getRequestParameters());
@Override public DeviceCode approveDeviceCode(DeviceCode dc, OAuth2Authentication auth) { DeviceCode found = repository.getById(dc.getId()); found.setApproved(true); AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(auth); found.setAuthenticationHolder(authHolder); return repository.save(found); }
if (!dc.getUserCode().equals(userCode)) { model.addAttribute("error", "userCodeMismatch"); return "requestUserCode"; if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) { model.addAttribute("error", "expiredUserCode"); return "requestUserCode"; ClientDetailsEntity client = clientService.loadClientByClientId(dc.getClientId()); Set<SystemScope> scopes = scopeService.fromStrings(dc.getScope());
response.put("device_code", dc.getDeviceCode()); response.put("user_code", dc.getUserCode()); response.put("verification_uri", config.getIssuer() + USER_URL); if (client.getDeviceCodeValiditySeconds() != null) { .addParameter("user_code", dc.getUserCode()) .build();
@Override @Transactional(value="defaultTransactionManager") public void remove(DeviceCode scope) { DeviceCode found = getById(scope.getId()); if (found != null) { em.remove(found); } }
@Override public DeviceCode findDeviceCode(String deviceCode, ClientDetails client) { DeviceCode found = repository.getByDeviceCode(deviceCode); if (found != null) { if (found.getClientId().equals(client.getClientId())) { // make sure the client matches, if so, we're good return found; } else { // if the clients don't match, pretend the code wasn't found return null; } } else { // didn't find the code, return null return null; } }
if (!dc.getUserCode().equals(userCode)) { model.addAttribute("error", "userCodeMismatch"); return "requestUserCode"; if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) { model.addAttribute("error", "expiredUserCode"); return "requestUserCode"; ClientDetailsEntity client = clientService.loadClientByClientId(dc.getClientId()); Set<SystemScope> scopes = scopeService.fromStrings(dc.getScope());
@Override public DeviceCode approveDeviceCode(DeviceCode dc, OAuth2Authentication auth) { DeviceCode found = repository.getById(dc.getId()); found.setApproved(true); AuthenticationHolderEntity authHolder = new AuthenticationHolderEntity(); authHolder.setAuthentication(auth); found.setAuthenticationHolder(authHolder); return repository.save(found); }
response.put("device_code", dc.getDeviceCode()); response.put("user_code", dc.getUserCode()); response.put("verification_uri", config.getIssuer() + USER_URL); if (client.getDeviceCodeValiditySeconds() != null) {
@Override @Transactional(value="defaultTransactionManager") public DeviceCode save(DeviceCode scope) { return saveOrUpdate(scope.getId(), em, scope); }
@Override public DeviceCode findDeviceCode(String deviceCode, ClientDetails client) { DeviceCode found = repository.getByDeviceCode(deviceCode); if (found != null) { if (found.getClientId().equals(client.getClientId())) { // make sure the client matches, if so, we're good return found; } else { // if the clients don't match, pretend the code wasn't found return null; } } else { // didn't find the code, return null return null; } }
if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) { model.addAttribute("error", "expiredUserCode"); return "requestUserCode"; if (dc.isApproved()) { model.addAttribute("error", "userCodeAlreadyApproved"); return "requestUserCode"; ClientDetailsEntity client = clientService.loadClientByClientId(dc.getClientId()); Set<SystemScope> scopes = scopeService.fromStrings(dc.getScope()); AuthorizationRequest authorizationRequest = oAuth2RequestFactory.createAuthorizationRequest(dc.getRequestParameters());
@Override protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) { String deviceCode = tokenRequest.getRequestParameters().get("device_code"); // look up the device code and consume it DeviceCode dc = deviceCodeService.findDeviceCode(deviceCode, client); if (dc != null) { // make sure the code hasn't expired yet if (dc.getExpiration() != null && dc.getExpiration().before(new Date())) { deviceCodeService.clearDeviceCode(deviceCode, client); throw new DeviceCodeExpiredException("Device code has expired " + deviceCode); } else if (!dc.isApproved()) { // still waiting for approval throw new AuthorizationPendingException("Authorization pending for code " + deviceCode); } else { // inherit the (approved) scopes from the original request tokenRequest.setScope(dc.getScope()); OAuth2Authentication auth = new OAuth2Authentication(getRequestFactory().createOAuth2Request(client, tokenRequest), dc.getAuthenticationHolder().getUserAuth()); deviceCodeService.clearDeviceCode(deviceCode, client); return auth; } } else { throw new InvalidGrantException("Invalid device code: " + deviceCode); } }
@Override 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, and always uppercase (lookup is case insensitive) String userCode = randomGenerator.generate().toUpperCase(); DeviceCode dc = new DeviceCode(deviceCode, userCode, requestedScopes, client.getClientId(), parameters); if (client.getDeviceCodeValiditySeconds() != null) { dc.setExpiration(new Date(System.currentTimeMillis() + client.getDeviceCodeValiditySeconds() * 1000L)); } dc.setApproved(false); return repository.save(dc); }
@Override @Transactional(value="defaultTransactionManager") public void remove(DeviceCode scope) { DeviceCode found = getById(scope.getId()); if (found != null) { em.remove(found); } }
@Override @Transactional(value="defaultTransactionManager") public DeviceCode save(DeviceCode scope) { return saveOrUpdate(scope.getId(), em, scope); }