
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
/*
* 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 java.util.List;
import java.util.stream.Collectors;
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;
import com.formkiq.server.domain.type.OAuthGrantTypes;
/**
* 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, final List grantTypes)
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");
}
if (StringUtils.isEmpty(grantTypes)) {
throw new ClientRegistrationException("Grant Type(s) are required");
}
detail.setClientId(clientId);
detail.setClientSecret(clientSecret);
detail.addAdditionalInformation(CLIENT_NAME, clientName);
detail.setAuthorizedGrantTypes(toString(grantTypes));
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, final List grantTypes) {
try {
BaseClientDetails detail = (BaseClientDetails)
this.clientDetailsService.loadClientByClientId(clientId);
if (!StringUtils.isEmpty(clientName)) {
detail.addAdditionalInformation(CLIENT_NAME, clientName);
}
detail.setAuthorizedGrantTypes(toString(grantTypes));
this.clientDetailsService.updateClientDetails(detail);
if (!StringUtils.isEmpty(clientSecret)) {
this.clientDetailsService.updateClientSecret(clientId,
clientSecret);
}
} catch (InvalidClientException | NoSuchClientException e) {
addClientDetails(clientName, clientId, clientSecret, grantTypes);
}
}
/**
* Convert OAuthGrantTypes to String.
* @param grantTypes {@link List}
* @return {@link List}
*/
private List toString(final List grantTypes) {
return grantTypes.stream()
.map(i -> i.name().toLowerCase()).collect(Collectors.toList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy