OAuthManagementController.java

/*
 * Copyright 2020 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.v1.impl;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.genesys.blocks.oauth.model.OAuthClient;
import org.genesys.blocks.oauth.service.OAuthClientService;
import org.gringlobal.api.exception.NotFoundElement;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.service.ShortFilterService;
import org.gringlobal.service.filter.OAuthClientFilter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * Allow administrators to manage OAuth clients and keys.
 */
@RestController("oauthManagementApi1")
@RequestMapping(OAuthManagementController.API_URL)
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
@Tag(name = "UserAdmin")
@Slf4j
public class OAuthManagementController extends ApiBaseController {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv1_ADMIN_BASE + "/oauth-clients";

	public static final String ENDPOINT_CLIENT_ID = "/{clientId:.+}";

	/**
	 * The short filter service.
	 */
	@Autowired
	private ShortFilterService shortFilterService;

	@Autowired
	private OAuthClientService clientDetailsService;

	/**
	 * Get OAuth client by clientId
	 *
	 * @param clientId the clientId
	 * @return the loaded OAuth client
	 */
	@GetMapping(value = ENDPOINT_CLIENT_ID)
	@Operation(operationId = "getOauthClient", summary = "Get", description = "Get OAuth client by clientId")
	public OAuthClient get(@PathVariable(value = "clientId") final String clientId) throws NotFoundElement {
		OAuthClient client = clientDetailsService.getClient(clientId);
		if (client == null) {
			throw new NotFoundElement("No client found with id=" + clientId);
		}
		return client;
	}

	/**
	 * Register a new OAuth client.
	 *
	 * @param source the source
	 * @return the recorded OAuth client
	 */
	@PostMapping(value = "")
	@Operation(operationId = "addOauthClient", summary = "Create", description = "Add new OAuth client")
	public OAuthClient create(@RequestBody OAuthClient source) {
		OAuthClient oauthClient = clientDetailsService.addClient(source);
		log.info("Created OAuth client with clientId={}", oauthClient.getClientId());
		return oauthClient;
	}

	/**
	 * Update a client.
	 *
	 * @param source the source
	 * @return the updated OAuth client
	 */
	@PutMapping(value = "")
	@Operation(operationId = "updateOauthClient", summary = "Update", description = "Update OAuth client")
	public OAuthClient update(@RequestBody OAuthClient source) throws NotFoundElement  {
		OAuthClient client = get(source.getClientId());
		return clientDetailsService.updateClient(client.getId(), source.getVersion(), source);
	}

	/**
	 * Update a clientId.
	 *
	 * @param sourceClientId the sourceClientId
	 * @param targetClientId the sourceClientId
	 * @return the updated OAuth client
	 */
	@PostMapping(value = "/update-clientId")
	@Operation(operationId = "updateOauthClient", summary = "Update", description = "Update OAuth client")
	public OAuthClient updateClientId(@RequestParam String sourceClientId, @RequestParam String targetClientId) throws NotFoundElement  {
		OAuthClient client = get(sourceClientId);
		return clientDetailsService.updateClientId(client.getClientId(), targetClientId);
	}

	/**
	 * Remove the OAuth client.
	 *
	 * @param clientId the clientId
	 * @return the removed client
	 */
	@DeleteMapping(value = ENDPOINT_CLIENT_ID)
	@Operation(operationId = "removeOauthClient", summary = "Delete", description = "Remove OAuth client")
	public OAuthClient remove(@PathVariable("clientId") final String clientId) throws NotFoundElement {
		OAuthClient client = get(clientId);
		log.info("Removing client {}", client.getClientId());
		return clientDetailsService.removeClient(client);
	}

	/**
	 * Generate a new secret for OAuth client.
	 *
	 * @param clientId the clientId
	 * @return the secret
	 */
	@PostMapping(value = ENDPOINT_CLIENT_ID + "/secret")
	@Operation(operationId = "generateSecret", summary = "Regenerate  secret", description = "Generate a new secret for OAuth client")
	public String generateSecret(@PathVariable(value = "clientId") final String clientId) throws NotFoundElement {
		OAuthClient client = get(clientId);
		return clientDetailsService.resetSecret(client);
	}

	/**
	 * Set a new secret for OAuth client.
	 *
	 * @param clientId the clientId
	 * @param secret the secret
	 * @return the HTTP status
	 */
	@PutMapping(value = ENDPOINT_CLIENT_ID + "/secret")
	public ResponseEntity<HttpStatus> setSecret(@PathVariable("clientId") String clientId, @RequestBody(required = true) String secret) {
		OAuthClient oauthClient = clientDetailsService.getClient(clientId);
		clientDetailsService.setSecret(oauthClient, secret);
		return ResponseEntity.ok().build();
	}

	/**
	 * Remove the secret.
	 *
	 * @param clientId the clientId
	 * @return the updated client
	 */
	@DeleteMapping(value = ENDPOINT_CLIENT_ID + "/secret")
	@Operation(operationId = "clearSecret", summary = "Clear secret", description = "Clear the secret for OAuth client")
	public OAuthClient removeSecret(@PathVariable(value = "clientId") final String clientId) throws NotFoundElement {
		clientDetailsService.removeSecret(get(clientId));
		return get(clientId);
	}

	/**
	 * Filter OAuth clients.
	 *
	 * @param filterCode the filter code
	 * @param page the page
	 * @param filter the filter
	 * @return the filtered page
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	@PostMapping(value = "/filter")
	@Operation(operationId = "listClients", summary = "List", description = "List OAuth clients")
	public FilteredPage<OAuthClient, OAuthClientFilter> listClients(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) OAuthClientFilter filter) throws IOException {

		if (filterCode != null) {
			filter = shortFilterService.filterByCode(filterCode, OAuthClientFilter.class);
		} else if (filter != null){
			filterCode = shortFilterService.getCode(filter);
		} else {
			filter = new OAuthClientFilter();
		}
		
		ShortFilterService.FilterInfo<OAuthClientFilter> filterInfo = shortFilterService.processFilter(filterCode, filter, OAuthClientFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, clientDetailsService.listClientDetails(filterInfo.filter.buildPredicate(), page.toPageRequest(100, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "clientId")));
	}

	@PostMapping(value = "/{clientId}/set-recaptcha-keys")
	public @ResponseBody boolean setRecaptchaKeys(@PathVariable("clientId") String clientId, @RequestParam("privateKey") String privateKey,
		@RequestParam("publicKey") String publicKey) {

		OAuthClient oauthClient = clientDetailsService.getClient(clientId);
		if (oauthClient == null) {
			throw new NotFoundElement("No such client");
		}
		oauthClient.setPrivateRecaptchaKey(privateKey);
		oauthClient.setPublicRecaptchaKey(publicKey);
		clientDetailsService.updateClient(oauthClient.getId(), oauthClient.getVersion(), oauthClient);
		return true;
	}
}