|
22 | 22 |
|
23 | 23 | package eu.webeid.security.validator; |
24 | 24 |
|
| 25 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | +import com.fasterxml.jackson.databind.ObjectReader; |
25 | 27 | import eu.webeid.security.authtoken.WebEidAuthToken; |
26 | 28 | import eu.webeid.security.exceptions.AuthTokenException; |
| 29 | +import eu.webeid.security.exceptions.AuthTokenParseException; |
27 | 30 | import eu.webeid.security.exceptions.JceException; |
28 | 31 | import eu.webeid.security.validator.ocsp.OcspClient; |
| 32 | +import eu.webeid.security.validator.versionvalidators.AuthTokenVersionValidatorFactory; |
29 | 33 | import org.slf4j.Logger; |
30 | 34 | import org.slf4j.LoggerFactory; |
31 | 35 |
|
| 36 | +import java.io.IOException; |
32 | 37 | import java.security.cert.X509Certificate; |
33 | 38 |
|
34 | 39 | /** |
|
37 | 42 | final class AuthTokenValidatorManager implements AuthTokenValidator { |
38 | 43 |
|
39 | 44 | private static final Logger LOG = LoggerFactory.getLogger(AuthTokenValidatorManager.class); |
40 | | - private final AuthTokenValidatorFactory tokenValidatorFactory; |
| 45 | + |
| 46 | + private final AuthTokenVersionValidatorFactory tokenValidatorFactory; |
| 47 | + |
| 48 | + // Use human-readable meaningful names for token limits. |
| 49 | + private final int TOKEN_MIN_LENGTH = 100; |
| 50 | + private final int TOKEN_MAX_LENGTH = 10000; |
| 51 | + |
| 52 | + private final ObjectReader TOKEN_READER = new ObjectMapper().readerFor(WebEidAuthToken.class); |
41 | 53 |
|
42 | 54 | AuthTokenValidatorManager(AuthTokenValidationConfiguration configuration, OcspClient ocspClient) |
43 | 55 | throws JceException { |
44 | | - this.tokenValidatorFactory = AuthTokenValidatorFactory.create(configuration, ocspClient); |
| 56 | + this.tokenValidatorFactory = AuthTokenVersionValidatorFactory.create(configuration, ocspClient); |
45 | 57 | } |
46 | 58 |
|
47 | 59 | @Override |
48 | 60 | public WebEidAuthToken parse(String authToken) throws AuthTokenException { |
49 | | - return AuthTokenValidator.super.parse(authToken); |
| 61 | + try { |
| 62 | + LOG.info("Starting token parsing"); |
| 63 | + validateTokenLength(authToken); |
| 64 | + return parseToken(authToken); |
| 65 | + } catch (Exception e) { |
| 66 | + // Generally "log and rethrow" is an antipattern, but it fits with the surrounding logging style. |
| 67 | + LOG.warn("Token parsing was interrupted:", e); |
| 68 | + throw e; |
| 69 | + } |
50 | 70 | } |
51 | 71 |
|
52 | 72 | @Override |
53 | 73 | public X509Certificate validate(WebEidAuthToken authToken, String currentChallengeNonce) throws AuthTokenException { |
54 | 74 | try { |
55 | 75 | LOG.info("Starting token validation"); |
56 | | - return tokenValidatorFactory.getValidatorFor(authToken.getFormat()).validate(authToken, currentChallengeNonce); |
| 76 | + return tokenValidatorFactory |
| 77 | + .getValidatorFor(authToken.getFormat()) |
| 78 | + .validate(authToken, currentChallengeNonce); |
57 | 79 | } catch (Exception e) { |
58 | | - // Generally "log and rethrow" is an anti-pattern, but it fits with the surrounding logging style. |
| 80 | + // Generally "log and rethrow" is an antipattern, but it fits with the surrounding logging style. |
59 | 81 | LOG.warn("Token validation was interrupted:", e); |
60 | 82 | throw e; |
61 | 83 | } |
62 | 84 | } |
| 85 | + |
| 86 | + private void validateTokenLength(String authToken) throws AuthTokenParseException { |
| 87 | + if (authToken == null || authToken.length() < TOKEN_MIN_LENGTH) { |
| 88 | + throw new AuthTokenParseException("Auth token is null or too short"); |
| 89 | + } |
| 90 | + if (authToken.length() > TOKEN_MAX_LENGTH) { |
| 91 | + throw new AuthTokenParseException("Auth token is too long"); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private WebEidAuthToken parseToken(String authToken) throws AuthTokenParseException { |
| 96 | + try { |
| 97 | + final WebEidAuthToken token = TOKEN_READER.readValue(authToken); |
| 98 | + if (token == null) { |
| 99 | + throw new AuthTokenParseException("Web eID authentication token is null"); |
| 100 | + } |
| 101 | + return token; |
| 102 | + } catch (IOException e) { |
| 103 | + throw new AuthTokenParseException("Error parsing Web eID authentication token", e); |
| 104 | + } |
| 105 | + } |
| 106 | + |
63 | 107 | } |
0 commit comments