moved services and api over to using new client Id field (instead of client_id)
parent
eb5a24690f
commit
155974d8e3
|
@ -29,8 +29,9 @@ public interface OAuth2ClientRepository {
|
||||||
|
|
||||||
public void deleteClient(ClientDetailsEntity client);
|
public void deleteClient(ClientDetailsEntity client);
|
||||||
|
|
||||||
public ClientDetailsEntity updateClient(String clientId, ClientDetailsEntity client);
|
public ClientDetailsEntity updateClient(Long id, ClientDetailsEntity client);
|
||||||
|
|
||||||
public Collection<ClientDetailsEntity> getAllClients();
|
public Collection<ClientDetailsEntity> getAllClients();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,8 @@ import org.springframework.security.oauth2.provider.ClientDetailsService;
|
||||||
|
|
||||||
public interface ClientDetailsEntityService extends ClientDetailsService {
|
public interface ClientDetailsEntityService extends ClientDetailsService {
|
||||||
|
|
||||||
|
public ClientDetailsEntity saveNewClient(ClientDetailsEntity client);
|
||||||
|
|
||||||
public ClientDetailsEntity getClientById(Long id);
|
public ClientDetailsEntity getClientById(Long id);
|
||||||
|
|
||||||
public ClientDetailsEntity loadClientByClientId(String clientId) throws OAuth2Exception;
|
public ClientDetailsEntity loadClientByClientId(String clientId) throws OAuth2Exception;
|
||||||
|
@ -33,7 +35,6 @@ public interface ClientDetailsEntityService extends ClientDetailsService {
|
||||||
|
|
||||||
public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDetailsEntity newClient);
|
public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDetailsEntity newClient);
|
||||||
|
|
||||||
public ClientDetailsEntity saveClient(ClientDetailsEntity client);
|
|
||||||
|
|
||||||
public Collection<ClientDetailsEntity> getAllClients();
|
public Collection<ClientDetailsEntity> getAllClients();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,7 @@ public class JpaOAuth2ClientRepository implements OAuth2ClientRepository {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void deleteClient(ClientDetailsEntity client) {
|
public void deleteClient(ClientDetailsEntity client) {
|
||||||
ClientDetailsEntity found = getClientByClientId(client.getClientId());
|
ClientDetailsEntity found = getById(client.getId());
|
||||||
if (found != null) {
|
if (found != null) {
|
||||||
manager.remove(found);
|
manager.remove(found);
|
||||||
} else {
|
} else {
|
||||||
|
@ -82,8 +82,8 @@ public class JpaOAuth2ClientRepository implements OAuth2ClientRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientDetailsEntity updateClient(String clientId, ClientDetailsEntity client) {
|
public ClientDetailsEntity updateClient(Long id, ClientDetailsEntity client) {
|
||||||
return JpaUtil.saveOrUpdate(clientId, manager, client);
|
return JpaUtil.saveOrUpdate(id, manager, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -51,6 +51,24 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
|
||||||
this.tokenRepository = tokenRepository;
|
this.tokenRepository = tokenRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientDetailsEntity saveNewClient(ClientDetailsEntity client) {
|
||||||
|
if (client.getId() != null) { // if it's not null, it's already been saved, this is an error
|
||||||
|
return null; // TODO: throw exception?
|
||||||
|
}
|
||||||
|
|
||||||
|
// assign a random clientid and secret if they're empty
|
||||||
|
if (client.getClientId() == null || client.getClientId().equals("")) {
|
||||||
|
client.setClientId(UUID.randomUUID().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (client.getClientSecret().equals("")) {
|
||||||
|
client.setClientSecret(UUID.randomUUID().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return clientRepository.saveClient(client);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the client by its internal ID
|
* Get the client by its internal ID
|
||||||
*/
|
*/
|
||||||
|
@ -84,7 +102,7 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
|
||||||
@Override
|
@Override
|
||||||
public void deleteClient(ClientDetailsEntity client) throws InvalidClientException {
|
public void deleteClient(ClientDetailsEntity client) throws InvalidClientException {
|
||||||
|
|
||||||
if (clientRepository.getClientByClientId(client.getClientId()) == null) {
|
if (clientRepository.getById(client.getId()) == null) {
|
||||||
throw new InvalidClientException("Client with id " + client.getClientId() + " was not found");
|
throw new InvalidClientException("Client with id " + client.getClientId() + " was not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,34 +121,11 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
|
||||||
@Override
|
@Override
|
||||||
public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDetailsEntity newClient) throws IllegalArgumentException {
|
public ClientDetailsEntity updateClient(ClientDetailsEntity oldClient, ClientDetailsEntity newClient) throws IllegalArgumentException {
|
||||||
if (oldClient != null && newClient != null) {
|
if (oldClient != null && newClient != null) {
|
||||||
return clientRepository.updateClient(oldClient.getClientId(), newClient);
|
return clientRepository.updateClient(oldClient.getId(), newClient);
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException("Neither old client or new client can be null!");
|
throw new IllegalArgumentException("Neither old client or new client can be null!");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param client object to be saved
|
|
||||||
* @return ClientDetailsEntity the saved object
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public ClientDetailsEntity saveClient(ClientDetailsEntity client) {
|
|
||||||
|
|
||||||
if (client.getClientSecret().equals("")) {
|
|
||||||
client.setClientSecret(UUID.randomUUID().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
// this must be a new client if we don't have a clientID
|
|
||||||
// assign it a new ID
|
|
||||||
if (client.getClientId() == null || client.getClientId().equals("") || this.loadClientByClientId(client.getClientId()) == null) {
|
|
||||||
client.setClientId(UUID.randomUUID().toString());
|
|
||||||
return clientRepository.saveClient(client);
|
|
||||||
} else {
|
|
||||||
return clientRepository.updateClient(client.getClientId(), client);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all clients in the system
|
* Get all clients in the system
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -68,21 +68,23 @@ public class ClientAPI {
|
||||||
// set owners as current logged in user
|
// set owners as current logged in user
|
||||||
//client.setOwner(principal.getName());
|
//client.setOwner(principal.getName());
|
||||||
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
|
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
|
||||||
m.addAttribute("entity", clientService.saveClient(client));
|
m.addAttribute("entity", clientService.saveNewClient(client));
|
||||||
|
|
||||||
return "jsonClientView";
|
return "jsonClientView";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value="/{id}", method = RequestMethod.PUT, headers = "Accept=application/json")
|
@RequestMapping(value="/{id}", method = RequestMethod.PUT, headers = "Accept=application/json")
|
||||||
public String apiUpdateClient(@PathVariable("id") String id, @RequestBody String json, Model m, Principal principal) {
|
public String apiUpdateClient(@PathVariable("id") Long id, @RequestBody String json, Model m, Principal principal) {
|
||||||
|
|
||||||
ClientDetailsEntity client = new Gson().fromJson(json, ClientDetailsEntity.class);
|
ClientDetailsEntity client = new Gson().fromJson(json, ClientDetailsEntity.class);
|
||||||
client.setClientId(id);
|
|
||||||
|
ClientDetailsEntity oldClient = clientService.getClientById(id);
|
||||||
|
|
||||||
// set owners as current logged in user
|
// set owners as current logged in user
|
||||||
// client.setOwner(principal.getName());
|
// client.setOwner(principal.getName());
|
||||||
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
|
//TODO: owner has been replaced by a list of contacts, which should be styled as email addresses.
|
||||||
|
|
||||||
m.addAttribute("entity", clientService.saveClient(client));
|
m.addAttribute("entity", clientService.updateClient(oldClient, client));
|
||||||
|
|
||||||
return "jsonClientView";
|
return "jsonClientView";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue