2929/** Validates the Common Name of a terminal API certificate. */
3030public final class TerminalCommonNameValidator {
3131
32- // regex for Terminal API CN format
33- private static final String TERMINAL_API_CN_TEST_ENVIRONMENT =
34- "[a-zA-Z0-9]{3,}-[a-zA-Z0-9]{9,15}\\ .test\\ .terminal\\ .adyen\\ .com" ;
35- private static final String TERMINAL_API_CN_LIVE_ENVIRONMENT =
36- "[a-zA-Z0-9]{3,}-[a-zA-Z0-9]{9,15}\\ .live\\ .terminal\\ .adyen\\ .com" ;
37- // regex for Legacy format
38- private static final String TERMINAL_API_LEGACY_CN_TEST_ENVIRONMENT =
32+ // Precompiled regex for Terminal API CN format
33+ private static final Pattern TERMINAL_API_CN_TEST =
34+ Pattern .compile ("[a-zA-Z0-9]{3,}-[a-zA-Z0-9]{9,15}\\ .test\\ .terminal\\ .adyen\\ .com" );
35+ private static final Pattern TERMINAL_API_CN_LIVE =
36+ Pattern .compile ("[a-zA-Z0-9]{3,}-[a-zA-Z0-9]{9,15}\\ .live\\ .terminal\\ .adyen\\ .com" );
37+
38+ // Exact strings for legacy format (no regex needed)
39+ private static final String TERMINAL_API_LEGACY_TEST =
3940 "legacy-terminal-certificate.test.terminal.adyen.com" ;
40- private static final String TERMINAL_API_LEGACY_CN_LIVE_ENVIRONMENT =
41+ private static final String TERMINAL_API_LEGACY_LIVE =
4142 "legacy-terminal-certificate.live.terminal.adyen.com" ;
4243
44+ // Regex to extract CN from subject string
45+ private static final Pattern SUBJECT_ATTRIBUTE_PATTERN =
46+ Pattern .compile ("(?:^|,\\ s?)([A-Z]+)=((?:\" [^\" ]+\" )|[^,]+)" );
47+
4348 private TerminalCommonNameValidator () {}
4449
4550 /**
@@ -51,53 +56,33 @@ private TerminalCommonNameValidator() {}
5156 * @return true if the Common Name is valid, false otherwise.
5257 */
5358 public static boolean validateCertificate (X509Certificate certificate , Environment environment ) {
54-
5559 String name = certificate .getSubjectX500Principal ().getName ();
56- String patternRegex = "(?:^|,\\ s?)(?:([A-Z]+)=(\" (?:[^\" ]|\" \" )+\" |[^,]+))+" ;
57- Pattern pattern = Pattern .compile (patternRegex );
58- Matcher matcher = pattern .matcher (name );
60+ Matcher matcher = SUBJECT_ATTRIBUTE_PATTERN .matcher (name );
5961
60- boolean valid = false ;
61- while (matcher .find () && !valid ) {
62+ while (matcher .find ()) {
6263 String groupName = matcher .group (1 );
6364 if ("CN" .equals (groupName )) {
6465 String commonName = matcher .group (2 );
65- valid =
66- commonName != null
67- &&
68- // must match any of the regex
69- (commonName .matches (getEnvironmentRegex (environment ))
70- || commonName .equals (getEnvironmentRegexLegacy (environment )));
66+ return isValidCommonName (commonName , environment );
7167 }
7268 }
73- return valid ;
69+ return false ;
7470 }
7571
76- /**
77- * Returns the regex for the given {@link Environment}.
78- *
79- * @param environment Environment
80- * @return String with the regex
81- */
82- private static String getEnvironmentRegex (Environment environment ) {
83- if (environment == Environment .LIVE ) {
84- return TERMINAL_API_CN_LIVE_ENVIRONMENT ;
85- } else {
86- return TERMINAL_API_CN_TEST_ENVIRONMENT ;
72+ private static boolean isValidCommonName (String commonName , Environment environment ) {
73+ if (commonName == null ) {
74+ return false ;
8775 }
88- }
8976
90- /**
91- * Returns the LEGACY regex for the given {@link Environment}.
92- *
93- * @param environment Environment
94- * @return String with the regex
95- */
96- private static String getEnvironmentRegexLegacy (Environment environment ) {
97- if (environment == Environment .LIVE ) {
98- return TERMINAL_API_LEGACY_CN_LIVE_ENVIRONMENT ;
99- } else {
100- return TERMINAL_API_LEGACY_CN_TEST_ENVIRONMENT ;
77+ switch (environment ) {
78+ case LIVE :
79+ return TERMINAL_API_CN_LIVE .matcher (commonName ).matches ()
80+ || TERMINAL_API_LEGACY_LIVE .equals (commonName );
81+ case TEST :
82+ return TERMINAL_API_CN_TEST .matcher (commonName ).matches ()
83+ || TERMINAL_API_LEGACY_TEST .equals (commonName );
84+ default :
85+ return false ;
10186 }
10287 }
10388}
0 commit comments