com.formkiq.server.service.OAuthServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
package com.formkiq.server.service;
import static com.formkiq.server.dao.UserDao.CLIENT_NAME;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientAlreadyExistsException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.stereotype.Service;
import com.formkiq.server.dao.ClientDao;
import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.ClientListDTO;
/**
* Implementation of OAuthService.
*
*/
@Service
public class OAuthServiceImpl implements OAuthService {
/** JdbcClientDetailsService. */
@Autowired
private JdbcClientDetailsService clientDetailsService;
/** PasswordEncoder. */
@Autowired
private PasswordEncoder passwordEncoder;
/** Client Dao. */
@Autowired
private ClientDao clientDao;
@Override
public void addClientDetails(final String clientName, final String clientId,
final String clientSecret) throws ClientAlreadyExistsException {
BaseClientDetails detail = new BaseClientDetails();
detail.setClientId(clientId);
detail.setClientSecret(clientSecret);
detail.addAdditionalInformation(CLIENT_NAME, clientName);
detail.setAuthorizedGrantTypes(Arrays.asList("authorization_code",
"password", "refresh_token"));
detail.setScope(Arrays.asList("read/write"));
this.clientDetailsService.addClientDetails(detail);
}
@Override
public int clientCount() {
return this.clientDao.clientCount();
}
@Override
public boolean isValidClient(final String clientid) {
boolean valid;
try {
ClientDetails details = this.clientDetailsService
.loadClientByClientId(clientid);
valid = details != null;
} catch (InvalidClientException | NoSuchClientException e) {
valid = false;
}
return valid;
}
@Override
public boolean isValidClient(final String clientid,
final String clientSecret) {
boolean valid;
try {
ClientDetails details = this.clientDetailsService
.loadClientByClientId(clientid);
valid = this.passwordEncoder.matches(clientSecret,
details.getClientSecret());
} catch (InvalidClientException | NoSuchClientException e) {
valid = false;
}
return valid;
}
@Override
public ClientListDTO list() {
List dtos = new ArrayList();
List clients = this.clientDetailsService
.listClientDetails();
for (ClientDetails details : clients) {
String name = details.getAdditionalInformation().get(CLIENT_NAME)
.toString();
dtos.add(new ClientDTO(name, details.getClientId()));
}
ClientListDTO dto = new ClientListDTO();
dto.setClients(dtos);
return dto;
}
}