Create rest endpoint to delete api keys

This commit is contained in:
René Pfeuffer
2020-09-30 08:49:24 +02:00
parent 0923c2d63e
commit 91471c0ec0
8 changed files with 59 additions and 17 deletions

View File

@@ -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");

View File

@@ -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);
}
}

View File

@@ -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());
}
}

View File

@@ -123,7 +123,7 @@ public class MeResource {
return Response.noContent().build();
}
@Path("apiKeys")
@Path("api-keys")
public ApiKeyResource apiKeys() {
return apiKeyResource.get();
}

View File

@@ -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() {

View File

@@ -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() {