OAuthClientApiServiceImpl.java

/*
 * Copyright 2024 Global Crop Diversity Trust
 *
 * 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 org.gringlobal.api.v2.facade.impl;

import com.querydsl.core.types.Predicate;
import lombok.extern.slf4j.Slf4j;
import org.genesys.blocks.oauth.service.OAuthClientService;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.api.exception.NotFoundElement;
import org.gringlobal.api.model.OAuthClientDTO;
import org.gringlobal.api.v2.facade.OAuthClientApiService;
import org.gringlobal.api.v2.mapper.MapstructMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.AbstractOAuth2TokenAuthenticationToken;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@Slf4j
public class OAuthClientApiServiceImpl implements OAuthClientApiService {

	@Autowired
	private OAuthClientService oAuthClientService;

	@Autowired
	private MapstructMapper mapper;

	@Override
	public OAuthClientDTO getCurrentClient() {
		final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication instanceof AbstractOAuth2TokenAuthenticationToken<?>) {
			var oauthAuth = (AbstractOAuth2TokenAuthenticationToken<?>) authentication;
//			if (oauthAuth.isClientOnly()) {
			log.debug("OAuth client-only authentication: {}", oauthAuth);
			final Object principal = oauthAuth.getPrincipal();
			log.debug("OAuth client-only principal: {} {}", principal.getClass(), principal);
			return mapper.map(oAuthClientService.getClient(oauthAuth.getName()));
//			}
		}
		throw new InvalidApiUsageException("Not using client authentication");
	}

	@Override
	public List<OAuthClientDTO> listClientDetails() {
		return mapper.map(oAuthClientService.listClientDetails(), mapper::map);
	}

	@Override
	public Page<OAuthClientDTO> listClientDetails(Pageable pageable) {
		return mapper.map(oAuthClientService.listClientDetails(pageable), mapper::map);
	}

	@Override
	public Page<OAuthClientDTO> listClientDetails(Predicate predicate, Pageable pageable) {
		return mapper.map(oAuthClientService.listClientDetails(predicate, pageable), mapper::map);
	}

	@Override
	public OAuthClientDTO getClient(String clientId) {
		return mapper.map(oAuthClientService.getClient(clientId));
	}

	@Override
	@Transactional
	public OAuthClientDTO addClient(OAuthClientDTO client) {
		return mapper.map(oAuthClientService.addClient(mapper.map(client)));
	}

	@Override
	@Transactional
	public OAuthClientDTO updateClient(long id, int version, OAuthClientDTO updates) {
		return mapper.map(oAuthClientService.getClient(oAuthClientService.updateClient(id, version, mapper.map(updates)).getClientId()));
	}

	@Override
	@Transactional
	public OAuthClientDTO updateClientId(String sourceId, String targetId) {
		return mapper.map(oAuthClientService.loadClientByClientId(oAuthClientService.updateClientId(sourceId, targetId).getClientId()));
	}

	@Override
	@Transactional
	public OAuthClientDTO removeClient(OAuthClientDTO oauthClient) {
		return mapper.map(oAuthClientService.removeClient(mapper.map(oauthClient)));
	}

	@Override
	public List<OAuthClientDTO> autocompleteClients(String title, int limit) {
		return mapper.map(oAuthClientService.autocompleteClients(title, limit), mapper::map);
	}

	@Override
	@Transactional
	public String resetSecret(String clientId) {
		var client = oAuthClientService.getClient(clientId);
		if (client == null) {
			throw new NotFoundElement("No client found with id=" + clientId);
		}
		return oAuthClientService.resetSecret(client);
	}

	@Override
	@Transactional
	public String setSecret(OAuthClientDTO oauthClient, String clientSecret) {
		return oAuthClientService.setSecret(mapper.map(oauthClient), clientSecret);
	}

	@Override
	@Transactional
	public OAuthClientDTO removeSecret(String clientId) {
		var client = oAuthClientService.getClient(clientId);
		if (client == null) {
			throw new NotFoundElement("No client found with id=" + clientId);
		}
		return mapper.map(oAuthClientService.removeSecret(client));
	}

	@Override
	public boolean isOriginRegistered(String origin) {
		return oAuthClientService.isOriginRegistered(origin);
	}

	@Override
	public OAuthClientDTO loadClientByClientId(String clientId) {
		return mapper.map(oAuthClientService.loadClientByClientId(clientId));
	}

	@Override
	public boolean isClientActive(String clientId) {
		return oAuthClientService.isClientActive(clientId);
	}

	@Override
	@Transactional
	public OAuthClientDTO update(OAuthClientDTO source) {
		var client = oAuthClientService.getClient(source.getClientId());
		if (client == null) {
			throw new NotFoundElement("No client found with id=" + source.getClientId());
		}
		return updateClient(client.getId(), source.getVersion(), source);
	}

	@Override
	@Transactional
	public OAuthClientDTO remove(String clientId) {
		var client = oAuthClientService.getClient(clientId);
		if (client == null) {
			throw new NotFoundElement("No client found with id=" + clientId);
		}
		log.info("Removing client {}", client.getClientId());
		return mapper.map(oAuthClientService.removeClient(client));
	}
}