Skip to content

Commit

Permalink
Merge pull request #294 from AuthGuard/refactor/long-id
Browse files Browse the repository at this point in the history
Migrate from string IDs to number IDs
  • Loading branch information
kmehrunes committed Dec 14, 2023
2 parents 0bf5e2f + c110dac commit 0065654
Show file tree
Hide file tree
Showing 164 changed files with 754 additions and 598 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@JsonSerialize(as = AccountDTO.class)
@JsonDeserialize(as = AccountDTO.class)
public interface Account {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
@Value.Immutable
@DTOStyle
public interface AccountLock {
String getAccountId();
long getAccountId();
Instant getExpiresAt();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
public interface ActionToken {
String getToken();
String getAction();
String getAccountId();
Long getAccountId();
long getValidFor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
@JsonSerialize(as = ApiKeyDTO.class)
@JsonDeserialize(as = ApiKeyDTO.class)
public interface ApiKey {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
String getAppId();
Long getAppId();
String getKey();
String getType();
boolean isForClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
@JsonSerialize(as = AppDTO.class)
@JsonDeserialize(as = AppDTO.class)
public interface App {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
String getExternalId();
String getName();
String getAccountId();
Long getAccountId();
String getDomain();
String getBaseUrl();
List<PermissionDTO> getPermissions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
@JsonSerialize(as = ClientDTO.class)
@JsonDeserialize(as = ClientDTO.class)
public interface Client {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
String getExternalId();
String getName();
String getAccountId();
Long getAccountId();
String getDomain();
String getBaseUrl();
String getClientType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@DTOStyle
@JsonDeserialize(as = CredentialsDTO.class)
public interface Credentials {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
Instant getPasswordUpdatedAt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
@Value.Immutable
@DTOStyle
public interface ExchangeAttempt {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
String getEntityId();
Long getEntityId();
String getExchangeFrom();
String getExchangeTo();
boolean isSuccessful();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@JsonSerialize(as = PermissionDTO.class)
@JsonDeserialize(as = PermissionDTO.class)
public interface Permission {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
String getGroup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@JsonDeserialize(as = RoleDTO.class)
@JsonSerialize(as = RoleDTO.class)
public interface Role {
String getId();
long getId();
Instant getCreatedAt();
Instant getLastModified();
String getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public interface ApiKeyRequest {
boolean isForClient();
String getKeyType();
String getAppId();
Long getAppId();
Instant getExpiresAt();
DurationRequestDTO getValidFor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public interface CreateAppRequest {
String getExternalId();
String getName();
String getAccountId();
Long getAccountId();
String getDomain();
List<PermissionDTO> getPermissions();
List<String> getScopes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public interface CreateClientRequest {
String getExternalId();
String getName();
String getAccountId();
Long getAccountId();
String getDomain();
String getBaseUrl();
ClientType getClientType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
@JsonDeserialize(as = OtpRequestDTO.class)
@JsonSerialize(as = OtpRequestDTO.class)
public interface OtpRequest {
String getPasswordId();
Long getPasswordId();
String getPassword();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ApiKeysRequestValidator implements Validator<ApiKeyRequestDTO> {
@Override
public List<Violation> validate(final ApiKeyRequestDTO obj) {
return FluentValidator.begin()
.validate("appId", obj.getAppId(), Constraints.required, Constraints.reasonableLength)
.validate("appId", obj.getAppId(), Constraints.required)
.validate("keyType", obj.getKeyType(), Constraints.required, Constraints.reasonableLength)
.validate("validFor", obj.getValidFor(), durationRequest -> {
if (durationRequest == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class CreateAppRequestValidator implements Validator<CreateAppRequest> {
public List<Violation> validate(final CreateAppRequest obj) {
return FluentValidator.begin()
.validate("externalId", obj.getExternalId(), Constraints.reasonableLength)
.validate("accountId", obj.getAccountId(), Constraints.reasonableLength)
.validate("name", obj.getName(), Constraints.required, Constraints.reasonableLength)
.validate("domain", obj.getDomain(), Constraints.required)
.getViolations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class CreateClientRequestValidator implements Validator<CreateClientReque
public List<Violation> validate(CreateClientRequestDTO obj) {
return FluentValidator.begin()
.validate("externalId", obj.getExternalId(), Constraints.reasonableLength)
.validate("accountId", obj.getAccountId(), Constraints.reasonableLength)
.validate("name", obj.getName(), Constraints.required, Constraints.reasonableLength)
.validate("domain", obj.getDomain(), Constraints.required)
.validate("clientType", obj.getClientType(), Constraints.required)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ApiKeysRequestValidatorTest {
@Test
void validate() {
final ApiKeyRequestDTO request = ApiKeyRequestDTO.builder()
.appId("app")
.appId(1L)
.keyType("default")
.build();

Expand All @@ -44,7 +44,7 @@ void validateMissingValues() {
@Test
void validateWithZeroValidity() {
final ApiKeyRequestDTO request = ApiKeyRequestDTO.builder()
.appId("app")
.appId(1L)
.keyType("default")
.validFor(DurationRequestDTO.builder()
.build())
Expand All @@ -59,7 +59,7 @@ void validateWithZeroValidity() {
@Test
void validateWithNonZeroValidity() {
final ApiKeyRequestDTO request = ApiKeyRequestDTO.builder()
.appId("app")
.appId(1L)
.keyType("default")
.validFor(DurationRequestDTO.builder()
.days(1)
Expand All @@ -77,7 +77,7 @@ void validateWithNonZeroValidity() {
@Test
void validateWithNegativeDaysValidity() {
final ApiKeyRequestDTO request = ApiKeyRequestDTO.builder()
.appId("app")
.appId(1L)
.keyType("default")
.validFor(DurationRequestDTO.builder()
.days(-1)
Expand All @@ -95,7 +95,7 @@ void validateWithNegativeDaysValidity() {
@Test
void validateWithNegativeHoursValidity() {
final ApiKeyRequestDTO request = ApiKeyRequestDTO.builder()
.appId("app")
.appId(1L)
.keyType("default")
.validFor(DurationRequestDTO.builder()
.hours(-1)
Expand All @@ -113,7 +113,7 @@ void validateWithNegativeHoursValidity() {
@Test
void validateWithNegativeMinutesValidity() {
final ApiKeyRequestDTO request = ApiKeyRequestDTO.builder()
.appId("app")
.appId(1L)
.keyType("default")
.validFor(DurationRequestDTO.builder()
.minutes(-1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OtpRequestValidatorTest {
@Test
void validateValid() {
final OtpRequestDTO request = OtpRequestDTO.builder()
.passwordId("password-id")
.passwordId(1L)
.password("password")
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public AuthResponseBO generateToken(final AppBO app) {
throw new UnsupportedOperationException("OTPs cannot be generated for applications");
}

private AuthResponseBO createToken(final String passwordId, final String accountId) {
private AuthResponseBO createToken(final long passwordId, final long accountId) {
return AuthResponseBO.builder()
.type(TOKEN_TYPE)
.token(passwordId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.vavr.control.Either;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.Optional;

public class OtpVerifier implements AuthVerifier {
Expand All @@ -25,7 +24,7 @@ public OtpVerifier(final OtpRepository otpRepository, final ServiceMapper servic
}

@Override
public Either<Exception, String> verifyAccountToken(final String token) {
public Either<Exception, Long> verifyAccountToken(final String token) {
// TODO: no need to have a special format for the token, just receive the two parts in the request
final String[] parts = token.split(":");

Expand All @@ -34,7 +33,14 @@ public Either<Exception, String> verifyAccountToken(final String token) {
"Invalid OTP token format"));
}

final String passwordId = parts[0];
final long passwordId;

try {
passwordId = Long.parseLong(parts[0]);
} catch (Exception e) {
return Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_AUTHORIZATION_FORMAT,
"Invalid OTP ID"));
}
final String otp = parts[1];

final Optional<OneTimePasswordBO> generatedOpt = otpRepository.getById(passwordId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.vavr.control.Either;

import java.time.Instant;
import java.time.OffsetDateTime;

public class PasswordlessVerifier implements AuthVerifier {
private final AccountTokensRepository accountTokensRepository;
Expand All @@ -21,15 +20,15 @@ public PasswordlessVerifier(final AccountTokensRepository accountTokensRepositor
}

@Override
public Either<Exception, String> verifyAccountToken(final String passwordlessToken) {
public Either<Exception, Long> verifyAccountToken(final String passwordlessToken) {
return accountTokensRepository.getByToken(passwordlessToken)
.join()
.map(this::verifyToken)
.orElseGet(() -> Either.left(new ServiceAuthorizationException(ErrorCode.INVALID_TOKEN,
"No passwordless token found for " + passwordlessToken)));
}

private Either<Exception, String> verifyToken(final AccountTokenDO accountToken) {
private Either<Exception, Long> verifyToken(final AccountTokenDO accountToken) {
if (accountToken.getExpiresAt().isBefore(Instant.now())) {
return Either.left(new ServiceAuthorizationException(ErrorCode.EXPIRED_TOKEN, "Expired passwordless token",
EntityType.ACCOUNT, accountToken.getAssociatedAccountId()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void setup() {

private AccountBO createCredentials(final String username) {
return AccountBO.builder()
.id("credentials")
.id(1)
.active(true)
.addIdentifiers(UserIdentifierBO.builder()
.identifier(username)
Expand Down Expand Up @@ -215,12 +215,6 @@ void authenticateWithPreviousPasswordVersionWrongPassword() {
assertThat(result.getLeft()).isInstanceOf(ServiceAuthorizationException.class);
}

@Test
void authenticateBadAuthorization() {
final String authorization = RandomStringUtils.randomAlphanumeric(20);
assertThatThrownBy(() -> basicAuth.authenticateAndGetAccount(authorization)).isInstanceOf(ServiceException.class);
}

@Test
void authenticateBadBasicScheme() {
final String authorization = "dGhpc2RvbmVzbid0Zmx5aW5vdXJjaXR5";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void verify() {
Mockito.when(mockOtpRepository.getById(otp.getId()))
.thenReturn(CompletableFuture.completedFuture(Optional.of(otp)));

final Either<Exception, String> generated = otpVerifier.verifyAccountToken(otp.getId() + ":" + otp.getPassword());
final Either<Exception, Long> generated = otpVerifier.verifyAccountToken(otp.getId() + ":" + otp.getPassword());

assertThat(generated.get()).isEqualTo(otp.getAccountId());
}
Expand Down Expand Up @@ -84,7 +84,7 @@ void verifyInvalidOtpFormat() {

setup(otpConfig);

final Either<Exception, String> result = otpVerifier.verifyAccountToken("not a valid OTP");
final Either<Exception, Long> result = otpVerifier.verifyAccountToken("not a valid OTP");

assertThat(result.isLeft()).isTrue();
assertThat(result.getLeft()).isInstanceOf(ServiceAuthorizationException.class);
Expand All @@ -105,7 +105,7 @@ void verifyPasswordNotFound() {
Mockito.when(mockOtpRepository.getById(otp.getId()))
.thenReturn(CompletableFuture.completedFuture(Optional.empty()));

final Either<Exception, String> result = otpVerifier.verifyAccountToken(otp.getId() + ":" + otp.getPassword());
final Either<Exception, Long> result = otpVerifier.verifyAccountToken(otp.getId() + ":" + otp.getPassword());

assertThat(result.isLeft()).isTrue();
assertThat(result.getLeft()).isInstanceOf(ServiceAuthorizationException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void generateToken() {
setup(passwordlessConfig);

final AccountBO account = AccountBO.builder()
.id("account")
.id(101)
.active(true)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import java.util.concurrent.CompletableFuture;

public interface AccountLocksRepository {
CompletableFuture<Collection<AccountLockDO>> findByAccountId(String accountId);
CompletableFuture<Collection<AccountLockDO>> findByAccountId(long accountId);

CompletableFuture<AccountLockDO> save(AccountLockDO accountLock);

CompletableFuture<Optional<AccountLockDO>> delete(String id);
CompletableFuture<Optional<AccountLockDO>> delete(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public interface OtpRepository {
CompletableFuture<OneTimePasswordDO> save(OneTimePasswordDO password);
CompletableFuture<Optional<OneTimePasswordDO>> getById(String id);
CompletableFuture<Optional<OneTimePasswordDO>> getById(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public interface SessionsRepository {
CompletableFuture<SessionDO> save(final SessionDO session);

CompletableFuture<Optional<SessionDO>> getById(final String sessionId);
CompletableFuture<Optional<SessionDO>> getById(final long sessionId);

CompletableFuture<Optional<SessionDO>> getByToken(final String sessionToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
@MappedSuperclass
public abstract class AbstractDO {
@Id
@Convert(converter = LongToStringConverter.class)
private String id;
private long id;
private boolean deleted;
private Instant createdAt;
private Instant lastModified;
Expand Down

0 comments on commit 0065654

Please sign in to comment.