ApiTokenController.java

/*
 * Copyright 2025 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.genesys.server.api.v2.impl;

import io.swagger.annotations.Api;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.ApiTokenApiService;
import org.genesys.server.api.v2.model.impl.ApiTokenDTO;
import org.genesys.server.api.v2.model.impl.ApiTokenInfo;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
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.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController("apiTokenApi2")
@RequestMapping(ApiTokenController.API_URL)
@Api(tags = { "apiToken" })
@PreAuthorize("isFullyAuthenticated()")
public class ApiTokenController extends ApiBaseController {

	public static final String API_URL = APIv2_BASE + "/api-token";

	@Autowired
	private ApiTokenApiService apiTokenService;

	@PostMapping(value = "/user/generate", produces = MediaType.APPLICATION_JSON_VALUE)
	@PreAuthorize("hasRole('ADMINISTRATOR') || hasRole('VETTEDUSER')")
	public ApiTokenDTO generateUserToken(@RequestParam(required = false) String label) {
		return apiTokenService.generateUserToken(label);
	}

	@PostMapping(value = "/user/{userId}/generate", produces = MediaType.APPLICATION_JSON_VALUE)
	@PreAuthorize("hasRole('ADMINISTRATOR') || (hasRole('VETTEDUSER') && #userId == principal.id)")
	public ApiTokenDTO generateTokenForUser(@PathVariable Long userId, @RequestParam(required = false) String label) {
		return apiTokenService.generateTokenForUser(userId, label);
	}

	@PostMapping(value = "/client/{clientId}/generate", produces = MediaType.APPLICATION_JSON_VALUE)
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	public ApiTokenDTO generateTokenForClient(@PathVariable String clientId, @RequestParam(required = false) String label) {
		return apiTokenService.generateTokenForClient(clientId, label);
	}

	@GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	@PreAuthorize("hasRole('ADMINISTRATOR')")
	public Page<ApiTokenInfo> getAllTokens(@ParameterObject final Pagination page) {
		Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
		return apiTokenService.getAllTokens(pageable);
	}

	@GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<ApiTokenInfo> getMyTokens() {
		return apiTokenService.getMyTokens();
	}

	@PutMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	@PreAuthorize("hasRole('ADMINISTRATOR') || hasRole('VETTEDUSER')")
	public ApiTokenInfo update(@RequestBody @NotNull final ApiTokenInfo apiToken) {
		return apiTokenService.update(apiToken);
	}

	@DeleteMapping(value = "/{id}")
	@PreAuthorize("hasRole('ADMINISTRATOR') || hasRole('VETTEDUSER')")
	public ApiTokenInfo removeToken(@PathVariable Long id) {
		return apiTokenService.removeToken(id);
	}

}