-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1347 from folio-org/OKAPI-1081-restrict-invalid-t…
…enantids OKAPI-1081: Reject invalid tenant ids
- Loading branch information
Showing
10 changed files
with
250 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
okapi-core/src/main/java/org/folio/okapi/util/TenantValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.folio.okapi.util; | ||
|
||
import io.vertx.core.Future; | ||
import java.util.regex.Pattern; | ||
import org.folio.okapi.common.ErrorType; | ||
import org.folio.okapi.common.Messages; | ||
|
||
/** | ||
* Validate a tenant ID to match ^[a-z][a-z0-9]{0,30}$ as required by | ||
* <a href="https://folio-org.atlassian.net/wiki/display/TC/ADR-000002+-+Tenant+Id+and+Module+Name+Restrictions"> | ||
* https://folio-org.atlassian.net/wiki/display/TC/ADR-000002+-+Tenant+Id+and+Module+Name+Restrictions</a> | ||
* Technical Council decision. | ||
*/ | ||
public final class TenantValidator { | ||
/** Maximum length of a tenant ID. */ | ||
public static final int MAX_LENGTH = 31; | ||
|
||
// multi-byte sequences forbidden in pattern, so char length = byte length | ||
@SuppressWarnings("java:S5867") // we want to forbid Unicode characters, therefore we | ||
// suppress warning "Unicode-aware versions of character classes should be preferred" | ||
private static final String TENANT_PATTERN_STRING = "^[a-z][a-z0-9]{0,30}$"; | ||
private static final Pattern TENANT_PATTERN = Pattern.compile(TENANT_PATTERN_STRING); | ||
private static final Pattern STARTS_WITH_DIGIT = Pattern.compile("^\\d"); | ||
private static final Messages MESSAGES = Messages.getInstance(); | ||
|
||
private TenantValidator() { | ||
throw new UnsupportedOperationException("Cannot instantiate utility class."); | ||
} | ||
|
||
/** | ||
* Validate tenantId against ^[a-z][a-z0-9]{0,30}$ and return a failed Future with | ||
* clear violation explanation message on validation failure. | ||
*/ | ||
public static Future<Void> validate(String tenantId) { | ||
if (TENANT_PATTERN.matcher(tenantId).matches()) { | ||
return Future.succeededFuture(); | ||
} | ||
|
||
String message; | ||
|
||
if (tenantId.contains("_")) { | ||
message = "11609"; | ||
} else if (tenantId.contains("-")) { | ||
message = "11610"; | ||
} else if (tenantId.length() > MAX_LENGTH) { | ||
message = "11611"; | ||
} else if (STARTS_WITH_DIGIT.matcher(tenantId).matches()) { | ||
message = "11612"; | ||
} else { | ||
message = "11601"; | ||
} | ||
|
||
return Future.failedFuture(new OkapiError(ErrorType.USER, | ||
MESSAGES.getMessage(message, tenantId))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.