mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-04 07:49:00 +02:00
Create rest endpoint to delete api keys
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.api;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -42,7 +42,7 @@ public class NotSupportedExceptionMapper implements ExceptionMapper<NotSupported
|
||||
|
||||
@Override
|
||||
public Response toResponse(NotSupportedException exception) {
|
||||
LOG.debug("illegal media type");
|
||||
LOG.debug("illegal media type", exception);
|
||||
ErrorDto error = new ErrorDto();
|
||||
error.setTransactionId(MDC.get("transaction_id"));
|
||||
error.setMessage("illegal media type");
|
||||
|
||||
@@ -38,6 +38,7 @@ import sonia.scm.web.VndMediaType;
|
||||
import javax.inject.Inject;
|
||||
import javax.validation.Valid;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.DELETE;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
@@ -129,6 +130,7 @@ public class ApiKeyResource {
|
||||
@POST
|
||||
@Path("")
|
||||
@Consumes(VndMediaType.API_KEY)
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
@Operation(summary = "Create new api key for the current user", description = "Creates a new api key for the given user with the role specified in the given key.", tags = "User")
|
||||
@ApiResponse(
|
||||
responseCode = "201",
|
||||
@@ -158,4 +160,14 @@ public class ApiKeyResource {
|
||||
.location(URI.create(resourceLinks.apiKey().self(newKey.getId())))
|
||||
.build();
|
||||
}
|
||||
|
||||
@DELETE
|
||||
@Path("{id}")
|
||||
@Operation(summary = "Delete api key", description = "Deletes the api key with the given id for the current user.", tags = "User")
|
||||
@ApiResponse(responseCode = "204", description = "delete success or nothing to delete")
|
||||
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
|
||||
@ApiResponse(responseCode = "500", description = "internal server error")
|
||||
public void delete(@PathParam("id") String id) {
|
||||
apiKeyService.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,17 +31,21 @@ import sonia.scm.security.ApiKey;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static de.otto.edison.hal.Link.link;
|
||||
|
||||
@Mapper
|
||||
public abstract class ApiKeyToApiKeyDtoMapper {
|
||||
|
||||
@Inject
|
||||
private ResourceLinks links;
|
||||
private ResourceLinks resourceLinks;
|
||||
|
||||
abstract ApiKeyDto map(ApiKey key);
|
||||
|
||||
@ObjectFactory
|
||||
ApiKeyDto createDto(ApiKey key) {
|
||||
Links.linkingTo().self(links.apiKey().self(key.getId()));
|
||||
return new ApiKeyDto(Links.linkingTo().self(links.apiKey().self(key.getId())).build());
|
||||
Links.Builder links = Links.linkingTo()
|
||||
.self(resourceLinks.apiKey().self(key.getId()))
|
||||
.single(link("delete", resourceLinks.apiKey().delete(key.getId())));
|
||||
return new ApiKeyDto(links.build());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ public class MeResource {
|
||||
return Response.noContent().build();
|
||||
}
|
||||
|
||||
@Path("apiKeys")
|
||||
@Path("api-keys")
|
||||
public ApiKeyResource apiKeys() {
|
||||
return apiKeyResource.get();
|
||||
}
|
||||
|
||||
@@ -238,6 +238,10 @@ class ResourceLinks {
|
||||
String self(String id) {
|
||||
return apiKeyLinkBuilder.method("apiKeys").parameters().method("get").parameters(id).href();
|
||||
}
|
||||
|
||||
String delete(String id) {
|
||||
return apiKeyLinkBuilder.method("apiKeys").parameters().method("delete").parameters(id).href();
|
||||
}
|
||||
}
|
||||
|
||||
UserCollectionLinks userCollection() {
|
||||
|
||||
@@ -41,6 +41,7 @@ import java.util.Optional;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
@@ -140,7 +141,12 @@ public class ApiKeyService {
|
||||
}
|
||||
|
||||
public Collection<ApiKey> getKeys() {
|
||||
return store.get(currentUser()).getKeys().stream().map(ApiKey::new).collect(toList());
|
||||
return store.getOptional(currentUser())
|
||||
.map(ApiKeyCollection::getKeys)
|
||||
.map(Collection::stream)
|
||||
.orElse(Stream.empty())
|
||||
.map(ApiKey::new)
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
private String currentUser() {
|
||||
|
||||
Reference in New Issue
Block a user