com.formkiq.server.service.OAuthServiceImpl Maven / Gradle / Ivy
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formkiq.server.service;
import static com.formkiq.server.dao.ClientDao.CLIENT_NAME;
import java.util.Arrays;
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.StringUtils;
import com.formkiq.server.dao.ClientDao;
import com.formkiq.server.domain.User;
import com.formkiq.server.domain.type.ClientDTO;
import com.formkiq.server.domain.type.ClientListDTO;
/**
* Implementation of OAuthService.
*
*/
@Service
public class OAuthServiceImpl implements OAuthService {
/** PasswordEncoder. */
@Autowired
private PasswordEncoder passwordEncoder;
/** Client Dao. */
@Autowired
private ClientDao clientDao;
/** Client Details Service. */
@Autowired
private JdbcClientDetailsService clientDetailsService;
@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 {
if (this.clientDao.clientCount() > 1) {
this.clientDetailsService.removeClientDetails(clientid);
} else {
throw new NoSuchClientException("Cannot delete only Client");
}
}
@Override
public ClientDTO findClient(final UserDetails ud, final String client) {
User user = (User) ud;
ClientDTO dto = this.clientDao.findClient(user, client);
return dto;
}
@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);
}
}
}