![JAR search and dependency download from the Maven repository](/logo.png)
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 greeting Show documentation
Show all versions of greeting Show documentation
Server-side integration for the FormKiQ ios application
The newest version!
package com.formkiq.server.service;
import static com.formkiq.server.dao.ClientDao.CLIENT_NAME;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
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.ClientRegistrationException;
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 org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import com.formkiq.server.dao.ClientDao;
import com.formkiq.server.dao.FormDao;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.ClientFormType;
import com.formkiq.server.domain.type.ClientListDTO;
import com.formkiq.server.domain.type.FormDTO;
/**
* 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;
/** Form Dao. */
@Autowired
private FormDao formDao;
@Override
public void addClientDetails(final String clientName, final String clientId,
final String clientSecret) throws ClientAlreadyExistsException {
BaseClientDetails detail = new BaseClientDetails();
if (StringUtils.isEmpty(clientName)) {
throw new ClientRegistrationException("Client Name is required");
}
if (StringUtils.isEmpty(clientSecret)) {
throw new ClientRegistrationException("Client Secret is required");
}
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 void deleteClient(final String clientid)
throws NoSuchClientException {
List forms = this.formDao
.findForms(ClientFormType.FORM, clientid, null).getForms();
if (CollectionUtils.isEmpty(forms)) {
this.clientDetailsService.removeClientDetails(clientid);
} else {
throw new NoSuchClientException("Client has Forms");
}
}
@Override
public ClientDTO findClient(final UserDetails user, final String client) {
return this.clientDao.findClient((User) user, client);
}
@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(final String token) {
return this.clientDao.findClients(token);
}
@Override
public void save(final String clientName, final String clientId,
final String clientSecret) {
try {
BaseClientDetails detail = (BaseClientDetails)
this.clientDetailsService.loadClientByClientId(clientId);
if (!StringUtils.isEmpty(clientName)) {
detail.addAdditionalInformation(CLIENT_NAME, clientName);
}
this.clientDetailsService.updateClientDetails(detail);
if (!StringUtils.isEmpty(clientSecret)) {
this.clientDetailsService.updateClientSecret(clientId,
clientSecret);
}
} catch (InvalidClientException | NoSuchClientException e) {
addClientDetails(clientName, clientId, clientSecret);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy