mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-06 12:20:56 +01:00
Add created date to api key
This commit is contained in:
@@ -31,6 +31,7 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.time.Instant;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@@ -40,6 +41,7 @@ public class ApiKeyDto extends HalRepresentation {
|
||||
private String displayName;
|
||||
@NotEmpty
|
||||
private String permissionRole;
|
||||
private Instant created;
|
||||
|
||||
public ApiKeyDto(Links links) {
|
||||
super(links);
|
||||
|
||||
@@ -27,14 +27,22 @@ package sonia.scm.security;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class ApiKey {
|
||||
private final String id;
|
||||
private final String displayName;
|
||||
private final String permissionRole;
|
||||
private final Instant created;
|
||||
|
||||
ApiKey(ApiKeyWithPassphrase apiKeyWithPassphrase) {
|
||||
this(apiKeyWithPassphrase.getId(), apiKeyWithPassphrase.getDisplayName(), apiKeyWithPassphrase.getPermissionRole());
|
||||
this(
|
||||
apiKeyWithPassphrase.getId(),
|
||||
apiKeyWithPassphrase.getDisplayName(),
|
||||
apiKeyWithPassphrase.getPermissionRole(),
|
||||
apiKeyWithPassphrase.getCreated()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.time.Instant.now;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.apache.commons.lang.RandomStringUtils.random;
|
||||
@@ -79,7 +80,7 @@ public class ApiKeyService {
|
||||
String passphrase = passphraseGenerator.get();
|
||||
String hashedPassphrase = passwordService.encryptPassword(passphrase);
|
||||
final String id = keyGenerator.createKey();
|
||||
ApiKeyWithPassphrase key = new ApiKeyWithPassphrase(id, name, permissionRole, hashedPassphrase);
|
||||
ApiKeyWithPassphrase key = new ApiKeyWithPassphrase(id, name, permissionRole, hashedPassphrase, now());
|
||||
Lock lock = locks.get(user).writeLock();
|
||||
lock.lock();
|
||||
try {
|
||||
|
||||
@@ -28,10 +28,13 @@ import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import sonia.scm.xml.XmlInstantAdapter;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAccessType;
|
||||
import javax.xml.bind.annotation.XmlAccessorType;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
||||
import java.time.Instant;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@@ -44,4 +47,6 @@ class ApiKeyWithPassphrase {
|
||||
@XmlElement(name = "permission-role")
|
||||
private String permissionRole;
|
||||
private String passphrase;
|
||||
@XmlJavaTypeAdapter(XmlInstantAdapter.class)
|
||||
private Instant created;
|
||||
}
|
||||
|
||||
@@ -53,12 +53,12 @@ import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static com.google.inject.util.Providers.of;
|
||||
import static java.time.Instant.now;
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
@@ -229,7 +229,10 @@ public class MeResourceTest {
|
||||
|
||||
@Test
|
||||
public void shouldGetAllApiKeys() throws URISyntaxException, UnsupportedEncodingException {
|
||||
when(apiKeyService.getKeys()).thenReturn(Arrays.asList(new ApiKey("1", "key 1", "READ"), new ApiKey("2", "key 2", "WRITE")));
|
||||
when(apiKeyService.getKeys())
|
||||
.thenReturn(asList(
|
||||
new ApiKey("1", "key 1", "READ", now()),
|
||||
new ApiKey("2", "key 2", "WRITE", now())));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + MeResource.ME_PATH_V2 + "api_keys");
|
||||
dispatcher.invoke(request, response);
|
||||
@@ -244,7 +247,10 @@ public class MeResourceTest {
|
||||
|
||||
@Test
|
||||
public void shouldGetSingleApiKey() throws URISyntaxException, UnsupportedEncodingException {
|
||||
when(apiKeyService.getKeys()).thenReturn(Arrays.asList(new ApiKey("1", "key 1", "READ"), new ApiKey("2", "key 2", "WRITE")));
|
||||
when(apiKeyService.getKeys())
|
||||
.thenReturn(asList(
|
||||
new ApiKey("1", "key 1", "READ", now()),
|
||||
new ApiKey("2", "key 2", "WRITE", now())));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get("/" + MeResource.ME_PATH_V2 + "api_keys/1");
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.time.Instant.now;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ApiKeyTokenHandlerTest {
|
||||
@@ -37,7 +38,7 @@ class ApiKeyTokenHandlerTest {
|
||||
|
||||
@Test
|
||||
void shouldSerializeAndDeserializeToken() {
|
||||
final String tokenString = handler.createToken("dent", new ApiKey("42", "hg2g", "READ"), "some secret");
|
||||
final String tokenString = handler.createToken("dent", new ApiKey("42", "hg2g", "READ", now()), "some secret");
|
||||
|
||||
System.out.println(tokenString);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user